From 5d5b361764a706c9f4787001719fa0e67d0ae78e Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sat, 6 Dec 2014 15:57:02 +0100 Subject: [PATCH] delegate mouse events directly to the tiles --- src/entities/tile.coffee | 9 +++++++++ src/game.coffee | 2 -- src/main.coffee | 31 +++++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/entities/tile.coffee b/src/entities/tile.coffee index d9956c1..7f64ad9 100644 --- a/src/entities/tile.coffee +++ b/src/entities/tile.coffee @@ -9,6 +9,15 @@ class Tile extends BaseEntity @layers.push new Tilelayer(type: restype, depth: i) @currentLayer = 0 #getRandomInt 0, resTypes.length-1 + + click: (button)-> + console.log button + + moveIn: -> + console.log "in" + + moveOut: -> + console.log "out" tick: (delta) -> # TODO: digging deeper? diff --git a/src/game.coffee b/src/game.coffee index a2d3f68..9ce5570 100644 --- a/src/game.coffee +++ b/src/game.coffee @@ -4,12 +4,10 @@ app.game = @map[i] = new Tile render: -> - app.layer.save().scale(5, 5) for tile, i in @map y = Math.floor(i/20) x = i-(y*20) tile.render(x, y) - app.layer.restore() @hud.render() step: -> diff --git a/src/main.coffee b/src/main.coffee index 04d975f..4a76506 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -1,20 +1,35 @@ app = playground({ - width: 8*20*5, - height: 8*15*5, - scaleToFit: false, + width: 8*20, + height: 8*15, + scaleToFit: true, smoothing: false, - + create: -> @loadImages "layers" + @currentHoveredTile = new Tile ready: -> @game.start() @setState @game step: -> - + + mousedown: (event)-> + @posToTile(event.x, event.y).click(event.button) + + mousemove: (event)-> + tile = @posToTile(event.x, event.y) + if tile != @currentHoveredTile + tile.moveIn() + @currentHoveredTile.moveOut() + @currentHoveredTile = tile + render: -> - @layer.clear("#00f"); - @game.render - + @layer.clear "#00f" + @game.render() + + posToTile: (x, y)-> + pos = (Math.floor(y/8)*20) + Math.floor(x/8) + @game.map[pos] + })