ld31-space-diggers/src/tiles/tile.coffee

82 lines
2.2 KiB
CoffeeScript

class Tile
constructor: (position)->
@position = position
@layers = []
for restype, i in allResourceTypes()
@layers.push new Tilelayer(
type: restype,
depth: i,
amount: Math.round(Math.random()*app.game.maxTileAmount)+1
)
@currentLayer = 0
@empty = false
@entity = null
@isActive = false
@isBuildable = !(Math.round(Math.random()*10) == 5)
@isBuildable = true if @isBuildable == 20*5+10
@randomSeed = Math.round(Math.random()*10)
click: (button)->
# Some tiles are not buildable
return unless @isBuildable
if button == "left" and !@empty
if (app.game.availableSiloStorage() - app.game.usedSiloStorage()) > 0
if @layers[@currentLayer].collect()
name = app.layerIndexToName(@currentLayer)
app.game.resources[name] += 1
else
@currentLayer += 1
if @currentLayer == allResourceTypes().length-1
# If we reach the bottom, the entity on top dies.
@entity = null
@empty = true
tick: ->
@entity.tick @ if @entity
moveIn: ->
@isActive = true
moveOut: ->
@isActive = false
select: ->
@isSelected = true
deselect: ->
@isSelected = false
getCurrentLayer: ->
@layers[@currentLayer]
render: (x, y)->
tileLayer = @getCurrentLayer()
app.layer.drawRegion app.images.layers, tileLayer.sprite, x*8, y*8
unless @isBuildable
app.layer.drawRegion app.images.deadtiles, [8*@randomSeed, 0, 8, 8], x*8, y*8
return
if @entity
app.layer.drawRegion app.images.entities, @entity.sprite(), x*8, y*8
if (@entity and @entity.isMoveable()) or @isActive
# Draw the status indicator.
app.layer.drawImage app.images.progress, x*8, y*8
numPercent = Math.floor((tileLayer.amount*6)/app.game.maxTileAmount)
for i in [0..numPercent]
color = ["#f00", "#a00", "#f60", "#aa0", "#0a0", "#0a0"][numPercent]
color = ["#f00", "#f00", "#f80", "#ff0", "#0f0", "#0f0"][numPercent] if i == numPercent
app.layer.setPixel(color, x*8+1+i, y*8+6)
if @isActive
app.layer.drawImage app.images.active, x*8, y*8
if @isSelected
app.layer.drawImage app.images.selected, x*8, y*8