Class: ActorViewFactory

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

Overview

ActorViewFactory is responsible for loading all ActorViews. It 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

builds a new ActorView and configures it per its definition that is setup via ActorView.define



8
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
# File 'lib/gamebox/core/actor_view_factory.rb', line 8

def build(actor, opts={})
  # TODO  have animated, graphical, physical set a view attr on actor
  view_klass = opts[:view] || actor.do_or_do_not(:view) || "#{actor.actor_type}_view"

  view_definition = ActorView.definitions[view_klass.to_sym]
  view = nil
  if view_definition
    actor_context = actor.this_object_context
    view = actor_context[:actor_view]
    reqs = view_definition.required_injections
    if reqs
      reqs.each do |req|
        object = actor_context[req]
        view.define_singleton_method req do
          components[req] 
        end
        components = view.send :components
        components[req] = object
      end
    end

    view.define_singleton_method :draw, &view_definition.draw_block if view_definition.draw_block
    if view_definition.setup_block
      view.define_singleton_method :setup, &view_definition.setup_block 
      view.setup
    end

    helpers = view_definition.helpers_block
    if helpers
      helpers_module = Module.new &helpers
      view.extend helpers_module
    end

    behavior_factory.add_behavior(actor, :visible, view: view)
    actor.react_to :show unless opts[:hide]
  end
  view
end