This commit is contained in:
monsterkodi 2016-08-13 21:51:18 +02:00
parent d8286b39e0
commit ef5e1c0355
9 changed files with 90 additions and 59 deletions

View File

@ -48,20 +48,22 @@ class Action
@mode = m @mode = m
@duration = d @duration = d
@event = null @event = null
@delete_flag_ptr = false @deleted = false
@reset() @reset()
del: -> del: ->
if @event then @event.removeAction @ log "Action.del #{@name} #{@event?} #{@object?}"
if @object then @object.removeAction @ if @event? then @event.removeAction @
if @delete_flag_ptr then @delete_flag_ptr = true if @object? then @object.removeAction @
@deleted = true
init: () -> @object.initAction @ init: () -> @object.initAction @
perform: () -> @object.performAction @ perform: () -> @object.performAction @
finish: () -> @object.finishAction @ finish: () -> @object.finishAction @
finished: () -> finished: () ->
log "Action.finished #{@name} #{@object?.actionFinished?}"
@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 if @current == @getDuration() # if keepRest wasn't called -> reset start and current values
@reset() @reset()
@ -89,7 +91,7 @@ class Action
performWithEvent: (event) -> performWithEvent: (event) ->
eventTime = event.getTime() eventTime = event.getTime()
log "action.performWithEvent #{@name} eventTime #{eventTime}" # log "action.performWithEvent #{@name} eventTime #{eventTime}" if @name != 'noop'
if @start == 0 if @start == 0
@start = eventTime @start = eventTime
@current = 0 @current = 0

View File

@ -52,27 +52,32 @@ class Actor extends Emitter
del: -> @deleteActions() 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) -> getActionWithId: (actionId) ->
if actionId < @actions.length and @actions[actionId].id == actionId if @actions[actionId]?.id? and @actions[actionId].id != actionId
return @actions[actionId] throw new Error
@actions[actionId]
# to be deleted... # if actionId < @actions.length and @actions[actionId].id == actionId
log "[WARNING] Actor.getActionWithId #{actionId} [#{@actions.length}]", (a.id for a in @actions) # return @actions[actionId]
for a in @actions #
return a if a.id == 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 for a in @actions
return a if action.name = name return a if a?.name == name
initAction: -> initAction: ->
performAction: -> performAction: ->
finishAction: -> finishAction: -> log "actor.finishAction not implemented? #{@name}"
actionFinished: -> actionFinished: -> log 'actor.actionFinished not implemented? #{@name}'
# 000000000 000 00 00 00000000 00000000 # 000000000 000 00 00 00000000 00000000
# 000 000 000 000 000 000 000 # 000 000 000 000 000 000 000

View File

@ -245,6 +245,7 @@ class Bot extends Pushable
actionId = action.id actionId = action.id
return if actionId == Action.NOOP or actionId == Action.SHOOT return if actionId == Action.NOOP or actionId == Action.SHOOT
log "Bot.finishAction #{actionId} #{action.name}"
if actionId == Action.PUSH if actionId == Action.PUSH
super action super action
@ -260,6 +261,7 @@ class Bot extends Pushable
@orientation = @orientation.mul @rotate_orientation.mul @rest_orientation # update rotation matrix @orientation = @orientation.mul @rotate_orientation.mul @rest_orientation # update rotation matrix
@rotate_orientation.reset() @rotate_orientation.reset()
@rest_orientation.reset() @rest_orientation.reset()
else if actionId < Action.END else if actionId < Action.END
@move_action = null @move_action = null
@ -285,34 +287,40 @@ class Bot extends Pushable
actionFinished: (action) -> actionFinished: (action) ->
actionId = action.id actionId = action.id
log "bot.actionFinished #{action.name} #{actionId}"
if not @died # if @isDead()
@die() # log "DIE!"
if actionId != Action.PUSH and actionId != Action.FALL # @die()
# dead player may only fall, nothing else # if actionId != Action.PUSH and actionId != Action.FALL
return # # dead player may only fall, nothing else
# return
if @spiked if @spiked
log 'spiked!'
@move_action = null @move_action = null
@startTimedAction @getActionWithId(Action.NOOP), 0 @startTimedAction @getActionWithId(Action.NOOP), 0
return return
if actionId == Action.PUSH or not @direction.isZero() if actionId == Action.PUSH or not @direction.isZero()
log 'super action!'
super action super action
return 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 # find next action depending on type of finished action and surrounding environment
if actionId == Action.JUMP_FORWARD if actionId == Action.JUMP_FORWARD
forwardPos = @position.plus @getDir()
forwardPos = @position + @getDir() log 'jump forwardPos', forwardPos
if world.isUnoccupiedPos forwardPos if world.isUnoccupiedPos forwardPos
# forward will be empty # forward will be empty
if world.isUnoccupiedPos forwardPos.minus @getUp() if world.isUnoccupiedPos forwardPos.minus @getUp()
# below forward will also be empty # below forward will also be empty
@move_action = @getActionWithId Action.FALL_FORWARD @move_action = @getActionWithId Action.FALL_FORWARD
@move_action.takeRest (action) @move_action.takeRest action
else else
@move_action = @getActionWithId Action.FORWARD @move_action = @getActionWithId Action.FORWARD
world.playSound 'BOT_LAND', @getPos(), 0.25 world.playSound 'BOT_LAND', @getPos(), 0.25
@ -321,7 +329,8 @@ class Bot extends Pushable
@move_action = @getActionWithId Action.CLIMB_UP @move_action = @getActionWithId Action.CLIMB_UP
world.playSound 'BOT_LAND', @getPos(), 0.5 world.playSound 'BOT_LAND', @getPos(), 0.5
else if world.isUnoccupiedPos @position.minus @getUp() # below will be empty 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() if world.isUnoccupiedPos @position.plus @getDir()
# forward will be empty # forward will be empty
if world.isOccupiedPos @position.plus @getDir().minus @getUp() if world.isOccupiedPos @position.plus @getDir().minus @getUp()
@ -335,26 +344,32 @@ class Bot extends Pushable
@move_action = @getActionWithId Action.CLIMB_UP @move_action = @getActionWithId Action.CLIMB_UP
if @move_action == null if @move_action == null
log 'fall!'
@move_action = @getActionWithId Action.FALL @move_action = @getActionWithId Action.FALL
@move_action.takeRest action @move_action.takeRest action
else if actionId == Action.FALL or actionId == Action.FALL_FORWARD # landed else if actionId == Action.FALL or actionId == Action.FALL_FORWARD # landed
log 'fall|forward!'
if @ == world.player if @ == world.player
world.playSound 'BOT_LAND' world.playSound 'BOT_LAND'
else else
world.playSound 'BOT_LAND', @getPos() world.playSound 'BOT_LAND', @getPos()
if @move_action if @move_action
log 'move_action!'
Timer.addAction @move_action Timer.addAction @move_action
return return
return if @rotate_action return if @rotate_action
if @move if @move
log '!move'
@moveBot() @moveBot()
else else
@dir_sgn = 1.0 @dir_sgn = 1
if actionId != Action.NOOP then @jump_once = false @jump_once = false if actionId != Action.NOOP
# keep action chain flowing in order to detect environment changes # keep action chain flowing in order to detect environment changes
log 'startTimedAction NOOP'
@startTimedAction @getActionWithId(Action.NOOP), 0 @startTimedAction @getActionWithId(Action.NOOP), 0
moveBot: () -> moveBot: () ->

View File

@ -54,11 +54,11 @@ class Event
triggerActions: () -> triggerActions: () ->
return if not @actions.length return if not @actions.length
@time = world.getTime() @time = world.getTime()
log 'trigger actions', @time, @actions.length # log 'trigger actions', @time, @actions.length
@save_actions = _.clone @actions @save_actions = _.clone @actions
while @save_actions.length while @save_actions.length
action = last @save_actions action = last @save_actions
log "performAction #{action.name}" # log "performAction #{action.name}" if action.name != 'noop'
action.performWithEvent @ action.performWithEvent @
if @save_actions.length and action == last @save_actions if @save_actions.length and action == last @save_actions
@save_actions.pop() @save_actions.pop()
@ -69,9 +69,13 @@ class Event
finishActions: () -> finishActions: () ->
while @finished_actions.length while @finished_actions.length
action = last @finished_actions log "Event.finishActions pop:#{@finished_actions.length}", last(@finished_actions).name
action.finished() @finished_actions.pop().finished()
if @finished_actions.length and action == last @finished_actions # action = last @finished_actions
@finished_actions.pop() # 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 module.exports = Event

View File

@ -7,16 +7,18 @@
log = require '/Users/kodi/s/ko/js/tools/log' log = require '/Users/kodi/s/ko/js/tools/log'
Actor = require './actor' Actor = require './actor'
Vector = require './lib/vector'
Quat = require './lib/quaternion'
Pos = require './lib/pos' Pos = require './lib/pos'
Vector = require './lib/vector'
Quaternion = require './lib/quaternion'
class Item extends Actor class Item extends Actor
constructor: -> constructor: ->
super super
@move_action = null @position = new Vector
@current_position = new Vector
@direction = new Vector @direction = new Vector
@move_action = null
del: -> del: ->
world.removeObject @ world.removeObject @
@ -40,7 +42,7 @@ class Item extends Actor
# getOrientation: -> @orientation # getOrientation: -> @orientation
# getCurrentPosition: -> @current_position # getCurrentPosition: -> @current_position
# getCurrentOrientation: -> @current_orientation # getCurrentOrientation: -> @current_orientation
setOrientation: (q) -> @current_orientation = @orientation = new Quat q setOrientation: (q) -> @current_orientation = @orientation = new Quaternion q
setCurrentPosition: (p) -> @current_position = p setCurrentPosition: (p) -> @current_position = p
setCurrentOrientation: (q) -> @current_orientation = q setCurrentOrientation: (q) -> @current_orientation = q

View File

@ -10,6 +10,7 @@ Bot = require './bot'
Action = require './action' Action = require './action'
Timer = require './timer' Timer = require './timer'
Vector = require './lib/vector' Vector = require './lib/vector'
Quaternion = require './lib/quaternion'
Perspective = require './perspective' Perspective = require './perspective'
forward_key = "UP" forward_key = "UP"
@ -433,12 +434,12 @@ class Player extends Bot
return keyHandled() return keyHandled()
if combo == @key.push if combo == @key.push
push = true @push = true
return keyHandled() return keyHandled()
if combo == @key.shoot if combo == @key.shoot
if not shoot if not @shoot
shoot = true @shoot = true
Timer.addAction @getActionWithId Action.SHOOT Timer.addAction @getActionWithId Action.SHOOT
return keyHandled() return keyHandled()
@ -469,18 +470,18 @@ class Player extends Bot
if combo == @key.shoot if combo == @key.shoot
Timer.removeAction @getActionWithId Action.SHOOT Timer.removeAction @getActionWithId Action.SHOOT
shoot = false @shoot = false
return releaseHandled() return releaseHandled()
if combo == @key.forward or combo == @key.backward if combo == @key.forward or combo == @key.backward
move = false @move = false
return releaseHandled() return releaseHandled()
if key.name == @key.jump if key.name == @key.jump
jump = false @jump = false
if jump_once if @jump_once
if @move_action == null and world.isUnoccupiedPos position.plus @getUp() if @move_action == null and world.isUnoccupiedPos position.plus @getUp()
jump_once = false @jump_once = false
@move_action = @getActionWithId Action.JUMP @move_action = @getActionWithId Action.JUMP
world.playSound 'BOT_JUMP' world.playSound 'BOT_JUMP'
Timer.addAction @move_action Timer.addAction @move_action

View File

@ -40,10 +40,12 @@ class Pushable extends Item
@setCurrentPosition @position + action.getRelativeTime() * @direction @setCurrentPosition @position + action.getRelativeTime() * @direction
finishAction: (action) -> finishAction: (action) ->
log "Pushable.finishAction #{action.name}"
switch action.id switch action.id
when Action.PUSH, Action.FALL when Action.PUSH, Action.FALL
@move_action = null @move_action = null
world.objectMovedFromPos @, @position world.objectMovedFromPos @, @position
log "Pushable.finishAction setPosition #{@current_position}"
@setPosition @current_position @setPosition @current_position
actionFinished: (action) -> actionFinished: (action) ->

View File

@ -14,18 +14,18 @@ class Timer
@init: -> @init: ->
@eventID = world.addEventWithName 'timer' @eventID = world.addEventWithName 'timer'
@event = world.getEventWithId @eventID @event = world.getEventWithId @eventID
log "Timer.@init @eventID:#{@eventID} #{@event.name}" log "Timer.init @eventID:#{@eventID} #{@event.name}"
@removeActionsOfObject: (o) -> @removeActionsOfObject: (o) ->
log "removeActionsOfObject" log "Timer.removeActionsOfObject"
@event.removeActionsOfObject o @event.removeActionsOfObject o
@addAction: (a) -> @addAction: (a) ->
log "addAction #{a.name}" log "Timer.addAction #{a.name}"
@event.addAction a @event.addAction a
@removeAction: (a) -> @removeAction: (a) ->
log "removeAction #{a.name}" log "Timer.removeAction #{a.name}"
@event.removeAction a @event.removeAction a
module.exports = Timer module.exports = Timer

View File

@ -40,7 +40,7 @@ class World extends Actor
super super
@speed = 5 @speed = 0.5
@screenSize = new Size @view.clientWidth, @view.clientHeight @screenSize = new Size @view.clientWidth, @view.clientHeight
# log "view @screenSize:", @screenSize # log "view @screenSize:", @screenSize