ld31-space-diggers/src/game.coffee

265 lines
6.7 KiB
CoffeeScript
Raw Permalink 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-07 19:11:11 +01:00
@cutScene = false
2014-12-07 18:59:59 +01:00
@intro = false
2014-12-07 19:11:11 +01:00
@titleScreen = true
2014-12-07 18:59:59 +01:00
2014-12-07 19:11:11 +01:00
# TODO: Refactor this whole crap ... I can't belive I am writing this ...
2014-12-07 18:59:59 +01:00
startCutScene: ->
2014-12-07 19:11:11 +01:00
if @titleScreen
@titleScreen = false
@cutScene = true
app.game.cutSceneImage = app.images.intro1
window.setTimeout app.game.cutScene2, 1500
2014-12-07 18:59:59 +01:00
cutScene2: =>
2014-12-07 19:11:11 +01:00
app.game.cutSceneImage = app.images.intro2
window.setTimeout app.game.cutScene3, 2000
2014-12-07 18:59:59 +01:00
cutScene3: =>
2014-12-07 19:11:11 +01:00
app.game.cutSceneImage = app.images.intro3
window.setTimeout app.game.cutScene4, 1500
2014-12-07 18:59:59 +01:00
cutScene4: =>
2014-12-07 19:11:11 +01:00
app.game.cutSceneImage = app.images.intro4
window.setTimeout app.game.cutSceneEnd, 1500
2014-12-07 18:59:59 +01:00
cutSceneEnd: =>
2014-12-07 19:11:11 +01:00
app.game.cutScene = false
app.game.intro = true
app.game.startIntro()
2014-12-07 18:41:33 +01:00
startIntro: ->
@speechbubble.setFix 90, 27
@speechbubble.say 'help', 2000
@timeout = window.setTimeout @intro2, 3000
intro2: =>
app.game.speechbubble.say 'damn', 2000
app.game.timeout = window.setTimeout app.game.intro3, 3000
intro3: =>
app.game.speechbubble.say 'need', 3500
app.game.timeout = window.setTimeout app.game.intro4, 4500
intro4: =>
app.game.speechbubble.say 'collect', 3500
app.game.timeout = window.setTimeout app.game.introEnd, 3500
introEnd: =>
@intro = false
app.game.speechbubble.setMouse()
2014-12-06 10:34:53 +01:00
render: ->
2014-12-07 19:11:11 +01:00
if @titleScreen
app.layer.drawImage app.images.titlescreen, 0, 0, 20*8, 15*8
return
2014-12-07 18:59:59 +01:00
if @cutScene
app.layer.drawImage @cutSceneImage, 0, 0, 20*8, 15*8
return
2014-12-07 18:50:10 +01:00
if @gameEndCheck()
app.layer.drawImage app.images.end, 0, 0, 20*8, 15*8
return
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'
2014-12-07 18:53:30 +01:00
if event.x > 143 and event.x < 151 and event.y > 95 and event.y < 106
app.game.hud.showBuildInfo()
else
tile.click(event.button)
2014-12-07 13:42:45 +01:00
2014-12-07 18:53:30 +01:00
@currentSelectedTile.deselect() if @currentSelectedTile
tile.select()
@currentSelectedTile = tile
2014-12-07 13:42:45 +01:00
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
2014-12-07 19:11:11 +01:00
when "enter" then @startCutScene()
2014-12-06 17:58:03 +01:00
when "m" then @createMiner()
2014-12-07 17:52:10 +01:00
when "e" then @createSolarpanel()
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
2014-12-07 20:48:54 +01:00
if @checkResource('lubinit', 5, true)
2014-12-07 17:31:30 +01:00
@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)
2014-12-07 20:48:54 +01:00
if @checkResource('dirt', 5, true)
2014-12-07 15:52:54 +01:00
@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 18:03:46 +01:00
else
@speechbubble.say 'nosel'
2014-12-07 00:19:22 +01:00
2014-12-07 18:50:10 +01:00
# TODO: OMG refactor this ...
2014-12-07 17:52:10 +01:00
createSolarpanel: ->
if @currentSelectedTile
if @checkPosition(@currentSelectedTile)
2014-12-07 20:48:54 +01:00
if @checkResource('notch', 30, true)
2014-12-07 17:52:10 +01:00
@currentSelectedTile.entity = new Solarpanel
2014-12-07 17:56:52 +01:00
else
@speechbubble.say 'nores'
2014-12-07 17:52:10 +01:00
else
@speechbubble.say 'toofar'
2014-12-07 18:03:46 +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-07 20:48:54 +01:00
maxTileAmount: 75
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-07 18:37:04 +01:00
solarpanelCount: ->
energy = 0
for tile in app.game.map
2014-12-07 18:50:10 +01:00
# FIXME: Thats not a sane solution hahahah :D
2014-12-07 18:37:04 +01:00
energy += 1 if tile.entity and tile.entity.energyProvided > 10
energy
gameEndCheck: ->
return true if @solarpanelCount() >= 4
false
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