diff --git a/src/coffee/display.coffee b/src/coffee/display.coffee index 8fb69b6..469ebc6 100644 --- a/src/coffee/display.coffee +++ b/src/coffee/display.coffee @@ -18,7 +18,7 @@ class Display _setupEvents: -> Backbone.on 'render', => @render() - Backbone.on 'set', (x, y, char) => @set(x, y, char) + Backbone.on 'set', (point, char) => @set(point, char) render: -> return unless @dirty? @@ -27,9 +27,7 @@ class Display return true toString: -> ((cell or " " for cell in row).join("") for row in @content).join("\n") - - set: (x,y, val) -> - @content[y][x] = val + set: (p, val) -> @content[p.y][p.x] = val makeElement: -> el = $('') diff --git a/src/coffee/engine.coffee b/src/coffee/engine.coffee index b06e95c..e28a526 100644 --- a/src/coffee/engine.coffee +++ b/src/coffee/engine.coffee @@ -13,12 +13,15 @@ class @Engine Backbone.on 'move', (args...) => @move args... - @player = @world.player + @player = new Player(0,0) @display = new Display(rows: height, columns: width) actors = [@player, new Random(10,10, @)] @scheduler = new Scheduler(actors) - actor.draw() for actor in actors + @world.paint @display + + for actor in actors + @display.set actor, actor.char run: => @display.render() @@ -26,10 +29,12 @@ class @Engine move: (actor, dx, dy) => [x,y] = [actor.x+dx, actor.y+dy] + oldPoint = {x: actor.x, y: actor.y} + newPoint = {x:x, y:y} unless @world.isBlocked x, y - @display.set x, y, actor.char - @world.redraw actor.x, actor.y + @display.set oldPoint, @world.charAt actor + @display.set newPoint, actor.char actor.x = x actor.y = y diff --git a/src/coffee/map.coffee b/src/coffee/map.coffee index 182f8f1..64c6e6e 100644 --- a/src/coffee/map.coffee +++ b/src/coffee/map.coffee @@ -9,6 +9,6 @@ class Map isBlocked: (x, y) -> [x, y] not of @ - redraw: (x, y) -> Backbone.trigger 'set', x, y, @[[x, y]] + charAt: (point) -> @[[point.x, point.y]] or '.' @Map = Map diff --git a/src/coffee/player.coffee b/src/coffee/player.coffee index 4ecfd11..a0834d4 100644 --- a/src/coffee/player.coffee +++ b/src/coffee/player.coffee @@ -4,7 +4,7 @@ class @Actor damage: 3 hp: 10 dead: false - draw: -> Backbone.trigger 'set', @x, @y, @char + draw: -> Backbone.trigger 'set', {x:@x, y:@y}, @char attack: (actor) -> actor.hit @damage console.log "Attacking", actor.char, "for", @damage, "damage" diff --git a/src/coffee/turn.coffee b/src/coffee/turn.coffee index 18b210f..b380531 100644 --- a/src/coffee/turn.coffee +++ b/src/coffee/turn.coffee @@ -12,5 +12,5 @@ class @Turn run: => @actor.act @ retry: => new Turn(@engine, @actor).run() finish: => - @actor.draw() + @engine.display.set @actor, @actor.char @engine.run() diff --git a/src/coffee/world.coffee b/src/coffee/world.coffee index 0ba065c..f217ad0 100644 --- a/src/coffee/world.coffee +++ b/src/coffee/world.coffee @@ -1,9 +1,14 @@ class World constructor: (@dimensions) -> @map = new Map(@dimensions) - @player = new Player(0,0) - redraw: (x, y) -> @map.redraw x, y + paint: (display) -> + for row in [0...@dimensions.height] + for col in [0...@dimensions.width] + point = {x:col, y:row} + display.set point, @charAt point + + charAt: (point) -> @map.charAt point isBlocked: (x, y) -> @map.isBlocked x, y @World = World