Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcneilly committed Sep 27, 2015
1 parent 8da78b5 commit abf4a5e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 89 deletions.
154 changes: 86 additions & 68 deletions scripts/coffee/TimeLord.coffee
@@ -1,50 +1,72 @@
StateMachine = require './state-machine'
module.exports = class Timelord
constructor: (game, cursors, opts = {}) ->
@game = game
@cursors = cursors

@velocity = opts.velocity ? 150
@spawnPosition = opts.spawnPosition ? {x: 32, y: 450}
@spawnPosition = opts.spawnPosition ? {x: 32, y: 450}
@movementHistory = []
@currentMove = 0
@sprite = null
@id = null
@currentState = null

@create()

create: ->
@statemachine = new StateMachine(
[
{
name: 'offCanvas',
behaviour: @offCanvas
},
{
name: 'playerControlled',
behaviour: @playerContolledState,
pre: @triggerPastControlledState
},
{
name: 'computerControlled'
behaviour: @computerControlledState
pre: @triggerComputerControlledState
}
]
)

# @setStateTo('futureControlled')

update: ->
@statemachine.runStates()

# manageState: ->
# switch @currentState
# when 'playerControlled' then @playerContolledState()
# when 'pastControlled' then @pastControlledState()
# when 'futureControlled' then @futureControlledState()
# when 'hidden' then @hiddenState()

# setStateTo: (stateName) ->
# switch stateName
# when 'playerControlled' then @triggerPlayerContolledState()
# when 'pastControlled' then @triggerPastControlledState()
# when 'futureControlled' then @triggerFutureControlledState()
# when 'hidden' then @triggerHiddenState()

offCanvas: ->
console.log('offCanvas')

triggerPlayerContolledState: =>
debugger
@sprite = @game.add.sprite @spawnPosition.x, @spawnPosition.y, 'dude'
@sprite.alpha = 1
@sprite.body.gravity.y = null
@sprite.body.collideWorldBounds = true

@sprite.animations.add 'left', [0..3], 10, true
@sprite.animations.add 'right', [5..8], 10, true
@setStateTo('futureControlled')

update: ->
@manageState()

manageState: ->
switch @currentState
when 'playerControlled' then @playerContolledState()
when 'pastControlled' then @pastControlledState()
when 'futureControlled' then @futureControlledState()
when 'hidden' then @hiddenState()

setStateTo: (stateName) ->
switch stateName
when 'playerControlled' then @triggerPlayerContolledState()
when 'pastControlled' then @triggerPastControlledState()
when 'futureControlled' then @triggerFutureControlledState()
when 'hidden' then @triggerHiddenState()

triggerPlayerContolledState: ->
console.log('triggerPlayerContolledState:')
@currentState = 'playerControlled'
@sprite.alpha = 1

playerContolledState: ->
playerContolledState: =>
@sprite.body.velocity.x = 0
@sprite.body.velocity.y = 0

Expand All @@ -56,7 +78,7 @@ module.exports = class Timelord
if @cursors.left.isDown
@sprite.body.velocity.x = -@velocity
@sprite.animations.play 'left'
timelordPos['name'] = 'leftDown'
timelordPos['name'] = 'leftDown'
else if @cursors.right.isDown
@sprite.body.velocity.x = @velocity
@sprite.animations.play 'right'
Expand All @@ -74,12 +96,11 @@ module.exports = class Timelord

@movementHistory.push(timelordPos)

triggerPastControlledState: ->
console.log('triggerPastControlledState:')
triggerComputerControlledState: =>
@currentState = 'pastControlled'
@sprite.alpha = 0.5

pastControlledState: ->
computerControlledState: =>
movement = @movementHistory[@currentMove]
@sprite.alpha = 0.5
if movement is undefined
Expand All @@ -89,44 +110,41 @@ module.exports = class Timelord
@sprite.body.velocity.x = 0
@sprite.body.velocity.y = 0

if(movement.name == 'leftDown')
@sprite.body.velocity.x = -150
@sprite.animations.play 'left'
else if (movement.name == 'rightDown')
@sprite.body.velocity.x = 150
@sprite.animations.play 'right'
else if (movement.name == 'upDown')
@sprite.body.velocity.y = -150
else if (movement.name == 'downDown')
@sprite.body.velocity.y = 150
else if (movement.name == 'up')
@sprite.body.velocity.y = -350
else
@sprite.animations.stop()
@sprite.frame = 4

if @sprite.x != movement.x
@sprite.x = movement.x

if @sprite.y != movement.y
@sprite.y = movement.y
# if(movement.name == 'leftDown')
# @sprite.body.velocity.x = -150
# @sprite.animations.play 'left'
# else if (movement.name == 'rightDown')
# @sprite.body.velocity.x = 150
# @sprite.animations.play 'right'
# else if (movement.name == 'upDown')
# @sprite.body.velocity.y = -150
# else if (movement.name == 'downDown')
# @sprite.body.velocity.y = 150
# else if (movement.name == 'up')
# @sprite.body.velocity.y = -350
# else
# @sprite.animations.stop()
# @sprite.frame = 4

# if @sprite.x != movement.x
@sprite.x = movement.x

# if @sprite.y != movement.y
@sprite.y = movement.y

@currentMove = @currentMove + 1

triggerFutureControlledState: ->
console.log('triggerFutureControlledState:')
@currentState = 'futureControlled'
@sprite.alpha = 0

futureControlledState: ->

triggerHiddenState: ->
@sprite.body.velocity.x = 0
@sprite.body.velocity.y = 0
@sprite.animations.stop()
@sprite.alpha = 0

hiddenState: ->



# triggerFutureControlledState: ->
# console.log('triggerFutureControlledState:')
# @currentState = 'futureControlled'
# @sprite.alpha = 0

# futureControlledState: ->
#
# triggerHiddenState: ->
# @sprite.body.velocity.x = 0
# @sprite.body.velocity.y = 0
# @sprite.animations.stop()
# @sprite.alpha = 0
#
# hiddenState: ->
2 changes: 1 addition & 1 deletion scripts/coffee/level-1.coffee
Expand Up @@ -3,4 +3,4 @@ TimeFixerBaseLevel = require './time-fixer-base-level'
module.exports = class Level1 extends TimeFixerBaseLevel
constructor: (game) ->
super game
@numTimelords = 2
@numTimelords = 5
19 changes: 8 additions & 11 deletions scripts/coffee/state-machine.coffee
@@ -1,5 +1,5 @@
module.exports = class StateMachine

constructor: (states) ->
@states = states
@activeStates = []
Expand All @@ -19,12 +19,18 @@ module.exports = class StateMachine
getStates: ->
@activeStates

runStates: ->
@activeStates.forEach (stateName) =>
triggeredState = _.find @states, (state) =>
return state.name == stateName
triggeredState.behaviour()

isActive: (stateName) ->
found = _.find @activeStates, (state) ->
return state == stateName
if found
return true
else
else
return false

triggerPreState: (stateName) ->
Expand All @@ -50,12 +56,3 @@ module.exports = class StateMachine
return state.name == stateName
if _.isFunction(triggeredState.postRemove)
triggeredState.postRemove()









32 changes: 23 additions & 9 deletions scripts/coffee/time-fixer-base-level.coffee
@@ -1,4 +1,5 @@
Timelord = require './timelord'
StateMachine = require './state-machine'

module.exports = class TimeFixerBaseLevel
constructor: (game) ->
Expand All @@ -17,7 +18,19 @@ module.exports = class TimeFixerBaseLevel
@game.load.image 'star', '/assets/images/star.png'
@game.load.spritesheet 'dude', '/assets/images/dude.png', 32, 48


create: =>
@statemachine = new StateMachine(
[
{
name: 'prespawn'

},
{
name: 'spawn'
}
]
)
@createWorld()
@createStars()
@cursors = @game.input.keyboard.createCursorKeys()
Expand All @@ -34,8 +47,8 @@ module.exports = class TimeFixerBaseLevel

manageState: ->
if @currentState is 'prespawn'
if @timer.seconds() > @respawnPause
@triggerSpawnState()
if @timer.seconds() > @respawnPause
@triggerSpawnState()
else if @currentState is 'spawn'
if @timer.seconds() > @respawnTime
@triggerPrespawnState()
Expand All @@ -47,23 +60,24 @@ module.exports = class TimeFixerBaseLevel

if @playerControlledTimelord is undefined
return

if @playerControlledTimelordNum is null
@playerControlledTimelordNum = 0
@playerControlledTimelord = @timelords[@playerControlledTimelordNum]
else
@playerControlledTimelord.setStateTo('pastControlled')
@playerControlledTimelord.statemachine.removeState('playerControlled')
@playerControlledTimelord.statemachine.setState('computerControlled')
@playerControlledTimelordNum = @playerControlledTimelordNum + 1
@playerControlledTimelord = @timelords[@playerControlledTimelordNum]

if @playerControlledTimelord is undefined
return

@playerControlledTimelord.setStateTo('playerControlled')
@playerControlledTimelord.statemachine.setState('playerControlled')


spawnState: ->
for timelord in @timelords
for timelord in @timelords
@game.physics.collide timelord.sprite, @platforms
timelord.update()

Expand All @@ -74,7 +88,7 @@ module.exports = class TimeFixerBaseLevel
for timelord in @timelords
timelord.currentMove = 0


createTimelords: (num) ->
@timelords = []
for i in [0..num-1]
Expand Down Expand Up @@ -102,7 +116,7 @@ module.exports = class TimeFixerBaseLevel

createStars: =>
@game.add.sprite 0, 0, 'star'
@stars = @game.add.group()
@stars = @game.add.group()
for i in [0..12]
star = @stars.create i * 70, 0, 'star'
star.body.gravity.y = 6
Expand All @@ -111,4 +125,4 @@ module.exports = class TimeFixerBaseLevel
clearTimer: ->
@timer.stop()
@timer = new Phaser.Timer(@game)
@timer.start()
@timer.start()

0 comments on commit abf4a5e

Please sign in to comment.