event
This commit is contained in:
parent
15a642863e
commit
4a4c062a59
|
@ -33,9 +33,9 @@ class Action
|
||||||
d = o.duration ? 0
|
d = o.duration ? 0
|
||||||
m = o.mode ? Action.ONCE
|
m = o.mode ? Action.ONCE
|
||||||
o = o.func
|
o = o.func
|
||||||
@action_object = o
|
@object = o
|
||||||
@action_name = n
|
@name = n
|
||||||
@action_id = i
|
@id = i
|
||||||
@mode = m
|
@mode = m
|
||||||
@duration = d
|
@duration = d
|
||||||
@event = null
|
@event = null
|
||||||
|
@ -57,14 +57,14 @@ class Action
|
||||||
|
|
||||||
del: ->
|
del: ->
|
||||||
if @event then @event.removeAction @
|
if @event then @event.removeAction @
|
||||||
if @action_object then @action_object.removeAction @
|
if @object then @object.removeAction @
|
||||||
if @delete_flag_ptr then @delete_flag_ptr = true
|
if @delete_flag_ptr then @delete_flag_ptr = true
|
||||||
|
|
||||||
init: () -> @action_object.initAction @
|
init: () -> @object.initAction @
|
||||||
perform: () -> @action_object.performAction @
|
perform: () -> @object.performAction @
|
||||||
finish: () -> @action_object.finishAction @
|
finish: () -> @object.finishAction @
|
||||||
finished: () ->
|
finished: () ->
|
||||||
@action_object.actionFinished @
|
@object.actionFinished @
|
||||||
return if @delete_flag_ptr
|
return if @delete_flag_ptr
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -41,8 +41,7 @@ class Actor extends event
|
||||||
# Controller.timer_event.addAction action
|
# Controller.timer_event.addAction action
|
||||||
|
|
||||||
startTimedAction: (action, duration) ->
|
startTimedAction: (action, duration) ->
|
||||||
if duration >= 0
|
action.duration = duration if duration >= 0
|
||||||
action.setDuration duration
|
|
||||||
# Controller.timer_event.addAction action
|
# Controller.timer_event.addAction action
|
||||||
|
|
||||||
removeAction: (action) ->
|
removeAction: (action) ->
|
||||||
|
|
|
@ -59,10 +59,10 @@ class Bot extends Pushable
|
||||||
@addAction new Action @, Action.FALL_FORWARD, "fall forward", 200
|
@addAction new Action @, Action.FALL_FORWARD, "fall forward", 200
|
||||||
@addAction new Action @, Action.SHOOT, "shoot", 200, Action.REPEAT
|
@addAction new Action @, Action.SHOOT, "shoot", 200, Action.REPEAT
|
||||||
|
|
||||||
@getActionWithId(Action.FALL).setDuration 120
|
@getActionWithId(Action.FALL).duration = 120
|
||||||
@addEventWithName "died"
|
@addEventWithName "died"
|
||||||
|
|
||||||
@startTimedAction @getActionWithId Action.NOOP, 500
|
@startTimedAction @getActionWithId(Action.NOOP), 500
|
||||||
|
|
||||||
addMoves: (m) -> @moves += m
|
addMoves: (m) -> @moves += m
|
||||||
addHealth: (h) -> @health = Math.max @health+h
|
addHealth: (h) -> @health = Math.max @health+h
|
||||||
|
|
74
coffee/event.coffee
Normal file
74
coffee/event.coffee
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
# 00000000 000 000 00000000 000 000 000000000
|
||||||
|
# 000 000 000 000 0000 000 000
|
||||||
|
# 0000000 000 000 0000000 000 0 000 000
|
||||||
|
# 000 000 000 000 0000 000
|
||||||
|
# 00000000 0 00000000 000 000 000
|
||||||
|
{
|
||||||
|
last
|
||||||
|
} = require '/Users/kodi/s/ko/js/tools/tools'
|
||||||
|
log = require '/Users/kodi/s/ko/js/tools/log'
|
||||||
|
_ = require 'lodash'
|
||||||
|
|
||||||
|
class Event
|
||||||
|
|
||||||
|
constructor: (obj, name) ->
|
||||||
|
@object = obj
|
||||||
|
@name = name
|
||||||
|
@save_actions = []
|
||||||
|
|
||||||
|
hasAction: (action) -> _.find @actions, action
|
||||||
|
|
||||||
|
addAction: (action) ->
|
||||||
|
if @hasAction action
|
||||||
|
actions.push action
|
||||||
|
action.event = @
|
||||||
|
action.init()
|
||||||
|
|
||||||
|
removeAllActions: () ->
|
||||||
|
while actions.length
|
||||||
|
@removeAction last @actions
|
||||||
|
|
||||||
|
getActionsOfObject: (object) ->
|
||||||
|
actions = []
|
||||||
|
for a in _.clone @actions
|
||||||
|
if a.object == object
|
||||||
|
actions.push a
|
||||||
|
actions
|
||||||
|
|
||||||
|
removeActionsOfObject: (object) ->
|
||||||
|
for a in @actions
|
||||||
|
@removeAction a if a.object == object
|
||||||
|
|
||||||
|
removeActionWithName: (actionName) ->
|
||||||
|
for a in @actions
|
||||||
|
if a.name == actionName
|
||||||
|
@removeAction a
|
||||||
|
|
||||||
|
removeAction: (action) ->
|
||||||
|
action.event = null
|
||||||
|
_.pull @actions, action
|
||||||
|
_.pull @save_actions, action
|
||||||
|
_.pull @finished_actions, action
|
||||||
|
|
||||||
|
triggerActions: () ->
|
||||||
|
time = KEventHandler.getTime()
|
||||||
|
@save_actions = KikiActionList (actions)
|
||||||
|
while @save_actions.length
|
||||||
|
action= last @save_actions
|
||||||
|
action.performWithEvent @
|
||||||
|
if @save_actions.length and action == last @save_actions
|
||||||
|
@save_actions.pop()
|
||||||
|
|
||||||
|
addFinishedAction: (action) -> @finished_actions.push action
|
||||||
|
|
||||||
|
finishActions: () ->
|
||||||
|
try
|
||||||
|
while @finished_actions.length
|
||||||
|
action = last @finished_actions
|
||||||
|
action.finished()
|
||||||
|
if @finished_actions.length and action == last @finished_actions
|
||||||
|
@finished_actions.pop()
|
||||||
|
catch err
|
||||||
|
log "!!! finishActions failed !!!", err
|
||||||
|
|
||||||
|
module.exports = Event
|
Loading…
Reference in New Issue
Block a user