Class: Draco::World::EntityStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/draco.rb

Overview

Internal: Stores Entities with better performance than Array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, *entities) ⇒ EntityStore

Internal: Initializes a new EntityStore

entities - The Entities to add to the EntityStore



698
699
700
701
702
703
704
705
# File 'lib/draco.rb', line 698

def initialize(parent, *entities)
  @parent = parent
  @entity_to_components = Hash.new { |hash, key| hash[key] = Set.new }
  @component_to_entities = Hash.new { |hash, key| hash[key] = Set.new }
  @entity_ids = {}

  self << entities
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



693
694
695
# File 'lib/draco.rb', line 693

def parent
  @parent
end

Instance Method Details

#<<(entities) ⇒ Object

Internal: Adds Entities to the EntityStore

entities - The Entity or Array list of Entities to add to the EntityStore.

Returns the EntityStore



737
738
739
740
# File 'lib/draco.rb', line 737

def <<(entities)
  Array(entities).flatten.each { |e| add(e) }
  self
end

#[](*components_or_ids) ⇒ Object

Internal: Gets all Entities that implement all of the given Components or that match the given entity ids.

components_or_ids - The Component Classes to filter by

Returns a Set list of Entities



712
713
714
715
716
717
# File 'lib/draco.rb', line 712

def [](*components_or_ids)
  components_or_ids
    .flatten
    .map { |component_or_id| select_entities(component_or_id) }
    .reduce { |acc, i| i & acc }
end

#add(entity) ⇒ Object

Internal: Adds an Entity to the EntityStore.

entity - The Entity to add to the EntityStore.

Returns the EntityStore



747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/draco.rb', line 747

def add(entity)
  entity.subscribe(self)

  @entity_ids[entity.id] = entity
  components = entity.components.map(&:class)
  @entity_to_components[entity].merge(components)

  components.each { |component| @component_to_entities[component].add(entity) }
  entity.components.each { |component| @parent.component_added(entity, component) }

  self
end

#component_added(entity, component) ⇒ Object

Internal: Updates the EntityStore when an Entity’s Components are added.

entity - The Entity the Component was added to. component - The Component that was added to the Entity.

Returns nothing.



790
791
792
793
# File 'lib/draco.rb', line 790

def component_added(entity, component)
  @component_to_entities[component.class].add(entity)
  @parent.component_added(entity, component)
end

#component_removed(entity, component) ⇒ Object

Internal: Updates the EntityStore when an Entity’s Components are removed.

entity - The Entity the Component was removed from. component - The Component that was removed from the Entity.

Returns nothing.



801
802
803
804
# File 'lib/draco.rb', line 801

def component_removed(entity, component)
  @component_to_entities[component.class].delete(entity)
  @parent.component_removed(entity, component)
end

#delete(entity) ⇒ Object

Internal: Removes an Entity from the EntityStore.

entity - The Entity to remove from the EntityStore.

Returns the EntityStore



765
766
767
768
769
770
771
772
# File 'lib/draco.rb', line 765

def delete(entity)
  @entity_ids.delete(entity.id)
  components = Array(@entity_to_components.delete(entity))

  components.each do |component|
    @component_to_entities[component].delete(entity)
  end
end

#each(&block) ⇒ Object

Internal: Returns an Enumerator for all of the Entities.



780
781
782
# File 'lib/draco.rb', line 780

def each(&block)
  @entity_to_components.keys.each(&block)
end

#empty?Boolean

Internal: Returns true if the EntityStore has no Entities.

Returns:

  • (Boolean)


775
776
777
# File 'lib/draco.rb', line 775

def empty?
  @entity_to_components.empty?
end

#select_entities(component_or_id) ⇒ Object

Internal: Gets entities by component or id.

component_or_id - The Component Class or entity id to select.

Returns an Array of Entities.



724
725
726
727
728
729
730
# File 'lib/draco.rb', line 724

def select_entities(component_or_id)
  if component_or_id.is_a?(Numeric)
    Array(@entity_ids[component_or_id])
  else
    @component_to_entities[component_or_id]
  end
end