Module: Chione::Archetype

Extended by:
Loggability, Pluggability
Defined in:
lib/chione/archetype.rb

Overview

An Archetype mixin for defining factories for common entity configurations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#componentsObject

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



65
66
67
# File 'lib/chione/archetype.rb', line 65

def components
  @components
end

Class Method Details

.extended(object) ⇒ Object

Extension callback – add archetype functionality to an extended object.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chione/archetype.rb', line 23

def self::extended( object )
	object.extend( Loggability )
	# object.extend( Chione::Inspection )
	object.extend( Chione::MethodUtilities )

	super

	object.log_to( :chione )
	object.components ||= {}
	object.singleton_attr_accessor :from_aspect
end

.from_aspect(aspect) ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/chione/archetype.rb', line 38

def self::from_aspect( aspect )
	mod = Module.new
	mod.extend( self )
	mod.from_aspect = aspect

	aspect.all_of.each( &mod.method(:add) )
	mod.add( aspect.one_of.first ) unless aspect.one_of.empty?

	return mod
end

Instance Method Details

#add(component_type, *init_args) ⇒ Object

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
72
73
# File 'lib/chione/archetype.rb', line 71

def add( component_type, *init_args )
	self.components[ component_type ] = init_args
end

#construct_for(world) ⇒ Object

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



78
79
80
81
82
83
84
85
86
# File 'lib/chione/archetype.rb', line 78

def construct_for( world )
	entity = world.create_blank_entity
	self.components.each do |component_type, args|
		component = component_type.new( *args )
		world.add_component_to( entity, component )
	end

	return entity
end

#included(mod) ⇒ Object

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



52
53
54
55
56
57
58
59
# File 'lib/chione/archetype.rb', line 52

def included( mod )
	super
	self.log.debug "Including %d components in %p" % [ self.components.length, mod ]
	self.components.each do |component_type, args|
		self.log.debug "Adding %p to %p from %p" % [ component_type, mod, self ]
		mod.add( component_type, *args )
	end
end

#inspectObject

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



90
91
92
93
94
95
96
# File 'lib/chione/archetype.rb', line 90

def inspect
	return "#<%p:%#016x %s>" % [
		self.class,
		self.object_id * 2,
		self.inspect_details,
	]
end