ld31-space-diggers/src/game.coffee

78 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-12-06 12:37:57 +01:00
app.game =
2014-12-06 08:37:36 +01:00
start: ->
2014-12-06 15:09:56 +01:00
for i in [0..20*15-1]
2014-12-06 10:34:53 +01:00
@map[i] = new Tile
2014-12-06 17:26:12 +01:00
@currentHoveredTile = new Tile
2014-12-06 17:58:03 +01:00
@currentSelectedTile = new Tile
2014-12-06 17:17:33 +01:00
2014-12-06 10:34:53 +01:00
render: ->
2014-12-06 12:37:57 +01:00
for tile, i in @map
2014-12-06 15:09:56 +01:00
y = Math.floor(i/20)
x = i-(y*20)
tile.render(x, y)
2014-12-06 16:49:29 +01:00
2014-12-06 12:55:43 +01:00
@hud.render()
2014-12-06 09:24:36 +01:00
2014-12-06 08:37:36 +01:00
step: ->
2014-12-06 10:34:53 +01:00
2014-12-06 17:26:12 +01:00
mousedown: (event)->
2014-12-06 17:58:03 +01:00
tile = @posToTile(event.x, event.y)
tile.click(event.button)
@currentSelectedTile.deselect() if @currentSelectedTile
tile.select()
@currentSelectedTile = tile
2014-12-06 17:26:12 +01:00
mousemove: (event)->
tile = @posToTile(event.x, event.y)
if tile
if tile != @currentHoveredTile
tile.moveIn()
@currentHoveredTile.moveOut()
@currentHoveredTile = tile
2014-12-06 17:58:03 +01:00
keyup: (event) ->
switch event.key
when "m" then @createMiner()
2014-12-06 20:05:02 +01:00
when "c" then @cheatah()
2014-12-06 17:58:03 +01:00
when "space"
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
createMiner: ->
2014-12-06 20:05:02 +01:00
if @currentSelectedTile and @checkRessources 'lubinit', 30, true
2014-12-06 17:58:03 +01:00
newMiner = new Miner
2014-12-06 20:05:02 +01:00
2014-12-06 17:58:03 +01:00
@miners.push = newMiner
@currentSelectedTile.attachMiner newMiner
2014-12-06 20:05:02 +01:00
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
2014-12-06 17:26:12 +01:00
posToTile: (x, y)->
2014-12-06 19:01:21 +01:00
pos = (Math.floor(y/8)*20) + Math.floor(x/8)
2014-12-06 17:26:12 +01:00
@map[pos]
2014-12-06 20:05:02 +01:00
checkRessources: (type, amount, drain = false) ->
if @resources[type] >= amount
@resources[type] -= amount if drain
return true
false
cheatah: ->
for type, amount of @resources
@resources[type] = 100
2014-12-06 10:34:53 +01:00
map: []
2014-12-06 20:39:59 +01:00
miners: []
2014-12-06 17:32:37 +01:00
maxTileAmount: 15
2014-12-06 08:37:36 +01:00
resources:
2014-12-06 17:38:03 +01:00
stardust: 0
bedrock: 0
2014-12-06 13:14:15 +01:00
lubinit: 0
oxodum: 0
notch: 0