action
This commit is contained in:
parent
d8286b39e0
commit
ef5e1c0355
|
@ -48,20 +48,22 @@ class Action
|
|||
@mode = m
|
||||
@duration = d
|
||||
@event = null
|
||||
@delete_flag_ptr = false
|
||||
@deleted = false
|
||||
@reset()
|
||||
|
||||
del: ->
|
||||
if @event then @event.removeAction @
|
||||
if @object then @object.removeAction @
|
||||
if @delete_flag_ptr then @delete_flag_ptr = true
|
||||
log "Action.del #{@name} #{@event?} #{@object?}"
|
||||
if @event? then @event.removeAction @
|
||||
if @object? then @object.removeAction @
|
||||
@deleted = true
|
||||
|
||||
init: () -> @object.initAction @
|
||||
perform: () -> @object.performAction @
|
||||
finish: () -> @object.finishAction @
|
||||
finished: () ->
|
||||
log "Action.finished #{@name} #{@object?.actionFinished?}"
|
||||
@object.actionFinished @
|
||||
return if @delete_flag_ptr
|
||||
return if @deleted
|
||||
|
||||
if @current == @getDuration() # if keepRest wasn't called -> reset start and current values
|
||||
@reset()
|
||||
|
@ -89,7 +91,7 @@ class Action
|
|||
|
||||
performWithEvent: (event) ->
|
||||
eventTime = event.getTime()
|
||||
log "action.performWithEvent #{@name} eventTime #{eventTime}"
|
||||
# log "action.performWithEvent #{@name} eventTime #{eventTime}" if @name != 'noop'
|
||||
if @start == 0
|
||||
@start = eventTime
|
||||
@current = 0
|
||||
|
|
|
@ -52,27 +52,32 @@ class Actor extends Emitter
|
|||
|
||||
del: -> @deleteActions()
|
||||
|
||||
deleteActions: -> last(@actions).del() while @actions.length
|
||||
deleteActions: ->
|
||||
a?.del() for a in @actions
|
||||
@actions = []
|
||||
|
||||
removeAction: (action) -> _.pull @actions, action
|
||||
removeAction: (action) -> @actions[action.id] = null
|
||||
|
||||
getActionWithId: (actionId) ->
|
||||
if actionId < @actions.length and @actions[actionId].id == actionId
|
||||
return @actions[actionId]
|
||||
|
||||
# to be deleted...
|
||||
log "[WARNING] Actor.getActionWithId #{actionId} [#{@actions.length}]", (a.id for a in @actions)
|
||||
for a in @actions
|
||||
return a if a.id == actionId
|
||||
if @actions[actionId]?.id? and @actions[actionId].id != actionId
|
||||
throw new Error
|
||||
@actions[actionId]
|
||||
# if actionId < @actions.length and @actions[actionId].id == actionId
|
||||
# return @actions[actionId]
|
||||
#
|
||||
# # to be deleted...
|
||||
# log "[WARNING] Actor.getActionWithId #{actionId} [#{@actions.length}]", (a?.id for a in @actions)
|
||||
# for a in @actions
|
||||
# return a if a?.id == actionId
|
||||
|
||||
getActionWithName: (name) ->
|
||||
getActionWithName: (name) ->
|
||||
for a in @actions
|
||||
return a if action.name = name
|
||||
return a if a?.name == name
|
||||
|
||||
initAction: ->
|
||||
performAction: ->
|
||||
finishAction: ->
|
||||
actionFinished: ->
|
||||
finishAction: -> log "actor.finishAction not implemented? #{@name}"
|
||||
actionFinished: -> log 'actor.actionFinished not implemented? #{@name}'
|
||||
|
||||
# 000000000 000 00 00 00000000 00000000
|
||||
# 000 000 000 000 000 000 000
|
||||
|
|
|
@ -245,6 +245,7 @@ class Bot extends Pushable
|
|||
actionId = action.id
|
||||
|
||||
return if actionId == Action.NOOP or actionId == Action.SHOOT
|
||||
log "Bot.finishAction #{actionId} #{action.name}"
|
||||
|
||||
if actionId == Action.PUSH
|
||||
super action
|
||||
|
@ -260,6 +261,7 @@ class Bot extends Pushable
|
|||
@orientation = @orientation.mul @rotate_orientation.mul @rest_orientation # update rotation matrix
|
||||
@rotate_orientation.reset()
|
||||
@rest_orientation.reset()
|
||||
|
||||
else if actionId < Action.END
|
||||
@move_action = null
|
||||
|
||||
|
@ -285,34 +287,40 @@ class Bot extends Pushable
|
|||
|
||||
actionFinished: (action) ->
|
||||
actionId = action.id
|
||||
log "bot.actionFinished #{action.name} #{actionId}"
|
||||
|
||||
if not @died
|
||||
@die()
|
||||
if actionId != Action.PUSH and actionId != Action.FALL
|
||||
# dead player may only fall, nothing else
|
||||
return
|
||||
# if @isDead()
|
||||
# log "DIE!"
|
||||
# @die()
|
||||
# if actionId != Action.PUSH and actionId != Action.FALL
|
||||
# # dead player may only fall, nothing else
|
||||
# return
|
||||
|
||||
if @spiked
|
||||
log 'spiked!'
|
||||
@move_action = null
|
||||
@startTimedAction @getActionWithId(Action.NOOP), 0
|
||||
return
|
||||
|
||||
if actionId == Action.PUSH or not @direction.isZero()
|
||||
log 'super action!'
|
||||
super action
|
||||
return
|
||||
|
||||
return if @move_action # action was not a move action -> return
|
||||
if @move_action # action was not a move action -> return
|
||||
log 'action was not a move action -> return'
|
||||
return
|
||||
|
||||
# find next action depending on type of finished action and surrounding environment
|
||||
if actionId == Action.JUMP_FORWARD
|
||||
|
||||
forwardPos = @position + @getDir()
|
||||
if world.isUnoccupiedPos forwardPos
|
||||
forwardPos = @position.plus @getDir()
|
||||
log 'jump forwardPos', forwardPos
|
||||
if world.isUnoccupiedPos forwardPos
|
||||
# forward will be empty
|
||||
if world.isUnoccupiedPos forwardPos.minus @getUp()
|
||||
# below forward will also be empty
|
||||
@move_action = @getActionWithId Action.FALL_FORWARD
|
||||
@move_action.takeRest (action)
|
||||
@move_action.takeRest action
|
||||
else
|
||||
@move_action = @getActionWithId Action.FORWARD
|
||||
world.playSound 'BOT_LAND', @getPos(), 0.25
|
||||
|
@ -321,7 +329,8 @@ class Bot extends Pushable
|
|||
@move_action = @getActionWithId Action.CLIMB_UP
|
||||
world.playSound 'BOT_LAND', @getPos(), 0.5
|
||||
else if world.isUnoccupiedPos @position.minus @getUp() # below will be empty
|
||||
if move # sticky if moving
|
||||
log 'below will be empty'
|
||||
if @move # sticky if moving
|
||||
if world.isUnoccupiedPos @position.plus @getDir()
|
||||
# forward will be empty
|
||||
if world.isOccupiedPos @position.plus @getDir().minus @getUp()
|
||||
|
@ -335,26 +344,32 @@ class Bot extends Pushable
|
|||
@move_action = @getActionWithId Action.CLIMB_UP
|
||||
|
||||
if @move_action == null
|
||||
log 'fall!'
|
||||
@move_action = @getActionWithId Action.FALL
|
||||
@move_action.takeRest action
|
||||
|
||||
else if actionId == Action.FALL or actionId == Action.FALL_FORWARD # landed
|
||||
log 'fall|forward!'
|
||||
if @ == world.player
|
||||
world.playSound 'BOT_LAND'
|
||||
else
|
||||
world.playSound 'BOT_LAND', @getPos()
|
||||
|
||||
if @move_action
|
||||
log 'move_action!'
|
||||
Timer.addAction @move_action
|
||||
return
|
||||
|
||||
return if @rotate_action
|
||||
|
||||
if @move
|
||||
log '!move'
|
||||
@moveBot()
|
||||
else
|
||||
@dir_sgn = 1.0
|
||||
if actionId != Action.NOOP then @jump_once = false
|
||||
@dir_sgn = 1
|
||||
@jump_once = false if actionId != Action.NOOP
|
||||
# keep action chain flowing in order to detect environment changes
|
||||
log 'startTimedAction NOOP'
|
||||
@startTimedAction @getActionWithId(Action.NOOP), 0
|
||||
|
||||
moveBot: () ->
|
||||
|
|
|
@ -54,11 +54,11 @@ class Event
|
|||
triggerActions: () ->
|
||||
return if not @actions.length
|
||||
@time = world.getTime()
|
||||
log 'trigger actions', @time, @actions.length
|
||||
# log 'trigger actions', @time, @actions.length
|
||||
@save_actions = _.clone @actions
|
||||
while @save_actions.length
|
||||
action = last @save_actions
|
||||
log "performAction #{action.name}"
|
||||
# log "performAction #{action.name}" if action.name != 'noop'
|
||||
action.performWithEvent @
|
||||
if @save_actions.length and action == last @save_actions
|
||||
@save_actions.pop()
|
||||
|
@ -69,9 +69,13 @@ class Event
|
|||
|
||||
finishActions: () ->
|
||||
while @finished_actions.length
|
||||
action = last @finished_actions
|
||||
action.finished()
|
||||
if @finished_actions.length and action == last @finished_actions
|
||||
@finished_actions.pop()
|
||||
log "Event.finishActions pop:#{@finished_actions.length}", last(@finished_actions).name
|
||||
@finished_actions.pop().finished()
|
||||
# action = last @finished_actions
|
||||
# action.finished()
|
||||
# if @finished_actions.length and action == last @finished_actions
|
||||
# log 'pop finished'
|
||||
# @finished_actions.pop()
|
||||
# log "Event.finishActions actions:#{@actions.length}" if @actions.length > 1 or @actions[0]?.name != 'noop'
|
||||
|
||||
module.exports = Event
|
||||
|
|
|
@ -5,18 +5,20 @@
|
|||
# 000 000 000 000 0 000
|
||||
# 000 000 00000000 000 000
|
||||
|
||||
log = require '/Users/kodi/s/ko/js/tools/log'
|
||||
Actor = require './actor'
|
||||
Vector = require './lib/vector'
|
||||
Quat = require './lib/quaternion'
|
||||
Pos = require './lib/pos'
|
||||
log = require '/Users/kodi/s/ko/js/tools/log'
|
||||
Actor = require './actor'
|
||||
Pos = require './lib/pos'
|
||||
Vector = require './lib/vector'
|
||||
Quaternion = require './lib/quaternion'
|
||||
|
||||
class Item extends Actor
|
||||
|
||||
constructor: ->
|
||||
super
|
||||
@move_action = null
|
||||
@direction = new Vector
|
||||
@position = new Vector
|
||||
@current_position = new Vector
|
||||
@direction = new Vector
|
||||
@move_action = null
|
||||
|
||||
del: ->
|
||||
world.removeObject @
|
||||
|
@ -40,7 +42,7 @@ class Item extends Actor
|
|||
# getOrientation: -> @orientation
|
||||
# getCurrentPosition: -> @current_position
|
||||
# getCurrentOrientation: -> @current_orientation
|
||||
setOrientation: (q) -> @current_orientation = @orientation = new Quat q
|
||||
setOrientation: (q) -> @current_orientation = @orientation = new Quaternion q
|
||||
setCurrentPosition: (p) -> @current_position = p
|
||||
setCurrentOrientation: (q) -> @current_orientation = q
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ Bot = require './bot'
|
|||
Action = require './action'
|
||||
Timer = require './timer'
|
||||
Vector = require './lib/vector'
|
||||
Quaternion = require './lib/quaternion'
|
||||
Perspective = require './perspective'
|
||||
|
||||
forward_key = "UP"
|
||||
|
@ -433,12 +434,12 @@ class Player extends Bot
|
|||
return keyHandled()
|
||||
|
||||
if combo == @key.push
|
||||
push = true
|
||||
@push = true
|
||||
return keyHandled()
|
||||
|
||||
if combo == @key.shoot
|
||||
if not shoot
|
||||
shoot = true
|
||||
if not @shoot
|
||||
@shoot = true
|
||||
Timer.addAction @getActionWithId Action.SHOOT
|
||||
return keyHandled()
|
||||
|
||||
|
@ -469,18 +470,18 @@ class Player extends Bot
|
|||
|
||||
if combo == @key.shoot
|
||||
Timer.removeAction @getActionWithId Action.SHOOT
|
||||
shoot = false
|
||||
@shoot = false
|
||||
return releaseHandled()
|
||||
|
||||
if combo == @key.forward or combo == @key.backward
|
||||
move = false
|
||||
@move = false
|
||||
return releaseHandled()
|
||||
|
||||
if key.name == @key.jump
|
||||
jump = false
|
||||
if jump_once
|
||||
@jump = false
|
||||
if @jump_once
|
||||
if @move_action == null and world.isUnoccupiedPos position.plus @getUp()
|
||||
jump_once = false
|
||||
@jump_once = false
|
||||
@move_action = @getActionWithId Action.JUMP
|
||||
world.playSound 'BOT_JUMP'
|
||||
Timer.addAction @move_action
|
||||
|
|
|
@ -40,10 +40,12 @@ class Pushable extends Item
|
|||
@setCurrentPosition @position + action.getRelativeTime() * @direction
|
||||
|
||||
finishAction: (action) ->
|
||||
log "Pushable.finishAction #{action.name}"
|
||||
switch action.id
|
||||
when Action.PUSH, Action.FALL
|
||||
@move_action = null
|
||||
world.objectMovedFromPos @, @position
|
||||
log "Pushable.finishAction setPosition #{@current_position}"
|
||||
@setPosition @current_position
|
||||
|
||||
actionFinished: (action) ->
|
||||
|
|
|
@ -14,18 +14,18 @@ class Timer
|
|||
@init: ->
|
||||
@eventID = world.addEventWithName 'timer'
|
||||
@event = world.getEventWithId @eventID
|
||||
log "Timer.@init @eventID:#{@eventID} #{@event.name}"
|
||||
log "Timer.init @eventID:#{@eventID} #{@event.name}"
|
||||
|
||||
@removeActionsOfObject: (o) ->
|
||||
log "removeActionsOfObject"
|
||||
log "Timer.removeActionsOfObject"
|
||||
@event.removeActionsOfObject o
|
||||
|
||||
@addAction: (a) ->
|
||||
log "addAction #{a.name}"
|
||||
log "Timer.addAction #{a.name}"
|
||||
@event.addAction a
|
||||
|
||||
@removeAction: (a) ->
|
||||
log "removeAction #{a.name}"
|
||||
log "Timer.removeAction #{a.name}"
|
||||
@event.removeAction a
|
||||
|
||||
module.exports = Timer
|
||||
|
|
|
@ -40,7 +40,7 @@ class World extends Actor
|
|||
|
||||
super
|
||||
|
||||
@speed = 5
|
||||
@speed = 0.5
|
||||
|
||||
@screenSize = new Size @view.clientWidth, @view.clientHeight
|
||||
# log "view @screenSize:", @screenSize
|
||||
|
|
Loading…
Reference in New Issue
Block a user