ld31-space-diggers/src/game.coffee

136 lines
3.4 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-07 14:07:13 +01:00
@map[i] = new Tile(i)
2014-12-07 00:19:22 +01:00
@map[20*5+10].entity = new Base
2014-12-06 17:26:12 +01:00
2014-12-07 14:07:13 +01:00
@currentHoveredTile = new Tile(-1)
2014-12-07 01:08:39 +01:00
@currentSelectedTile = null
2014-12-06 17:17:33 +01:00
2014-12-07 00:19:22 +01:00
window.setInterval @tick, 1000
2014-12-07 00:15:16 +01:00
@hud.start()
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 12:55:43 +01:00
@hud.render()
2014-12-06 09:24:36 +01:00
2014-12-06 17:26:12 +01:00
mousedown: (event)->
2014-12-07 14:07:13 +01:00
if @isMouseInView event.x/8, event.y/8
2014-12-07 13:42:45 +01:00
switch event.button
when 'left'
2014-12-07 14:07:13 +01:00
tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
2014-12-07 13:42:45 +01:00
tile.click(event.button)
@currentSelectedTile.deselect() if @currentSelectedTile
tile.select()
@currentSelectedTile = tile
when 'right'
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
2014-12-06 17:26:12 +01:00
mousemove: (event)->
2014-12-07 13:19:53 +01:00
if @isMouseInView event.x, event.y
2014-12-07 14:07:13 +01:00
tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
2014-12-07 13:19:53 +01:00
if tile
if tile != @currentHoveredTile
tile.moveIn()
@currentHoveredTile.moveOut() if @currentHoveredTile
@currentHoveredTile = tile
else
@currentHoveredTile.moveOut() if @currentHoveredTile
@currentHoveredTile = null
isMouseInView: (mouseX, mouseY) ->
if mouseX < app.width and mouseX >= 0 and mouseY < app.height and mouseY >= 0
return true
false
2014-12-06 17:26:12 +01:00
2014-12-06 17:58:03 +01:00
keyup: (event) ->
switch event.key
when "m" then @createMiner()
2014-12-07 00:19:22 +01:00
when "s" then @createSilo()
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
tick: =>
tile.tick() for tile in app.game.map
2014-12-06 20:52:56 +01:00
2014-12-06 17:58:03 +01:00
createMiner: ->
2014-12-07 14:07:13 +01:00
if @currentSelectedTile and !@currentSelectedTile.entity and @checkResource('stardust', 30, true)
2014-12-06 22:07:28 +01:00
@currentSelectedTile.entity = new Miner
2014-12-06 17:26:12 +01:00
2014-12-07 00:19:22 +01:00
createSilo: ->
2014-12-07 14:07:13 +01:00
if @currentSelectedTile and @checkPosition(@currentSelectedTile) and @checkResource('stardust', 20, true)
2014-12-07 00:19:22 +01:00
@currentSelectedTile.entity = new Silo
checkResource: (type, amount, drain = false) ->
2014-12-06 20:05:02 +01:00
if @resources[type] >= amount
@resources[type] -= amount if drain
return true
false
2014-12-07 14:07:13 +01:00
checkPosition: (tile)->
return false if tile.entity
# Check all 4 directions
[x, y] = posToXY(tile.position)
# Left
return true if app.game.map[xyToPos(x-1, y)].entity \
and app.game.map[xyToPos(x-1, y)].entity.isDockable \
and x > 0
# Right
return true if app.game.map[xyToPos(x+1, y)].entity \
and app.game.map[xyToPos(x+1, y)].entity.isDockable \
and x < 19
# Top
return true if app.game.map[xyToPos(x, y-1)].entity \
and app.game.map[xyToPos(x, y-1)].entity.isDockable \
and y > 0
# Bottom
return true if app.game.map[xyToPos(x, y+1)].entity \
and app.game.map[xyToPos(x, y+1)].entity.isDockable \
and y < 14
false
2014-12-06 20:05:02 +01:00
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
2014-12-07 00:46:46 +01:00
availableSiloStorage: ->
2014-12-07 14:07:13 +01:00
space = 0
2014-12-07 00:46:46 +01:00
for tile in app.game.map
2014-12-07 13:22:35 +01:00
space += tile.entity.spaceProvided if tile.entity
2014-12-07 00:46:46 +01:00
space
usedSiloStorage: ->
space = 0
space += amount for resource, amount of @resources
space
2014-12-06 08:37:36 +01:00
resources:
2014-12-06 17:38:03 +01:00
stardust: 0
2014-12-06 20:51:21 +01:00
dirt: 0
2014-12-06 17:38:03 +01:00
bedrock: 0
2014-12-06 13:14:15 +01:00
oxodum: 0
2014-12-06 20:51:21 +01:00
lubinit: 0
darkana: 0
bio: 0
2014-12-06 13:14:15 +01:00
notch: 0
2014-12-06 20:51:21 +01:00
lava: 0