ld31-space-diggers/src/game.coffee

173 lines
4.3 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-07 15:52:54 +01:00
@mouseX = 0
2014-12-07 16:27:44 +01:00
@mouseY = 0
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-07 15:52:54 +01:00
@speechbubble.start()
2014-12-07 00:15:16 +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 12:55:43 +01:00
@hud.render()
2014-12-07 15:52:54 +01:00
@speechbubble.render()
2014-12-06 09:24:36 +01:00
2014-12-06 17:26:12 +01:00
mousedown: (event)->
2014-12-07 16:27:44 +01:00
tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
# Can't click on dead tiles
return unless tile and tile.isBuildable
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'
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 15:52:54 +01:00
@mouseX = event.x
@mouseY = event.y
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 17:21:58 +01:00
when "i" then app.game.hud.showBuildInfo()
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-07 17:57:33 +01:00
when "1" then @releaseRes 'stardust'
when "2" then @releaseRes 'dirt'
when "3" then @releaseRes 'bedrock'
when "4" then @releaseRes 'oxodum'
when "5" then @releaseRes 'lubinit'
when "6" then @releaseRes 'darkana'
when "7" then @releaseRes 'bio'
when "8" then @releaseRes 'notch'
when "9" then @releaseRes 'lava'
2014-12-06 17:58:03 +01:00
when "space"
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
2014-12-07 17:57:33 +01:00
releaseRes: (string) ->
@resources[string] = 0
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 17:31:30 +01:00
if @currentSelectedTile and !@currentSelectedTile.entity
if @checkResource('stardust', 30, true)
@currentSelectedTile.entity = new Miner
else
@speechbubble.say 'nores'
2014-12-07 17:41:21 +01:00
else
@speechbubble.say 'nosel'
2014-12-06 17:26:12 +01:00
2014-12-07 00:19:22 +01:00
createSilo: ->
2014-12-07 15:52:54 +01:00
if @currentSelectedTile
if @checkPosition(@currentSelectedTile)
if @checkResource('stardust', 20, true)
@currentSelectedTile.entity = new Silo
2014-12-07 17:31:30 +01:00
else
@speechbubble.say 'nores'
2014-12-07 15:52:54 +01:00
else
@speechbubble.say 'toofar'
2014-12-07 17:41:21 +01:00
else
@speechbubble.say 'nosel'
2014-12-07 00:19:22 +01:00
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