app.game = start: -> for i in [0..20*15-1] @map[i] = new Tile(i) @map[20*5+10].entity = new Base @mouseX = 0 @mouseY = 0 @currentHoveredTile = new Tile(-1) @currentSelectedTile = null window.setInterval @tick, 1000 @hud.start() @speechbubble.start() render: -> 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' 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 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