lib/gamebox/core/behavior.rb in gamebox-0.4.0 vs lib/gamebox/core/behavior.rb in gamebox-0.4.1
- old
+ new
@@ -2,33 +2,51 @@
class Behavior
construct_with :actor, :behavior_factory
attr_accessor :opts
- def configure(opts={})
- @opts = opts
- setup
- end
-
- def setup
- end
-
+ # Specifies messages that your behavior is interested in and
+ # that you have defined methods for in your helpers block.
+ #
+ # reacts_with :remove
+ #
+ # helpers do
+ # def remove
+ # input_manager.unsubscribe_all self
+ # end
+ # end
def reacts_with(*messages_with_methods)
- # TODO do a merge here..
- @message_handlers = messages_with_methods
+ @message_handlers ||= Set.new
+ @message_handlers.merge(messages_with_methods)
end
+ # Dispatches reactions to helper methods based on name.
+ # See BehaviorDefinition to see how to override this.
+ #
+ # @see BehaviorDefinition
+ # @see Actor#react_to
def react_to(message_type, *opts, &blk)
- # TODO perf analysis, should I use a hash here?
if @message_handlers && @message_handlers.include?(message_type)
send message_type, *opts, &blk
end
end
+ # Builds and adds a behavior based on the name and options passed in.
+ #
+ # actor.input.when :shields_up do
+ # add_behavior :invincible, duration: actor.shield_charge
+ # end
+ #
def add_behavior(behavior_name, opts = {})
behavior_factory.add_behavior actor, behavior_name, opts
end
+ # Removes the behavior by name. Any added attributes will remain on the actor.
+ #
+ # actor.input.when :shields_down do
+ # remove_behavior :invincible
+ # end
+ #
def remove_behavior(behavior_name)
actor.remove_behavior(behavior_name)
end
class << self