diff --git a/coffee/action.coffee b/coffee/action.coffee new file mode 100644 index 0000000..9456b31 --- /dev/null +++ b/coffee/action.coffee @@ -0,0 +1,117 @@ +# 0000000 0000000 000000000 000 0000000 000 000 +# 000 000 000 000 000 000 000 0000 000 +# 000000000 000 000 000 000 000 000 0 000 +# 000 000 000 000 000 000 000 000 0000 +# 000 000 0000000 000 000 0000000 000 000 + +class Action + + constructor: (o, i, n, d, m) -> + + @action_object = o + @action_name = n + @action_id = i + @mode = m + @duration = d + @event = null + @delete_flag_ptr = false + @reset() + +# KikiAction (KikiActionObject* o, int d, int m ) +# { + # action_object = o + # action_id = 0 + # mode = m + # duration = d + # event = null + + # delete_flag_ptr = null + + # @reset() +# } + + del: -> + if @event then @event.removeAction @ + if @action_object then @action_object.removeAction @ + if @delete_flag_ptr then @delete_flag_ptr = true + + init: () -> @action_object.initAction @ + perform: () -> @action_object.performAction @ + finish: () -> @action_object.finishAction @ + finished: () -> + @action_object.actionFinished @ + return if @delete_flag_ptr + + if @current == @getDuration() # if keepRest wasn't called -> reset start and current values + @reset() + + reset: () -> + @start = 0 + @rest = 0 + @last = 0 + @current = 0 + + takeRest: (action) -> + @current = action.rest + @start = action.start + @last = 0 + @rest = 0 + + keepRest: () -> + if @rest != 0 + @current = @rest + @rest = 0 + + getRelativeTime: () -> + return @current / @getDuration() + + getDuration: () -> + return Controller.mapMsTime @duration + + performWithEvent: (event) -> + eventTime = event.getTime() + + if @start == 0 + @start = eventTime + @current = 0 + @rest = 0 + @last = 0 + if @duration == 0 and @mode == ONCE + event.removeAction @ + + @perform() + + @last = @current + + if @duration == 0 and @mode == ONCE + @finished() + else + currentDiff = eventTime - @start + if currentDiff >= @getDuration() + @current = @getDuration() + + @start += @current + @rest = eventTime - @start + @perform() + @last = 0 + + if @mode == CONTINUOUS + @current = @rest + return + if @mode == ONCE + event.removeAction @ + + @finish() + + if @mode == REPEAT + if @current == @getDuration() # if keepRest wasn't called -> reset start and current values + @reset() + return + + event.addFinishedAction @ + else + @current = currentDiff + @perform() + @last = @current + +module.exports = Action diff --git a/coffee/actor.coffee b/coffee/actor.coffee new file mode 100644 index 0000000..53db60f --- /dev/null +++ b/coffee/actor.coffee @@ -0,0 +1,75 @@ + +# 0000000 0000000 000000000 0000000 00000000 +# 000 000 000 000 000 000 000 000 +# 000000000 000 000 000 000 0000000 +# 000 000 000 000 000 000 000 000 +# 000 000 0000000 000 0000000 000 000 +{ +last +} = require '/Users/kodi/s/ko/js/tools/tools' +log = require '/Users/kodi/s/ko/js/tools/log' +event = require 'events' +_ = require 'lodash' + +class Actor extends event + + constructor: -> + @actions = [] + @events = [] + + addAction: (action) -> + @actions.push action if not _.find @actions, action + + del: -> @deleteActions() + + deleteActions: -> + last(@actions).del() while @actions.length + + initAction: -> + performAction: -> + finishAction: -> + actionFinished: -> + + stopAction: (action) -> + # Controller.timer_event.removeAction action + + startTimer: (duration, mode) -> + action = new Action @, 0, "timer", duration, mode + @actions.push action + # Controller.timer_event.addAction action + + startTimedAction: (action, duration) -> + if duration >= 0 + action.setDuration duration + # Controller.timer_event.addAction action + + removeAction: (action) -> + _.pull @actions, action + + getActionWithId: (actionId) -> + if actionId < @actions.size() and @actions[actionId].getId() == actionId + return @actions[actionId] + + # to be deleted... + log "[WARNING] Actor.getActionWithId #{actionId} [#{@actions.length}]" + for a in @actions + return a if a.getId() == actionId + + getActionWithName: (name) -> + for a in @actions + return a if action.name = name + + addEventWithName: (eventName) -> + if @getEventWithName eventName # to be removed + log "KikiActionObject::addEventWithName '#{eventName}' already in use!" + return -1; # shouldn't happen anyway :-) + @events.push new Event @, eventName + @events.length-1 + + getEventWithName: (name) -> + for e in @events + return e if e.name = name + + getEventWithId: (eventId) -> + return @events[eventId] + \ No newline at end of file diff --git a/coffee/item.coffee b/coffee/item.coffee index 1ead40f..f82260d 100644 --- a/coffee/item.coffee +++ b/coffee/item.coffee @@ -6,13 +6,14 @@ # 000 000 00000000 000 000 log = require '/Users/kodi/s/ko/js/tools/log' +Actor = require './actor' Vector = require './lib/vector' Pos = require './lib/pos' -event = require 'events' -class Item extends event +class Item extends Actor constructor: -> + super @move_action = null @direction = new Vector @@ -20,10 +21,6 @@ class Item extends event world.removeObject @ @emit 'deleted' - initAction: -> - performAction: -> - finishAction: -> - actionFinished: -> newCellMate: -> cellMateLeft: -> bulletImpact: ->