151 lines
3.6 KiB
CoffeeScript
151 lines
3.6 KiB
CoffeeScript
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 "s" then @createSilo()
|
|
when "c" then @cheatah()
|
|
when "space"
|
|
@currentSelectedTile.deselect() if @currentSelectedTile
|
|
@currentSelectedTile = null
|
|
|
|
tick: =>
|
|
tile.tick() for tile in app.game.map
|
|
|
|
createMiner: ->
|
|
if @currentSelectedTile and !@currentSelectedTile.entity and @checkResource('stardust', 30, true)
|
|
@currentSelectedTile.entity = new Miner
|
|
|
|
createSilo: ->
|
|
if @currentSelectedTile
|
|
if @checkPosition(@currentSelectedTile)
|
|
if @checkResource('stardust', 20, true)
|
|
@currentSelectedTile.entity = new Silo
|
|
else
|
|
@speechbubble.say 'toofar'
|
|
|
|
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
|
|
|
|
resources:
|
|
stardust: 0
|
|
dirt: 0
|
|
bedrock: 0
|
|
oxodum: 0
|
|
lubinit: 0
|
|
darkana: 0
|
|
bio: 0
|
|
notch: 0
|
|
lava: 0
|