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