This commit is contained in:
Ruben Müller 2014-12-07 00:15:22 +01:00
commit 927ae718f7
6 changed files with 28 additions and 43 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 381 B

After

Width:  |  Height:  |  Size: 510 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

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

View File

@ -19,10 +19,8 @@ app.game =
@hud.render()
step: ->
mousedown: (event)->
tile = @posToTile(event.x, event.y)
tile = posToTile(event.x, event.y)
tile.click(event.button)
@currentSelectedTile.deselect() if @currentSelectedTile
@ -30,7 +28,7 @@ app.game =
@currentSelectedTile = tile
mousemove: (event)->
tile = @posToTile(event.x, event.y)
tile = posToTile(event.x, event.y)
if tile
if tile != @currentHoveredTile
@ -51,17 +49,7 @@ app.game =
createMiner: ->
if @currentSelectedTile and @checkRessources 'lubinit', 30, true
newMiner = new Miner
@miners.push = newMiner
@currentSelectedTile.attachMiner newMiner
@currentSelectedTile.deselect() if @currentSelectedTile
@currentSelectedTile = null
posToTile: (x, y)->
pos = (Math.floor(y/8)*20) + Math.floor(x/8)
@map[pos]
@currentSelectedTile.entity = new Miner
checkRessources: (type, amount, drain = false) ->
if @resources[type] >= amount

View File

@ -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,19 @@ 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)
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 @isSelected
app.layer.drawImage app.images.selected, x*8, y*8
@ -63,10 +57,4 @@ class Tile
if @isActive
app.layer.drawImage app.images.active, x*8, y*8
# Draw the status indicator.
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)

View File

@ -1,2 +1,9 @@
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)
posToTile = (x, y)->
pos = (Math.floor(y/8)*20) + Math.floor(x/8)
app.game.map[pos]