Skip to content

Commit

Permalink
Pass a point to Display#set and move drawing out of other objects
Browse files Browse the repository at this point in the history
  • Loading branch information
reinh committed Mar 24, 2013
1 parent 108761e commit 4ab30f4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/coffee/display.coffee
Expand Up @@ -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?
Expand All @@ -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 = $('<textarea></textarea>')
Expand Down
13 changes: 9 additions & 4 deletions src/coffee/engine.coffee
Expand Up @@ -13,23 +13,28 @@ 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()
new Turn(@, @scheduler.next()).run()

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

2 changes: 1 addition & 1 deletion src/coffee/map.coffee
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/coffee/player.coffee
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/coffee/turn.coffee
Expand Up @@ -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()
9 changes: 7 additions & 2 deletions 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

0 comments on commit 4ab30f4

Please sign in to comment.