Refactor the miners to entity
This commit is contained in:
parent
bd46e22b28
commit
815fa7d2d1
4 changed files with 15 additions and 27 deletions
|
@ -1,3 +1,5 @@
|
|||
class Miner
|
||||
tick: (tile) ->
|
||||
tile.click "left"
|
||||
|
||||
sprite: [16, 0, 8, 8]
|
||||
|
|
|
@ -49,11 +49,7 @@ app.game =
|
|||
|
||||
createMiner: ->
|
||||
if @currentSelectedTile and @checkRessources 'lubinit', 30, true
|
||||
newMiner = new Miner
|
||||
|
||||
@miners.push = newMiner
|
||||
@currentSelectedTile.attachMiner newMiner
|
||||
|
||||
@currentSelectedTile.entity = new Miner
|
||||
@currentSelectedTile.deselect() if @currentSelectedTile
|
||||
@currentSelectedTile = null
|
||||
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
class Tile
|
||||
constructor: ->
|
||||
resTypes = (k for own k of app.game.resources)
|
||||
|
||||
@layers = []
|
||||
for restype, i in resTypes
|
||||
for restype, i in allResourceTypes()
|
||||
@layers.push new Tilelayer(
|
||||
type: restype,
|
||||
depth: i,
|
||||
|
@ -11,25 +9,21 @@ class Tile
|
|||
)
|
||||
|
||||
@currentLayer = 0
|
||||
@entity = null
|
||||
@isActive = false
|
||||
|
||||
died: false
|
||||
|
||||
click: (button)->
|
||||
if button == "left" and !@died
|
||||
if button == "left"
|
||||
if @layers[@currentLayer].collect()
|
||||
name = app.layerIndexToName(@currentLayer)
|
||||
app.game.resources[name] += 1
|
||||
else
|
||||
@currentLayer += 1
|
||||
|
||||
if @currentLayer == Object.keys(app.game.resources).length-1
|
||||
@died = true
|
||||
|
||||
@currentMiner = null if @currentMiner
|
||||
# If we reach the bottom, the entity on top dies.
|
||||
@entity = null if @currentLayer == allResourceTypes().length-1
|
||||
|
||||
tick: ->
|
||||
@currentMiner.tick @ if @currentMiner
|
||||
@entity.tick @ if @entity
|
||||
|
||||
moveIn: ->
|
||||
@isActive = true
|
||||
|
@ -43,19 +37,12 @@ class Tile
|
|||
deselect: ->
|
||||
@isSelected = false
|
||||
|
||||
attachMiner: (miner) ->
|
||||
if !@currentMiner
|
||||
@currentMiner = miner
|
||||
|
||||
detachMiner: ->
|
||||
@currentMiner = null
|
||||
|
||||
render: (x, y)->
|
||||
tileLayer = @layers[@currentLayer]
|
||||
app.layer.drawRegion app.images.layers, tileLayer.sprite, x*8, y*8
|
||||
|
||||
if @currentMiner
|
||||
app.layer.drawRegion app.images.entities, [16, 0, 8, 8], x*8, y*8
|
||||
if @entity
|
||||
app.layer.drawRegion app.images.entities, @entity.sprite, x*8, y*8
|
||||
|
||||
# Draw the status indicator.
|
||||
numPercent = Math.floor((tileLayer.amount*6)/app.game.maxTileAmount)
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
getRandomInt = (min, max) ->
|
||||
Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
|
||||
allResourceTypes = ->
|
||||
(k for own k of app.game.resources)
|
||||
|
|
Loading…
Reference in a new issue