module Peregrine::Collections::Composite

Provides methods for filtering Entity objects contained in a collection. This module is intended to be an extension to existing collection instances.

Public Instance Methods

with_any_component(*list) { |entity| ... } click to toggle source

Returns an array of all Entities in the collection which include any of the given Component classes. Yields the matching Entity instances if a block is given.

# File lib/peregrine/collections/composite.rb, line 23
def with_any_component(*list)
  entities = select do |item|
    next unless item.respond_to?(:component_classes)
    !(item.component_classes & list).empty?
  end
  entities.each { |entity| yield entity } if block_given?
  entities.extend(Collections)
end
with_component(*list)
Alias for: with_components
with_components(*list) { |entity| ... } click to toggle source

Returns an array of all Entities in the collection which include all of the given Component classes. Yields the matching Entity instances if a block is given.

# File lib/peregrine/collections/composite.rb, line 10
def with_components(*list)
  entities = select do |item|
    next unless item.respond_to?(:component_classes)
    list.all? { |component| item.component_classes.include?(component) }
  end
  entities.each { |entity| yield entity } if block_given?
  entities.extend(Collections)
end
Also aliased as: with_component