From 4a4c062a59f5ceb0d41db6248202106b5701beae Mon Sep 17 00:00:00 2001 From: monsterkodi Date: Fri, 12 Aug 2016 00:56:54 +0200 Subject: [PATCH] event --- coffee/action.coffee | 22 ++++++------- coffee/actor.coffee | 3 +- coffee/bot.coffee | 4 +-- coffee/event.coffee | 74 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 coffee/event.coffee diff --git a/coffee/action.coffee b/coffee/action.coffee index 8e863c0..4805646 100644 --- a/coffee/action.coffee +++ b/coffee/action.coffee @@ -33,12 +33,12 @@ class Action d = o.duration ? 0 m = o.mode ? Action.ONCE o = o.func - @action_object = o - @action_name = n - @action_id = i - @mode = m - @duration = d - @event = null + @object = o + @name = n + @id = i + @mode = m + @duration = d + @event = null @delete_flag_ptr = false @reset() @@ -57,14 +57,14 @@ class Action del: -> 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 - init: () -> @action_object.initAction @ - perform: () -> @action_object.performAction @ - finish: () -> @action_object.finishAction @ + init: () -> @object.initAction @ + perform: () -> @object.performAction @ + finish: () -> @object.finishAction @ finished: () -> - @action_object.actionFinished @ + @object.actionFinished @ return if @delete_flag_ptr if @current == @getDuration() # if keepRest wasn't called -> reset start and current values diff --git a/coffee/actor.coffee b/coffee/actor.coffee index e40d026..20cd959 100644 --- a/coffee/actor.coffee +++ b/coffee/actor.coffee @@ -41,8 +41,7 @@ class Actor extends event # Controller.timer_event.addAction action startTimedAction: (action, duration) -> - if duration >= 0 - action.setDuration duration + action.duration = duration if duration >= 0 # Controller.timer_event.addAction action removeAction: (action) -> diff --git a/coffee/bot.coffee b/coffee/bot.coffee index a71ade6..d7c9c40 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -59,10 +59,10 @@ class Bot extends Pushable @addAction new Action @, Action.FALL_FORWARD, "fall forward", 200 @addAction new Action @, Action.SHOOT, "shoot", 200, Action.REPEAT - @getActionWithId(Action.FALL).setDuration 120 + @getActionWithId(Action.FALL).duration = 120 @addEventWithName "died" - @startTimedAction @getActionWithId Action.NOOP, 500 + @startTimedAction @getActionWithId(Action.NOOP), 500 addMoves: (m) -> @moves += m addHealth: (h) -> @health = Math.max @health+h diff --git a/coffee/event.coffee b/coffee/event.coffee new file mode 100644 index 0000000..efe9024 --- /dev/null +++ b/coffee/event.coffee @@ -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