Refactor the miners to entity

This commit is contained in:
Aaron Mueller 2014-12-06 22:07:28 +01:00
parent bd46e22b28
commit 815fa7d2d1
4 changed files with 15 additions and 27 deletions

View file

@ -1,3 +1,5 @@
class Miner class Miner
tick: (tile) -> tick: (tile) ->
tile.click "left" tile.click "left"
sprite: [16, 0, 8, 8]

View file

@ -49,11 +49,7 @@ app.game =
createMiner: -> createMiner: ->
if @currentSelectedTile and @checkRessources 'lubinit', 30, true if @currentSelectedTile and @checkRessources 'lubinit', 30, true
newMiner = new Miner @currentSelectedTile.entity = new Miner
@miners.push = newMiner
@currentSelectedTile.attachMiner newMiner
@currentSelectedTile.deselect() if @currentSelectedTile @currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null @currentSelectedTile = null

View file

@ -1,9 +1,7 @@
class Tile class Tile
constructor: -> constructor: ->
resTypes = (k for own k of app.game.resources)
@layers = [] @layers = []
for restype, i in resTypes for restype, i in allResourceTypes()
@layers.push new Tilelayer( @layers.push new Tilelayer(
type: restype, type: restype,
depth: i, depth: i,
@ -11,25 +9,21 @@ class Tile
) )
@currentLayer = 0 @currentLayer = 0
@entity = null
@isActive = false @isActive = false
died: false
click: (button)-> click: (button)->
if button == "left" and !@died if button == "left"
if @layers[@currentLayer].collect() if @layers[@currentLayer].collect()
name = app.layerIndexToName(@currentLayer) name = app.layerIndexToName(@currentLayer)
app.game.resources[name] += 1 app.game.resources[name] += 1
else else
@currentLayer += 1 @currentLayer += 1
# If we reach the bottom, the entity on top dies.
if @currentLayer == Object.keys(app.game.resources).length-1 @entity = null if @currentLayer == allResourceTypes().length-1
@died = true
@currentMiner = null if @currentMiner
tick: -> tick: ->
@currentMiner.tick @ if @currentMiner @entity.tick @ if @entity
moveIn: -> moveIn: ->
@isActive = true @isActive = true
@ -43,19 +37,12 @@ class Tile
deselect: -> deselect: ->
@isSelected = false @isSelected = false
attachMiner: (miner) ->
if !@currentMiner
@currentMiner = miner
detachMiner: ->
@currentMiner = null
render: (x, y)-> render: (x, y)->
tileLayer = @layers[@currentLayer] tileLayer = @layers[@currentLayer]
app.layer.drawRegion app.images.layers, tileLayer.sprite, x*8, y*8 app.layer.drawRegion app.images.layers, tileLayer.sprite, x*8, y*8
if @currentMiner if @entity
app.layer.drawRegion app.images.entities, [16, 0, 8, 8], x*8, y*8 app.layer.drawRegion app.images.entities, @entity.sprite, x*8, y*8
# Draw the status indicator. # Draw the status indicator.
numPercent = Math.floor((tileLayer.amount*6)/app.game.maxTileAmount) numPercent = Math.floor((tileLayer.amount*6)/app.game.maxTileAmount)

View file

@ -1,2 +1,5 @@
getRandomInt = (min, max) -> getRandomInt = (min, max) ->
Math.floor(Math.random() * (max - min + 1)) + min; Math.floor(Math.random() * (max - min + 1)) + min;
allResourceTypes = ->
(k for own k of app.game.resources)