Entity constraints
This commit is contained in:
parent
dfbcb1598c
commit
d8cfae7940
7 changed files with 51 additions and 16 deletions
|
@ -11,8 +11,6 @@ app = playground(
|
|||
ready: ->
|
||||
@game.start()
|
||||
@setState @game
|
||||
|
||||
step: ->
|
||||
|
||||
render: ->
|
||||
@layer.clear "#00f"
|
||||
|
|
|
@ -8,4 +8,5 @@ class Base
|
|||
false
|
||||
|
||||
spaceProvided: 50
|
||||
isDockable: true
|
||||
|
||||
|
|
|
@ -9,3 +9,4 @@ class Miner
|
|||
true
|
||||
|
||||
spaceProvided: 5
|
||||
isDockable: false
|
||||
|
|
|
@ -19,3 +19,5 @@ class Silo
|
|||
|
||||
spaceProvided: 150
|
||||
|
||||
isDockable: true
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
class Tile
|
||||
constructor: ->
|
||||
constructor: (position)->
|
||||
@position = position
|
||||
@layers = []
|
||||
for restype, i in allResourceTypes()
|
||||
@layers.push new Tilelayer(
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue