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
23 def self::extended( object )
24     object.extend( Loggability )
25     # object.extend( Chione::Inspection )
26     object.extend( Chione::MethodUtilities )
27 
28     super
29 
30     object.log_to( :chione )
31     object.components ||= {}
32     object.singleton_attr_accessor :from_aspect
33 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
38 def self::from_aspect( aspect )
39     mod = Module.new
40     mod.extend( self )
41     mod.from_aspect = aspect
42 
43     aspect.all_of.each( &mod.method(:add) )
44     mod.add( aspect.one_of.first ) unless aspect.one_of.empty?
45 
46     return mod
47 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
71 def add( component_type, *init_args )
72     self.components[ component_type ] = init_args
73 end
construct_for( world )

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

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

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

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

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

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

Protected Instance Methods

inspect_details()

Provide details about the Archetype for #inspect output.

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