diff --git a/coffee/action.coffee b/coffee/action.coffee index 4a6e99f..8e863c0 100644 --- a/coffee/action.coffee +++ b/coffee/action.coffee @@ -4,6 +4,8 @@ # 000 000 000 000 000 000 000 000 0000 # 000 000 0000000 000 000 0000000 000 000 +_ = require 'lodash' + class Action @NOOP = 0 @@ -20,8 +22,17 @@ class Action @SHOOT = 11 @END = 12 - constructor: (o, i, n, d, m) -> + @ONCE = 0 + @CONTINUOUS = 1 + @REPEAT = 2 + constructor: (o, i, n, d, m) -> + if _.isPlainObject o + i = o.id ? 0 + n = o.name + d = o.duration ? 0 + m = o.mode ? Action.ONCE + o = o.func @action_object = o @action_name = n @action_id = i @@ -90,14 +101,14 @@ class Action @current = 0 @rest = 0 @last = 0 - if @duration == 0 and @mode == ONCE + if @duration == 0 and @mode == Action.ONCE event.removeAction @ @perform() @last = @current - if @duration == 0 and @mode == ONCE + if @duration == 0 and @mode == Action.ONCE @finished() else currentDiff = eventTime - @start @@ -109,15 +120,15 @@ class Action @perform() @last = 0 - if @mode == CONTINUOUS + if @mode == Action.CONTINUOUS @current = @rest return - if @mode == ONCE + if @mode == Action.ONCE event.removeAction @ @finish() - if @mode == REPEAT + if @mode == Action.REPEAT if @current == @getDuration() # if keepRest wasn't called -> reset start and current values @reset() return diff --git a/coffee/actor.coffee b/coffee/actor.coffee index 1193676..e40d026 100644 --- a/coffee/actor.coffee +++ b/coffee/actor.coffee @@ -17,6 +17,7 @@ class Actor extends event constructor: -> @actions = [] @events = [] + super addAction: (action) -> @actions.push action if not _.find @actions, action @@ -48,13 +49,13 @@ class Actor extends event _.pull @actions, action getActionWithId: (actionId) -> - if actionId < @actions.size() and @actions[actionId].getId() == actionId + if actionId < @actions.length and @actions[actionId].id == actionId return @actions[actionId] # to be deleted... log "[WARNING] Actor.getActionWithId #{actionId} [#{@actions.length}]" for a in @actions - return a if a.getId() == actionId + return a if a.id == actionId getActionWithName: (name) -> for a in @actions @@ -62,7 +63,7 @@ class Actor extends event addEventWithName: (eventName) -> if @getEventWithName eventName # to be removed - log "KikiActionObject::addEventWithName '#{eventName}' already in use!" + log "Actor.addEventWithName '#{eventName}' already in use!" return -1; # shouldn't happen anyway :-) @events.push new Event @, eventName @events.length-1 diff --git a/coffee/bot.coffee b/coffee/bot.coffee index 5d191eb..a71ade6 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -5,11 +5,14 @@ # 0000000 0000000 000 Pushable = require './pushable' +Action = require './action' class Bot extends Pushable constructor: () -> - + + super + @geom = new THREE.SphereGeometry 1, 32, 32 @mat = new THREE.MeshPhongMaterial color: 0x0000ff @@ -45,16 +48,16 @@ class Bot extends Pushable @dir_sgn = 1.0 - @addAction new KikiAction @, Action.NOOP, "noop", 0 - @addAction new KikiAction @, Action.FORWARD, "move forward", 200 - @addAction new KikiAction @, Action.CLIMB_UP, "climb up", 200 - @addAction new KikiAction @, Action.CLIMB_DOWN, "climb down", 500 - @addAction new KikiAction @, Action.TURN_LEFT, "turn left", 200 - @addAction new KikiAction @, Action.TURN_RIGHT, "turn right", 200 - @addAction new KikiAction @, Action.JUMP, "jump", 120 - @addAction new KikiAction @, Action.JUMP_FORWARD, "jump forward", 200 - @addAction new KikiAction @, Action.FALL_FORWARD, "fall forward", 200 - @addAction new KikiAction @, Action.SHOOT, "shoot", 200, KikiAction.REPEAT + @addAction new Action @, Action.NOOP, "noop", 0 + @addAction new Action @, Action.FORWARD, "move forward", 200 + @addAction new Action @, Action.CLIMB_UP, "climb up", 200 + @addAction new Action @, Action.CLIMB_DOWN, "climb down", 500 + @addAction new Action @, Action.TURN_LEFT, "turn left", 200 + @addAction new Action @, Action.TURN_RIGHT, "turn right", 200 + @addAction new Action @, Action.JUMP, "jump", 120 + @addAction new Action @, Action.JUMP_FORWARD, "jump forward", 200 + @addAction new Action @, Action.FALL_FORWARD, "fall forward", 200 + @addAction new Action @, Action.SHOOT, "shoot", 200, Action.REPEAT @getActionWithId(Action.FALL).setDuration 120 @addEventWithName "died" @@ -97,12 +100,12 @@ class Bot extends Pushable @spiked = false @died = false - isFalling: -> @move_action and @move_action.getId() == Action.FALL + isFalling: -> @move_action and @move_action.id == Action.FALL initAction: (action) -> newPos = new KikiPos @position - switch action.getId() + switch action.id when Action.NOOP then return when Action.FORWARD then newPos += @getDir() @@ -125,7 +128,7 @@ class Bot extends Pushable # world.objectWillMoveToPos (@, newPos, action.getDuration()) performAction: (action) -> - actionId = action.getId() + actionId = action.id relTime = action.getRelativeTime() dltTime = action.getRelativeDelta() @@ -221,7 +224,7 @@ class Bot extends Pushable finishAction: (action) -> - actionId = action.getId() + actionId = action.id return if actionId == Action.NOOP or actionId == Action.SHOOT @@ -247,7 +250,7 @@ class Bot extends Pushable if @rotate_action and actionId != Action.JUMP_FORWARD # bot is currently performing a rotation -> # take over result of rotation to prevent sliding - if @rotate_action.getId() == Action.TURN_LEFT + if @rotate_action.id == Action.TURN_LEFT @orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation @rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) else @@ -263,7 +266,7 @@ class Bot extends Pushable @rest_orientation.reset() actionFinished: (action) -> - actionId = action.getId() + actionId = action.id if @isDead() die() if not @died diff --git a/coffee/player.coffee b/coffee/player.coffee index 403b4ef..00abce4 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -37,9 +37,9 @@ class Player extends Bot # @flags[KDL_KEYHANDLER_FLAG_HANDLES_RELEASE] = true - @addAction new KikiAction @, Action.LOOK_UP, "look up", 220 - @addAction new KikiAction @, Action.LOOK_DOWN, "look down", 220 - @addAction new KikiAction @, Action.LOOK_RESET, "look reset", 60 + @addAction new Action @, Action.LOOK_UP, "look up", 220 + @addAction new Action @, Action.LOOK_DOWN, "look down", 220 + @addAction new Action @, Action.LOOK_RESET, "look reset", 60 @addEventWithName "keyset" @addEventWithName "keyset failed" @@ -94,7 +94,7 @@ class Player extends Bot if (move_action) relTime = (Controller.getTime() - move_action.getStart()) / move_action.getDuration() if relTime <= 1.0 - switch move_action.getId() + switch move_action.id when Action.FORWARD current_position = position + relTime * getDir() when Action.FALL @@ -258,7 +258,7 @@ class Player extends Bot return projection initAction: (action) -> - actionId = action.getId() + actionId = action.id switch actionId when Action.CLIMB_DOWN, Action.FORWARD @status.addMoves 1 @@ -273,7 +273,7 @@ class Player extends Bot performAction: (action) -> relTime = action.getRelativeTime() - switch action.getId() + switch action.id when Action.NOOP then return when Action.LOOK_UP @@ -291,7 +291,7 @@ class Player extends Bot KikiBot.performAction action finishAction: (action) -> - actionId = action.getId() + actionId = action.id if actionId == Action.LOOK_RESET @look_action = null @@ -433,7 +433,7 @@ class Player extends Bot return releaseHandled() if keyName == look_down_key or keyName == look_up_key - if @look_action and @look_action.getId() != Action.LOOK_RESET + if @look_action and @look_action.id != Action.LOOK_RESET Controller.timer_event.removeAction @look_action @look_action = getActionWithId Action.LOOK_RESET Controller.timer_event.addAction @look_action diff --git a/coffee/pushable.coffee b/coffee/pushable.coffee index 5db284b..097a895 100644 --- a/coffee/pushable.coffee +++ b/coffee/pushable.coffee @@ -35,19 +35,19 @@ class Pushable extends Item # Controller.world->objectWillMoveToPos @, @position + @direction, action->getDuration() performAction: (action) -> - switch action.getId() + switch action.id when Action.PUSH, Action.FALL @setCurrentPosition @position + action.getRelativeTime() * @direction finishAction: (action) -> - switch action.getId() + switch action.id when Action.PUSH, Action.FALL @move_action = null world.objectMovedFromPos @, @position @setPosition @current_position actionFinished: (action) -> - actionId = action.getId() + actionId = action.id if actionId == Action.PUSH or actionId == Action.FALL gravityDir = @direction diff --git a/coffee/world.coffee b/coffee/world.coffee index 9b31972..6589bdf 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -718,6 +718,18 @@ class KikiWorld getRelativeTime: -> @frame_time % (10000/@speed)/(10000.0/@speed) getRelativeDelta: -> (@frame_time - @last_time)/(10000.0/@speed) + continuous: (cb) -> + new Action + func: cb + name: "continuous" + mode: Action.CONTINUOUS + + once: (cb) -> + new Action + func: cb + name: "once" + mode: Action.ONCE + resized: (w,h) -> # log "world.resized w:#{w} h:#{h}" @aspect = w/h