diff --git a/app/assets/javascripts/controllers/game_controller.js.coffee b/app/assets/javascripts/controllers/game_controller.js.coffee index f666e76..e8fccfa 100644 --- a/app/assets/javascripts/controllers/game_controller.js.coffee +++ b/app/assets/javascripts/controllers/game_controller.js.coffee @@ -18,16 +18,12 @@ window.GameController = class extends BaseController for i in [0 .. 10] jack = new Lumberjack(Lumberjack.randomSpawnPosition()...) - jack.aimTo(canvasSize.width / 2, canvasSize.height / 2) + jack.aimTo(@pine) @lumberjacks.push(jack) - jack = new Lumberjack(10, 10) - jack.aimTo(canvasSize.width / 2, canvasSize.height / 2) - @lumberjacks.push(jack) - show: -> @.setupEventListeners() @@ -55,6 +51,9 @@ window.GameController = class extends BaseController for jack in @lumberjacks jack.updateState() + if @pine.health <= 0 + @.finish() + updateMousePosition: (event)-> touchpoint = if event.originalEvent.touches? then event.originalEvent.touches[0] else event @@ -114,5 +113,5 @@ window.GameController = class extends BaseController finish: -> @animator.deactivate() - alert('Done!') + alert('Game over!') diff --git a/app/assets/javascripts/models/lumberjack.js.coffee b/app/assets/javascripts/models/lumberjack.js.coffee index c90820b..0b7f7e0 100644 --- a/app/assets/javascripts/models/lumberjack.js.coffee +++ b/app/assets/javascripts/models/lumberjack.js.coffee @@ -11,28 +11,63 @@ window.Lumberjack = class [canvasSize.width + 50, _.random(0, canvasSize.height)] pixelsPerSecond: 50 + hitEvery: 1.5 constructor: (@x, @y)-> + @speed = {x: 0, y: 0} + @target = {x: 0, y: 0} + + @.startMoving() + + startMoving: -> + @state = 'moving' @last_position_update_at = Date.now() - @speed = {x: 0, y: 0} + startHitting: -> + @state = 'hitting' + @last_hit_at = Date.now() updateState: -> current_time = Date.now() - @.updatePosition(current_time) + @.updatePosition(current_time) if @state == 'moving' + @.updateHitting(current_time) if @state == 'hitting' updatePosition: (current_time)-> delta = (current_time - @last_position_update_at) / 1000 - @x += @speed.x * @.pixelsPerSecond * delta - @y += @speed.y * @.pixelsPerSecond * delta + dX = Math.abs(@target.x - @x) + dY = Math.abs(@target.y - @y) + + if dX + dY < 5 + @x = @target.x + @y = @target.y + + @.startHitting() + else + @x += @speed.x * @.pixelsPerSecond * delta + @y += @speed.y * @.pixelsPerSecond * delta @last_position_update_at = current_time - aimTo: (targetX, targetY)-> - dX = targetX - @x - dY = targetY - @y + updateHitting: (current_time)-> + delta = (current_time - @last_hit_at) / 1000 + + if delta >= @.hitEvery + @pine.getHit() + + @last_hit_at = current_time + + aimTo: (@pine)-> + if @x < @pine.x + @target.x = @pine.x - 40 + else + @target.x = @pine.x + 40 + + @target.y = @pine.y - _.random(-5, 50) + + dX = @target.x - @x + dY = @target.y - @y if Math.abs(dX) > Math.abs(dY) @speed.x = Math.sign(dX) diff --git a/app/assets/javascripts/models/pine.js.coffee b/app/assets/javascripts/models/pine.js.coffee index 6d43659..a2a6e30 100644 --- a/app/assets/javascripts/models/pine.js.coffee +++ b/app/assets/javascripts/models/pine.js.coffee @@ -1,4 +1,9 @@ window.Pine = class constructor: -> @x = canvasSize.width / 2 - @y = canvasSize.height / 2 \ No newline at end of file + @y = canvasSize.height / 2 + + @health = 20 + + getHit: -> + @health -= 1