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.
22 def self::extended( object )
23 object.extend( Loggability )
24
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
Create an anonymous Archetype Module that will create entities which match the specified aspect (Chione::Aspect).
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
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.
70 def add( component_type, *init_args )
71 self.components[ component_type ] = init_args
72 end
Construct a new entity for the specified world with all of the archetype’s components.
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
Inclusion callback – add the components from this archetype to those in the specified mod.
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
Return a human-readable representation of the object suitable for debugging.
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
Provide details about the Archetype for #inspect output.
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