Chione::
Archetype
module
An Archetype mixin for defining factories for common entity configurations.
- components RW
The Hash of component types and initialization values to add to entities constructed by this Archetype.
Extension callback – add archetype functionality to an extended object.
23 def self::extended( object )
24 object.extend( Loggability )
25
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
Create an anonymous Archetype Module that will create entities which match the specified aspect (Chione::Aspect).
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
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.
71 def add( component_type, *init_args )
72 self.components[ component_type ] = init_args
73 end
Construct a new entity for the specified world with all of the archetype’s components.
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
Inclusion callback – add the components from this archetype to those in the specified mod.
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
Return a human-readable representation of the object suitable for debugging.
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
Provide details about the Archetype for #inspect output.
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