Sha256: a78b6ee33eee142046e6ecabeeda50c97741d804db36453d658ef8920b2580bd

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# Behavior is any type of behavior an actor can exibit.
class Behavior
  construct_with :actor, :behavior_factory

  attr_accessor :opts

  def configure(opts={})
    @opts = opts
    setup
  end

  def setup
  end

  def reacts_with(*messages_with_methods)
    # TODO do a merge here..
    @message_handlers = messages_with_methods
  end

  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

  def add_behavior(behavior_name, opts = {})
    behavior_factory.add_behavior actor, behavior_name, opts
  end

  def remove_behavior(behavior_name)
    actor.remove_behavior(behavior_name)
  end

  class << self

    def define(behavior_type, &blk)
      @definitions ||= {}
      definition = BehaviorDefinition.new
      definition.source = caller.detect{|c|!c.match /core/}
      definition.instance_eval &blk if block_given?
      @definitions[behavior_type] = definition
    end

    def definitions
      @definitions ||= {}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gamebox-0.4.0 lib/gamebox/core/behavior.rb
gamebox-0.4.0.rc11 lib/gamebox/core/behavior.rb