class Chione::Aspect

An expression of component-matching criteria used to find entities that should be processed by a System.

Attributes

all_of[R]

The Set of component types which matching entities must have all of.

none_of[R]

The Set of component types which matching entities must not have any of.

one_of[R]

The Set of component types which matching entities must have at least one of.

Public Class Methods

for_archetype( archetype ) click to toggle source

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.

# File lib/chione/aspect.rb, line 44
def self::for_archetype( archetype )
        return archetype.from_aspect if archetype.from_aspect
        return self.new.with_all_of( archetype.components.keys )
end
new( one_of: [], all_of: [], none_of: [] ) click to toggle source

Create a new empty Aspect

# File lib/chione/aspect.rb, line 51
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
with_all_of( *component_types ) click to toggle source

Return a new Aspect that will match entities with all of the specified component_types.

# File lib/chione/aspect.rb, line 29
def self::with_all_of( *component_types )
        return self.new( all_of: component_types.flatten )
end
with_none_of( *component_types ) click to toggle source

Return a new Aspect that will match entities with none of the specified component_types.

# File lib/chione/aspect.rb, line 36
def self::with_none_of( *component_types )
        return self.new( none_of: component_types.flatten )
end
with_one_of( *component_types ) click to toggle source

Return a new Aspect that will match entities with at least one of the specified component_types.

# File lib/chione/aspect.rb, line 22
def self::with_one_of( *component_types )
        return self.new( one_of: component_types.flatten )
end

Public Instance Methods

and_all_of( *component_types )
Alias for: with_all_of
and_none_of( *component_types )
Alias for: with_none_of
and_one_of( *component_types )
Alias for: with_one_of
archetype() click to toggle source

Return an (anonymous) Chione::Archetype module that can be used to create entities that match it.

# File lib/chione/aspect.rb, line 147
def archetype
        return Chione::Archetype.from_aspect( self )
end
empty?() click to toggle source

Returns true if the receiver is an empty aspect, i.e., matches all entities.

# File lib/chione/aspect.rb, line 106
def empty?
        return self.one_of.empty? && self.all_of.empty? && self.none_of.empty?
end
initialize_copy( other ) click to toggle source

Copy constructor.

Calls superclass method
# File lib/chione/aspect.rb, line 59
def initialize_copy( other )
        super
        @one_of  = @one_of.dup
        @all_of  = @all_of.dup
        @none_of = @none_of.dup
end
match( component_hash )
Alias for: matches?
matches?( component_hash ) click to toggle source

Returns true if the components contained in the specified component_hash match the Aspect’s specifications.

# File lib/chione/aspect.rb, line 113
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
Also aliased as: match
matching_entities( entity_hash ) click to toggle source

Given an entity_hash keyed by Component class, return the subset of values matching the receiving Aspect.

# File lib/chione/aspect.rb, line 130
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 ) click to toggle source

Return a dup of this Aspect that also requires that matching entities have all of the given component_types.

# File lib/chione/aspect.rb, line 91
def with_all_of( *component_types )
        return self.dup_with( all_of: component_types.flatten )
end
Also aliased as: and_all_of
with_none_of( *component_types ) click to toggle source

Return a dup of this Aspect that also requires that matching entities have none of the given component_types.

# File lib/chione/aspect.rb, line 99
def with_none_of( *component_types )
        return self.dup_with( none_of: component_types.flatten )
end
Also aliased as: and_none_of
with_one_of( *component_types ) click to toggle source

Return a dup of this Aspect that also requires that matching entities have at least one of the given component_types.

# File lib/chione/aspect.rb, line 83
def with_one_of( *component_types )
        return self.dup_with( one_of: component_types.flatten )
end
Also aliased as: and_one_of

Protected Instance Methods

all_of_description() click to toggle source

Return a String describing the components matching entities must have all of.

# File lib/chione/aspect.rb, line 184
def all_of_description
        return nil if self.all_of.empty?
        return " with all of: %s" % [ self.all_of.map(&:name).join(', ') ]
end
dup_with( one_of: [], all_of: [], none_of: [] ) click to toggle source

Return a copy of the receiver with the specified additional required and excluded components.

# File lib/chione/aspect.rb, line 199
def dup_with( one_of: [], all_of: [], none_of: [] )
        self.log.debug "Making dup of %p with one_of: %p, all_of: %p, none_of: %p" %
                [ self, one_of, all_of, none_of ]

        copy = self.dup
        copy.one_of.merge( one_of )
        copy.all_of.merge( all_of )
        copy.none_of.merge( none_of )

        self.log.debug "  dup is: %p" % [ copy ]
        return copy
end
inspect_details() click to toggle source

Return the detail part of the inspect output.

# File lib/chione/aspect.rb, line 157
def inspect_details
        parts = []
        parts << self.one_of_description
        parts << self.all_of_description
        parts << self.none_of_description
        parts.compact!

        str = "matching entities"
        if parts.empty?
                str << " with any components"
        else
                str << parts.join( ', ' )
        end

        return str
end
none_of_description() click to toggle source

Return a String describing the components matching entities must not have any of.

# File lib/chione/aspect.rb, line 191
def none_of_description
        return nil if self.none_of.empty?
        return " with none of: %s" % [ self.none_of.map(&:name).join(', ') ]
end
one_of_description() click to toggle source

Return a String describing the components matching entities must have at least one of.

# File lib/chione/aspect.rb, line 177
def one_of_description
        return nil if self.one_of.empty?
        return " with at least one of: %s" % [ self.one_of.map(&:name).join(', ') ]
end