From d8cfae7940ffb6ff30ac11da6a8f3dedb6ee79b5 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sun, 7 Dec 2014 14:07:13 +0100 Subject: [PATCH] Entity constraints --- src/app.coffee | 2 -- src/entities/base.coffee | 1 + src/entities/miner.coffee | 1 + src/entities/silo.coffee | 2 ++ src/game.coffee | 47 ++++++++++++++++++++++++++++++--------- src/tiles/tile.coffee | 3 ++- src/tools.coffee | 11 +++++++-- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/app.coffee b/src/app.coffee index 24df7e4..a915aa6 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -11,8 +11,6 @@ app = playground( ready: -> @game.start() @setState @game - - step: -> render: -> @layer.clear "#00f" diff --git a/src/entities/base.coffee b/src/entities/base.coffee index cd0565d..db3dc3c 100644 --- a/src/entities/base.coffee +++ b/src/entities/base.coffee @@ -8,4 +8,5 @@ class Base false spaceProvided: 50 + isDockable: true diff --git a/src/entities/miner.coffee b/src/entities/miner.coffee index 4db52ac..82ea4e3 100644 --- a/src/entities/miner.coffee +++ b/src/entities/miner.coffee @@ -9,3 +9,4 @@ class Miner true spaceProvided: 5 + isDockable: false diff --git a/src/entities/silo.coffee b/src/entities/silo.coffee index b8db818..b60be38 100644 --- a/src/entities/silo.coffee +++ b/src/entities/silo.coffee @@ -19,3 +19,5 @@ class Silo spaceProvided: 150 + isDockable: true + diff --git a/src/game.coffee b/src/game.coffee index f0f9c24..804c6da 100644 --- a/src/game.coffee +++ b/src/game.coffee @@ -1,15 +1,13 @@ app.game = start: -> for i in [0..20*15-1] - @map[i] = new Tile + @map[i] = new Tile(i) @map[20*5+10].entity = new Base - @currentHoveredTile = new Tile + @currentHoveredTile = new Tile(-1) @currentSelectedTile = null - # Start the game tick window.setInterval @tick, 1000 - @hud.start() render: -> @@ -17,14 +15,13 @@ app.game = y = Math.floor(i/20) x = i-(y*20) tile.render(x, y) - @hud.render() mousedown: (event)-> - if @isMouseInView event.x, event.y + if @isMouseInView event.x/8, event.y/8 switch event.button when 'left' - tile = posToTile(event.x, event.y) + tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8)) tile.click(event.button) @currentSelectedTile.deselect() if @currentSelectedTile @@ -36,7 +33,7 @@ app.game = mousemove: (event)-> if @isMouseInView event.x, event.y - tile = posToTile(event.x, event.y) + tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8)) if tile if tile != @currentHoveredTile @@ -66,11 +63,11 @@ app.game = tile.tick() for tile in app.game.map createMiner: -> - if @currentSelectedTile and @checkResource 'stardust', 1, true + if @currentSelectedTile and !@currentSelectedTile.entity and @checkResource('stardust', 30, true) @currentSelectedTile.entity = new Miner createSilo: -> - if @currentSelectedTile and @checkResource 'stardust', 1, true + if @currentSelectedTile and @checkPosition(@currentSelectedTile) and @checkResource('stardust', 20, true) @currentSelectedTile.entity = new Silo checkResource: (type, amount, drain = false) -> @@ -79,6 +76,34 @@ app.game = return true false + checkPosition: (tile)-> + return false if tile.entity + + # Check all 4 directions + [x, y] = posToXY(tile.position) + + # Left + return true if app.game.map[xyToPos(x-1, y)].entity \ + and app.game.map[xyToPos(x-1, y)].entity.isDockable \ + and x > 0 + + # Right + return true if app.game.map[xyToPos(x+1, y)].entity \ + and app.game.map[xyToPos(x+1, y)].entity.isDockable \ + and x < 19 + + # Top + return true if app.game.map[xyToPos(x, y-1)].entity \ + and app.game.map[xyToPos(x, y-1)].entity.isDockable \ + and y > 0 + + # Bottom + return true if app.game.map[xyToPos(x, y+1)].entity \ + and app.game.map[xyToPos(x, y+1)].entity.isDockable \ + and y < 14 + + false + cheatah: -> for type, amount of @resources @resources[type] = 100 @@ -88,7 +113,7 @@ app.game = maxTileAmount: 15 availableSiloStorage: -> - space = 50 + space = 0 for tile in app.game.map space += tile.entity.spaceProvided if tile.entity space diff --git a/src/tiles/tile.coffee b/src/tiles/tile.coffee index a2746f9..c7a6dec 100644 --- a/src/tiles/tile.coffee +++ b/src/tiles/tile.coffee @@ -1,5 +1,6 @@ class Tile - constructor: -> + constructor: (position)-> + @position = position @layers = [] for restype, i in allResourceTypes() @layers.push new Tilelayer( diff --git a/src/tools.coffee b/src/tools.coffee index 91de026..fdaa3f5 100644 --- a/src/tools.coffee +++ b/src/tools.coffee @@ -5,5 +5,12 @@ 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] + app.game.map[xyToPos(x, y)] + +posToXY = (pos)-> + y = Math.floor(pos/20) + x = pos-(y*20) + [x, y] + +xyToPos = (x, y)-> + y*20 + x