Class: ActorFactory

Inherits:
Object show all
Defined in:
lib/gamebox/core/actor_factory.rb

Overview

ActorFactory is responsible for loading all Actors. It also creates the ActorView associated with the Actor and registers it to the Stage be drawn.

Instance Method Summary collapse

Instance Method Details

#build(actor, opts = {}) ⇒ Object

returns the newly created Actor after it and its ActorView has been created.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gamebox/core/actor_factory.rb', line 9

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

      model.has_attributes(view: "#{actor}_view") if actor_definition.view_blk

      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

#class_ancestors(actor) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/gamebox/core/actor_factory.rb', line 52

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