From 00cfb0cc841d84a65fa802fdfaad665b123ca658 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sat, 6 Dec 2014 17:17:30 +0100 Subject: [PATCH] resource collection --- src/entities/tile.coffee | 13 ++++++++++--- src/entities/tilelayer.coffee | 6 +++++- src/main.coffee | 9 ++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/entities/tile.coffee b/src/entities/tile.coffee index cba6637..28c53db 100644 --- a/src/entities/tile.coffee +++ b/src/entities/tile.coffee @@ -6,14 +6,21 @@ class Tile extends BaseEntity @layers = [] for restype, i in resTypes - @layers.push new Tilelayer(type: restype, depth: i) + @layers.push new Tilelayer( + type: restype, + depth: i, + amount: Math.random(5)+10 + ) @currentLayer = 0 @isActive = false click: (button)-> - # console.log button - @isSelected = true + @isSelected = true + if button == "left" and @layers[@currentLayer].collect() + name = app.layerIndexToName(@currentLayer) + console.log(name) + app.game.resources[name] += 1 moveIn: -> @isActive = true diff --git a/src/entities/tilelayer.coffee b/src/entities/tilelayer.coffee index 5d951d2..a22da6c 100644 --- a/src/entities/tilelayer.coffee +++ b/src/entities/tilelayer.coffee @@ -1,5 +1,9 @@ class Tilelayer extends BaseEntity - constructor: (options) -> + constructor: (options)-> {@type, @depth, @amount} = options @randomFactor = getRandomInt 0, 4 @sprite = [@randomFactor*8, @depth*8, 8, 8] + + collect: -> + return false if @amount == 0 + @amount -= 1 diff --git a/src/main.coffee b/src/main.coffee index 68890a4..3260965 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -1,4 +1,4 @@ -app = playground({ +app = playground( width: 8*20*5, height: 8*15*5, scaleToFit: true, @@ -31,5 +31,8 @@ app = playground({ posToTile: (x, y)-> pos = (Math.floor(y/8/5)*20) + Math.floor(x/8/5) @game.map[pos] - - }) + + layerIndexToName: (index)-> + resTypes = (k for own k of @game.resources) + resTypes[index] +)