Chione::

Archetype module

An Archetype mixin for defining factories for common entity configurations.

Attributes

components RW

The Hash of component types and initialization values to add to entities constructed by this Archetype.

Public Class Methods

extended( object )

Extension callback – add archetype functionality to an extended object.

   # File lib/chione/archetype.rb
22 def self::extended( object )
23     object.extend( Loggability )
24     # object.extend( Chione::Inspection )
25     object.extend( Chione::MethodUtilities )
26 
27     super
28 
29     object.log_to( :chione )
30     object.components ||= {}
31     object.singleton_attr_accessor :from_aspect
32 end
from_aspect( aspect )

Create an anonymous Archetype Module that will create entities which match the specified aspect (Chione::Aspect).

   # File lib/chione/archetype.rb
37 def self::from_aspect( aspect )
38     mod = Module.new
39     mod.extend( self )
40     mod.from_aspect = aspect
41 
42     aspect.all_of.each( &mod.method(:add) )
43     mod.add( aspect.one_of.first ) unless aspect.one_of.empty?
44 
45     return mod
46 end

Public Instance Methods

add( component_type, *init_args )

Add a component_type to the list used when constructing a new entity from the current Archetype. The component will be instantiated using the specified init_args.

   # File lib/chione/archetype.rb
70 def add( component_type, *init_args )
71     self.components[ component_type ] = init_args
72 end
construct_for( world )

Construct a new entity for the specified world with all of the archetype’s components.

   # File lib/chione/archetype.rb
77 def construct_for( world )
78     entity = world.create_blank_entity
79     self.components.each do |component_type, args|
80         component = component_type.new( *args )
81         world.add_component_to( entity, component )
82     end
83 
84     return entity
85 end
included( mod )

Inclusion callback – add the components from this archetype to those in the specified mod.

   # File lib/chione/archetype.rb
51 def included( mod )
52     super
53     self.log.debug "Including %d components in %p" % [ self.components.length, mod ]
54     self.components.each do |component_type, args|
55         self.log.debug "Adding %p to %p from %p" % [ component_type, mod, self ]
56         mod.add( component_type, *args )
57     end
58 end
inspect()

Return a human-readable representation of the object suitable for debugging.

   # File lib/chione/archetype.rb
89 def inspect
90     return "#<%p:%#016x %s>" % [
91         self.class,
92         self.object_id * 2,
93         self.inspect_details,
94     ]
95 end

Protected Instance Methods

inspect_details()

Provide details about the Archetype for #inspect output.

    # File lib/chione/archetype.rb
103 def inspect_details
104     if self.from_aspect
105         return "Chione::Archetype from %p" % [ self.from_aspect ]
106     elsif !self.components.empty?
107         return "Chione::Archetype for creating entities with %p" % [ self.components.keys ]
108     else
109         return "blank Chione::Archetype"
110     end
111 end