Skip to content

Commit

Permalink
Pine hitting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dekart committed Dec 6, 2014
1 parent a37c47e commit f088f2c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
11 changes: 5 additions & 6 deletions app/assets/javascripts/controllers/game_controller.js.coffee
Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -114,5 +113,5 @@ window.GameController = class extends BaseController
finish: ->
@animator.deactivate()

alert('Done!')
alert('Game over!')

49 changes: 42 additions & 7 deletions app/assets/javascripts/models/lumberjack.js.coffee
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion app/assets/javascripts/models/pine.js.coffee
@@ -1,4 +1,9 @@
window.Pine = class
constructor: ->
@x = canvasSize.width / 2
@y = canvasSize.height / 2
@y = canvasSize.height / 2

@health = 20

getHit: ->
@health -= 1

0 comments on commit f088f2c

Please sign in to comment.