Sha256: 413624643494385e857217af5cc47bcf23104ba93cc08ed2ecea557ba196e66f

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 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)
    @message_handlers = messages_with_methods
  end

  def react_to(message_type, *opts)
    # TODO perf analysis, should I use a hash here?
    if @message_handlers && @message_handlers.include?(message_type)
      send message_type, *opts
    end
  end

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

  class << self

    def define(behavior_type, &blk)
      @definitions ||= {}
      definition = BehaviorDefinition.new
      definition.instance_eval &blk if block_given?
      @definitions[behavior_type] = definition
    end

    def definitions
      @definitions ||= {}
    end
  end

  class BehaviorDefinition
    attr_accessor :setup_block, :required_injections, :react_to_block, :required_behaviors,
      :helpers_block

    def requires(*injections_needed)
      @required_injections = injections_needed
    end

    def requires_behaviors(*behaviors_needed)
      @required_behaviors = behaviors_needed
    end

    def setup(&setup_block)
      @setup_block = setup_block
    end

    def react_to(&react_to_block)
      @react_to_block = react_to_block
    end

    def helpers(&helpers_block)
      @helpers_block = helpers_block
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gamebox-0.4.0.rc5 lib/gamebox/core/behavior.rb
gamebox-0.4.0.rc4 lib/gamebox/core/behavior.rb
gamebox-0.4.0.rc3 lib/gamebox/core/behavior.rb