Entity constraints

This commit is contained in:
Aaron Mueller 2014-12-07 14:07:13 +01:00
parent dfbcb1598c
commit d8cfae7940
7 changed files with 51 additions and 16 deletions

View file

@ -12,8 +12,6 @@ app = playground(
@game.start() @game.start()
@setState @game @setState @game
step: ->
render: -> render: ->
@layer.clear "#00f" @layer.clear "#00f"
@game.render() @game.render()

View file

@ -8,4 +8,5 @@ class Base
false false
spaceProvided: 50 spaceProvided: 50
isDockable: true

View file

@ -9,3 +9,4 @@ class Miner
true true
spaceProvided: 5 spaceProvided: 5
isDockable: false

View file

@ -19,3 +19,5 @@ class Silo
spaceProvided: 150 spaceProvided: 150
isDockable: true

View file

@ -1,15 +1,13 @@
app.game = app.game =
start: -> start: ->
for i in [0..20*15-1] for i in [0..20*15-1]
@map[i] = new Tile @map[i] = new Tile(i)
@map[20*5+10].entity = new Base @map[20*5+10].entity = new Base
@currentHoveredTile = new Tile @currentHoveredTile = new Tile(-1)
@currentSelectedTile = null @currentSelectedTile = null
# Start the game tick
window.setInterval @tick, 1000 window.setInterval @tick, 1000
@hud.start() @hud.start()
render: -> render: ->
@ -17,14 +15,13 @@ app.game =
y = Math.floor(i/20) y = Math.floor(i/20)
x = i-(y*20) x = i-(y*20)
tile.render(x, y) tile.render(x, y)
@hud.render() @hud.render()
mousedown: (event)-> mousedown: (event)->
if @isMouseInView event.x, event.y if @isMouseInView event.x/8, event.y/8
switch event.button switch event.button
when 'left' when 'left'
tile = posToTile(event.x, event.y) tile = posToTile(Math.floor(event.x/8), Math.floor(event.y/8))
tile.click(event.button) tile.click(event.button)
@currentSelectedTile.deselect() if @currentSelectedTile @currentSelectedTile.deselect() if @currentSelectedTile
@ -36,7 +33,7 @@ app.game =
mousemove: (event)-> mousemove: (event)->
if @isMouseInView event.x, event.y 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
if tile != @currentHoveredTile if tile != @currentHoveredTile
@ -66,11 +63,11 @@ app.game =
tile.tick() for tile in app.game.map tile.tick() for tile in app.game.map
createMiner: -> createMiner: ->
if @currentSelectedTile and @checkResource 'stardust', 1, true if @currentSelectedTile and !@currentSelectedTile.entity and @checkResource('stardust', 30, true)
@currentSelectedTile.entity = new Miner @currentSelectedTile.entity = new Miner
createSilo: -> createSilo: ->
if @currentSelectedTile and @checkResource 'stardust', 1, true if @currentSelectedTile and @checkPosition(@currentSelectedTile) and @checkResource('stardust', 20, true)
@currentSelectedTile.entity = new Silo @currentSelectedTile.entity = new Silo
checkResource: (type, amount, drain = false) -> checkResource: (type, amount, drain = false) ->
@ -79,6 +76,34 @@ app.game =
return true return true
false 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: -> cheatah: ->
for type, amount of @resources for type, amount of @resources
@resources[type] = 100 @resources[type] = 100
@ -88,7 +113,7 @@ app.game =
maxTileAmount: 15 maxTileAmount: 15
availableSiloStorage: -> availableSiloStorage: ->
space = 50 space = 0
for tile in app.game.map for tile in app.game.map
space += tile.entity.spaceProvided if tile.entity space += tile.entity.spaceProvided if tile.entity
space space

View file

@ -1,5 +1,6 @@
class Tile class Tile
constructor: -> constructor: (position)->
@position = position
@layers = [] @layers = []
for restype, i in allResourceTypes() for restype, i in allResourceTypes()
@layers.push new Tilelayer( @layers.push new Tilelayer(

View file

@ -5,5 +5,12 @@ allResourceTypes = ->
(k for own k of app.game.resources) (k for own k of app.game.resources)
posToTile = (x, y)-> posToTile = (x, y)->
pos = (Math.floor(y/8)*20) + Math.floor(x/8) app.game.map[xyToPos(x, y)]
app.game.map[pos]
posToXY = (pos)->
y = Math.floor(pos/20)
x = pos-(y*20)
[x, y]
xyToPos = (x, y)->
y*20 + x