Class: Chione::Aspect
- Inherits:
-
Object
- Object
- Chione::Aspect
- Extended by:
- Loggability
- Includes:
- Inspection
- Defined in:
- lib/chione/aspect.rb
Overview
An expression of component-matching criteria used to find entities that should be processed by a System.
Instance Attribute Summary collapse
-
#all_of ⇒ Object
readonly
The Set of component types which matching entities must have all of.
-
#none_of ⇒ Object
readonly
The Set of component types which matching entities must not have any of.
-
#one_of ⇒ Object
readonly
The Set of component types which matching entities must have at least one of.
Class Method Summary collapse
-
.for_archetype(archetype) ⇒ Object
Create a new Aspect that will match the given
archetype, unless the given archetype was created from an existing Aspect, in which case just returns that. -
.with_all_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with all of the specified
component_types. -
.with_none_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with none of the specified
component_types. -
.with_one_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with at least one of the specified
component_types.
Instance Method Summary collapse
-
#archetype ⇒ Object
Return an (anonymous) Chione::Archetype module that can be used to create entities that match it.
-
#empty? ⇒ Boolean
Returns true if the receiver is an empty aspect, i.e., matches all entities.
-
#initialize(one_of: [], all_of: [], none_of: []) ⇒ Aspect
constructor
Create a new empty Aspect.
-
#initialize_copy(other) ⇒ Object
Copy constructor.
-
#matches?(component_hash) ⇒ Boolean
(also: #match)
Returns
trueif the components contained in the specifiedcomponent_hashmatch the Aspect’s specifications. -
#matching_entities(entity_hash) ⇒ Object
Given an
entity_hashkeyed by Component class, return the subset of values matching the receiving Aspect. -
#with_all_of(*component_types) ⇒ Object
(also: #and_all_of)
Return a dup of this Aspect that also requires that matching entities have all of the given
component_types. -
#with_none_of(*component_types) ⇒ Object
(also: #and_none_of)
Return a dup of this Aspect that also requires that matching entities have none of the given
component_types. -
#with_one_of(*component_types) ⇒ Object
(also: #and_one_of)
Return a dup of this Aspect that also requires that matching entities have at least one of the given
component_types.
Methods included from Inspection
Constructor Details
#initialize(one_of: [], all_of: [], none_of: []) ⇒ Aspect
Create a new empty Aspect
52 53 54 55 56 |
# File 'lib/chione/aspect.rb', line 52 def initialize( one_of: [], all_of: [], none_of: [] ) @one_of = Set.new( one_of ) @all_of = Set.new( all_of ) @none_of = Set.new( none_of ) end |
Instance Attribute Details
#all_of ⇒ Object (readonly)
The Set of component types which matching entities must have all of.
76 77 78 |
# File 'lib/chione/aspect.rb', line 76 def all_of @all_of end |
#none_of ⇒ Object (readonly)
The Set of component types which matching entities must not have any of.
79 80 81 |
# File 'lib/chione/aspect.rb', line 79 def none_of @none_of end |
#one_of ⇒ Object (readonly)
The Set of component types which matching entities must have at least one of.
73 74 75 |
# File 'lib/chione/aspect.rb', line 73 def one_of @one_of end |
Class Method Details
.for_archetype(archetype) ⇒ Object
Create a new Aspect that will match the given archetype, unless the given archetype was created from an existing Aspect, in which case just returns that.
45 46 47 48 |
# File 'lib/chione/aspect.rb', line 45 def self::for_archetype( archetype ) return archetype.from_aspect if archetype.from_aspect return self.new.with_all_of( archetype.components.keys ) end |
.with_all_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with all of the specified component_types.
30 31 32 |
# File 'lib/chione/aspect.rb', line 30 def self::with_all_of( *component_types ) return self.new( all_of: component_types.flatten ) end |
.with_none_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with none of the specified component_types.
37 38 39 |
# File 'lib/chione/aspect.rb', line 37 def self::with_none_of( *component_types ) return self.new( none_of: component_types.flatten ) end |
.with_one_of(*component_types) ⇒ Object
Return a new Aspect that will match entities with at least one of the specified component_types.
23 24 25 |
# File 'lib/chione/aspect.rb', line 23 def self::with_one_of( *component_types ) return self.new( one_of: component_types.flatten ) end |
Instance Method Details
#archetype ⇒ Object
Return an (anonymous) Chione::Archetype module that can be used to create entities that match it.
148 149 150 |
# File 'lib/chione/aspect.rb', line 148 def archetype return Chione::Archetype.from_aspect( self ) end |
#empty? ⇒ Boolean
Returns true if the receiver is an empty aspect, i.e., matches all entities.
107 108 109 |
# File 'lib/chione/aspect.rb', line 107 def empty? return self.one_of.empty? && self.all_of.empty? && self.none_of.empty? end |
#initialize_copy(other) ⇒ Object
Copy constructor.
60 61 62 63 64 65 |
# File 'lib/chione/aspect.rb', line 60 def initialize_copy( other ) super @one_of = @one_of.dup @all_of = @all_of.dup @none_of = @none_of.dup end |
#matches?(component_hash) ⇒ Boolean Also known as: match
Returns true if the components contained in the specified component_hash match the Aspect’s specifications.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/chione/aspect.rb', line 114 def matches?( component_hash ) return true if self.empty? component_hash = component_hash.components if component_hash.respond_to?( :components ) return false unless self.one_of.empty? || self.one_of.any? {|component| component_hash.key?(component) } return false unless self.none_of.none? {|component| component_hash.key?(component) } return false unless self.all_of.all? {|component| component_hash.key?(component) } return true end |
#matching_entities(entity_hash) ⇒ Object
Given an entity_hash keyed by Component class, return the subset of values matching the receiving Aspect.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/chione/aspect.rb', line 131 def matching_entities( entity_hash ) initial_set = if self.one_of.empty? entity_hash.values else entity_hash.values_at( *self.one_of ) end with_one = initial_set.reduce( :| ) || Set.new with_all = entity_hash.values_at( *self.all_of ).reduce( with_one, :& ) without_any = entity_hash.values_at( *self.none_of ).reduce( with_all, :- ) return without_any end |
#with_all_of(*component_types) ⇒ Object Also known as: and_all_of
Return a dup of this Aspect that also requires that matching entities have all of the given component_types.
92 93 94 |
# File 'lib/chione/aspect.rb', line 92 def with_all_of( *component_types ) return self.dup_with( all_of: component_types.flatten ) end |
#with_none_of(*component_types) ⇒ Object Also known as: and_none_of
Return a dup of this Aspect that also requires that matching entities have none of the given component_types.
100 101 102 |
# File 'lib/chione/aspect.rb', line 100 def with_none_of( *component_types ) return self.dup_with( none_of: component_types.flatten ) end |
#with_one_of(*component_types) ⇒ Object Also known as: and_one_of
Return a dup of this Aspect that also requires that matching entities have at least one of the given component_types.
84 85 86 |
# File 'lib/chione/aspect.rb', line 84 def with_one_of( *component_types ) return self.dup_with( one_of: component_types.flatten ) end |