Sha256: 404ae08f5f2cd62796c37e005a5644ad88830d40f3668608496873de260934ba

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

# ActorViewFactory is responsible for loading all ActorViews. It creates the ActorView 
# associated with the Actor and registers it to the Stage be drawn. 
class ActorViewFactory
  construct_with :behavior_factory

  # builds a new ActorView and configures it per its definition that is setup via ActorView.define
  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

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gamebox-0.5.5 lib/gamebox/core/actor_view_factory.rb
gamebox-0.5.4 lib/gamebox/core/actor_view_factory.rb
gamebox-0.5.2 lib/gamebox/core/actor_view_factory.rb
gamebox-0.5.1 lib/gamebox/core/actor_view_factory.rb
gamebox-0.5.0 lib/gamebox/core/actor_view_factory.rb
gamebox-0.4.1 lib/gamebox/core/actor_view_factory.rb
gamebox-0.4.0 lib/gamebox/core/actor_view_factory.rb
gamebox-0.4.0.rc11 lib/gamebox/core/actor_view_factory.rb