diff --git a/lib/scene.coffee b/lib/scene.coffee index 316d826..6f2cab8 100644 --- a/lib/scene.coffee +++ b/lib/scene.coffee @@ -2,6 +2,13 @@ module.exports = class Scene constructor: (@screen, @context) -> @sprites = [] + addSprite: (sprite) -> + @sprites.push sprite + + redraw: -> + @screen.clear() + sprite.draw @context for sprite in @sprites + draw: (image, x, y) -> @context.drawImage image, x, y diff --git a/lib/sprite.coffee b/lib/sprite.coffee index 710646d..659a7f3 100644 --- a/lib/sprite.coffee +++ b/lib/sprite.coffee @@ -8,6 +8,14 @@ module.exports = class Sprite else @initSprite(image) + setPosition: (x,y) -> + @x = x + @y = y + + move: (x,y) -> + @x = @x + x + @y = @y + y + initSprite: (image) -> @img = document.createElement 'canvas' @img.width = @width @@ -16,9 +24,9 @@ module.exports = class Sprite ctx.drawImage image, @offset_x, @offset_y, @width, @height, 0, 0, @width, @height @drawCallback?() - draw: (scene, x, y) -> + draw: (context) -> if @image_complete - scene.draw @img, x, y + context.drawImage @img, @x, @y else @drawCallback = => - scene.draw @img, x, y + context.drawImage @img, @x, @y diff --git a/test/test.coffee b/test/test.coffee index 9c046db..42a878c 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -8,9 +8,12 @@ img = new Image img.src = '/images/simple_sprite.png' window.sprite = new Squash.Sprite img, 0, 0, 20, 20 -@x = 90 +@x = 0 @y = 90 -sprite.draw scene, @x, @y + +sprite.setPosition(@x, @y) +scene.addSprite sprite +scene.redraw() window.timer = new Squash.Timer @@ -21,10 +24,9 @@ timer.registerCallback (timeSinceLastTick) => console.log "FPS: #{@ticks}" @ms = 0 @ticks = 0 - scene.clear() - @x = @x + 10 - sprite.draw scene, @x, @y + sprite.move 10, 0 else @ms += timeSinceLastTick @ticks += 1 + scene.redraw() timer.start()