Sha256: d6b71efea7e3ca11ce439975a9656482ab7b81f3c17a9b9e5634ae2947da325b

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

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

  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

  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

2 entries across 2 versions & 1 rubygems

Version Path
gamebox-0.4.0.rc2 lib/gamebox/core/behavior.rb
gamebox-0.4.0.rc1 lib/gamebox/core/behavior.rb