ld31-space-diggers/src/game.coffee

260 lines
6.5 KiB
CoffeeScript

app.game =
start: ->
for i in [0..20*15-1]
@map[i] = new Tile(i)
@map[20*5+10].entity = new Base
# TODO: BUG BUG: ... the base is some time replaced by a tile ...
@mouseX = 0
@mouseY = 0
@currentHoveredTile = new Tile(-1)
@currentSelectedTile = null
window.setInterval @tick, 1000
@hud.start()
@speechbubble.start()
@cutScene = true
@cutSceneImage = app.images.intro1
@intro = false
@startCutScene()
# TODO: Refactor this whole crap ... I can't belive I am writing rthis ...
startCutScene: ->
window.setTimeout @cutScene2, 500
cutScene2: =>
@cutSceneImage = app.images.intro2
window.setTimeout @cutScene3, 500
cutScene3: =>
@cutSceneImage = app.images.intro3
window.setTimeout @cutScene4, 500
cutScene4: =>
@cutSceneImage = app.images.intro4
window.setTimeout @cutSceneEnd, 500
cutSceneEnd: =>
@cutScene = false
@intro = true
@startIntro()
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()
render: ->
if @cutScene
app.layer.drawImage @cutSceneImage, 0, 0, 20*8, 15*8
return
if @gameEndCheck()
app.layer.drawImage app.images.end, 0, 0, 20*8, 15*8
return
for tile, i in @map
y = Math.floor(i/20)
x = i-(y*20)
tile.render(x, y)
@hud.render()
@speechbubble.render()
mousedown: (event)->
tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
# Can't click on dead tiles
return unless tile and tile.isBuildable
if @isMouseInView event.x/8, event.y/8
switch event.button
when 'left'
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)
@currentSelectedTile.deselect() if @currentSelectedTile
tile.select()
@currentSelectedTile = tile
when 'right'
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
mousemove: (event)->
@mouseX = event.x
@mouseY = event.y
if @isMouseInView event.x, event.y
tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
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
keyup: (event) ->
switch event.key
when "m" then @createMiner()
when "e" then @createSolarpanel()
when "i" then app.game.hud.showBuildInfo()
when "s" then @createSilo()
when "c" then @cheatah()
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'
when "space"
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
releaseRes: (string) ->
@resources[string] = 0
tick: =>
tile.tick() for tile in app.game.map
createMiner: ->
if @currentSelectedTile and !@currentSelectedTile.entity
if @checkResource('stardust', 30, true)
@currentSelectedTile.entity = new Miner
else
@speechbubble.say 'nores'
else
@speechbubble.say 'nosel'
createSilo: ->
if @currentSelectedTile
if @checkPosition(@currentSelectedTile)
if @checkResource('stardust', 20, true)
@currentSelectedTile.entity = new Silo
else
@speechbubble.say 'nores'
else
@speechbubble.say 'toofar'
else
@speechbubble.say 'nosel'
# TODO: OMG refactor this ...
createSolarpanel: ->
if @currentSelectedTile
if @checkPosition(@currentSelectedTile)
if @checkResource('notch', 10, true)
@currentSelectedTile.entity = new Solarpanel
else
@speechbubble.say 'nores'
else
@speechbubble.say 'toofar'
else
@speechbubble.say 'nosel'
checkResource: (type, amount, drain = false) ->
if @resources[type] >= amount
@resources[type] -= amount if drain
return true
false
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
cheatah: ->
for type, amount of @resources
@resources[type] = 100
map: []
miners: []
maxTileAmount: 15
availableSiloStorage: ->
space = 0
for tile in app.game.map
space += tile.entity.spaceProvided if tile.entity
space
usedSiloStorage: ->
space = 0
space += amount for resource, amount of @resources
space
solarpanelCount: ->
energy = 0
for tile in app.game.map
# FIXME: Thats not a sane solution hahahah :D
energy += 1 if tile.entity and tile.entity.energyProvided > 10
energy
gameEndCheck: ->
return true if @solarpanelCount() >= 4
false
resources:
stardust: 0
dirt: 0
bedrock: 0
oxodum: 0
lubinit: 0
darkana: 0
bio: 0
notch: 0
lava: 0