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

82 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2014-12-06 21:36:19 +01:00
class Tile
2014-12-07 14:07:13 +01:00
constructor: (position)->
@position = position
2014-12-06 14:28:58 +01:00
@layers = []
2014-12-06 22:07:28 +01:00
for restype, i in allResourceTypes()
2014-12-06 17:17:30 +01:00
@layers.push new Tilelayer(
type: restype,
depth: i,
2014-12-07 15:47:42 +01:00
amount: Math.round(Math.random()*app.game.maxTileAmount)+1
2014-12-06 17:17:30 +01:00
)
2014-12-06 14:55:33 +01:00
2014-12-06 16:35:05 +01:00
@currentLayer = 0
2014-12-07 18:17:33 +01:00
@empty = false
2014-12-06 22:07:28 +01:00
@entity = null
2014-12-06 16:35:05 +01:00
@isActive = false
2014-12-07 15:47:42 +01:00
@isBuildable = !(Math.round(Math.random()*10) == 5)
2014-12-07 19:14:40 +01:00
@isBuildable = true if @isBuildable == 20*5+10
2014-12-07 15:47:42 +01:00
@randomSeed = Math.round(Math.random()*10)
click: (button)->
2014-12-07 16:27:44 +01:00
# Some tiles are not buildable
return unless @isBuildable
2014-12-07 18:17:33 +01:00
if button == "left" and !@empty
2014-12-07 12:55:08 +01:00
if (app.game.availableSiloStorage() - app.game.usedSiloStorage()) > 0
if @layers[@currentLayer].collect()
name = app.layerIndexToName(@currentLayer)
app.game.resources[name] += 1
else
@currentLayer += 1
2014-12-07 18:17:33 +01:00
if @currentLayer == allResourceTypes().length-1
# If we reach the bottom, the entity on top dies.
@entity = null
@empty = true
2014-12-06 21:08:46 +01:00
2014-12-06 19:49:57 +01:00
tick: ->
2014-12-06 22:07:28 +01:00
@entity.tick @ if @entity
2014-12-06 19:49:57 +01:00
moveIn: ->
2014-12-06 16:35:05 +01:00
@isActive = true
moveOut: ->
2014-12-06 16:35:05 +01:00
@isActive = false
2014-12-06 17:58:03 +01:00
select: ->
@isSelected = true
deselect: ->
@isSelected = false
2014-12-07 01:08:39 +01:00
getCurrentLayer: ->
@layers[@currentLayer]
2014-12-06 12:37:57 +01:00
render: (x, y)->
2014-12-07 01:08:39 +01:00
tileLayer = @getCurrentLayer()
2014-12-06 16:35:05 +01:00
app.layer.drawRegion app.images.layers, tileLayer.sprite, x*8, y*8
2014-12-06 16:52:53 +01:00
2014-12-07 15:47:42 +01:00
unless @isBuildable
app.layer.drawRegion app.images.deadtiles, [8*@randomSeed, 0, 8, 8], x*8, y*8
return
2014-12-06 22:07:28 +01:00
if @entity
2014-12-07 00:19:22 +01:00
app.layer.drawRegion app.images.entities, @entity.sprite(), x*8, y*8
2014-12-06 17:58:03 +01:00
2014-12-07 00:28:16 +01:00
if (@entity and @entity.isMoveable()) or @isActive
2014-12-06 21:47:58 +01:00
# Draw the status indicator.
2014-12-07 00:19:22 +01:00
app.layer.drawImage app.images.progress, x*8, y*8
2014-12-06 21:47:58 +01:00
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)
2014-12-06 17:58:03 +01:00
2014-12-06 17:32:37 +01:00
if @isActive
2014-12-06 16:35:05 +01:00
app.layer.drawImage app.images.active, x*8, y*8
2014-12-06 17:32:37 +01:00
2014-12-07 01:08:39 +01:00
if @isSelected
app.layer.drawImage app.images.selected, x*8, y*8
2014-12-06 18:12:38 +01:00