Sha256: 430f0354400a71603dc9b4576e9337f90dedbb63642f1ff1a3a238b4b0f34f07

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

# ActorFactory is responsible for loading all Actors. It also creates the ActorView 
# associated with the Actor and registers it to the Stage be drawn. 
class ActorFactory
  construct_with :input_manager, :wrapped_screen, :resource_manager,
    :behavior_factory, :actor_view_factory, :this_object_context

  # returns the newly created Actor after it and its ActorView has been created.
  def build(actor, opts={})
    merged_opts = opts.merge(actor_type: actor)

    model = nil
    this_object_context.in_subcontext do |actor_context|
      begin
        model = actor_context[:actor]

        actor_definition = Actor.definitions[actor]
        raise "#{actor} not found in Actor.definitions" if actor_definition.nil?
        model.configure(merged_opts)

        actor_definition.attributes.each do |attr|
          model.has_attributes attr
        end

        actor_definition.behaviors.each do |behavior|
          beh_opts = {}
          beh_key = behavior

          if behavior.is_a?(Hash)
            beh_opts = behavior.values.first
            beh_key = behavior.keys.first
          end

          behavior_factory.add_behavior(model, beh_key, beh_opts)
        end

        actor_view_factory.build model, opts
      rescue Exception => e
        # binding.pry
        raise """
          #{actor} not found: 
          #{e.inspect}
          #{e.backtrace[0..6].join("\n")}
        """
      end
    end
    model
  end

  def class_ancestors(actor)
    klass = actor.class
    [].tap { |actor_klasses|
      while klass != Actor
        actor_klasses << klass
        klass = klass.superclass
      end
    }
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gamebox-0.4.1 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0.rc11 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0.rc5 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0.rc4 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0.rc3 lib/gamebox/core/actor_factory.rb
gamebox-0.4.0.rc2 lib/gamebox/core/actor_factory.rb