class FelFlame::Entities

Creates and manages Entities. Allows accessing Entities using their {FelFlame::Entities#id ID}. Entities are just collections of Components.

Attributes

id[RW]

Holds the unique ID of this entity @return [Integer]

Public Class Methods

[](entity_id) click to toggle source

Gets an Entity from the given {id unique ID}. Usage is simular to how an Array lookup works

@example

# This gets the Entity with ID 7
FelFlame::Entities[7]

@param entity_id [Integer] @return [Entity] returns the Entity that uses the given unique ID, nil if there is no Entity associated with the given ID

# File lib/felflame/entity_manager.rb, line 116
def [](entity_id)
  data[entity_id]
end
data() click to toggle source

@return [Array<Entity>] Array of all Entities that exist @!visibility private

# File lib/felflame/entity_manager.rb, line 105
def data
  @data ||= []
end
each(&block) click to toggle source

Iterates over all entities. The data is compacted so that means index does not correlate to ID. You also call other enumerable methods instead of each, such as each_with_index or select @return [Enumerator]

# File lib/felflame/entity_manager.rb, line 123
def each(&block)
  data.compact.each(&block)
end
new(*components) click to toggle source

Creating a new Entity @param components [Components] Can be any number of components, identical duplicates will be automatically purged however different components from the same component manager are allowed. @return [Entity]

# File lib/felflame/entity_manager.rb, line 16
def initialize(*components)
  # Assign new unique ID
  new_id = self.class.data.find_index(&:nil?)
  new_id = self.class.data.size if new_id.nil?
  self.id = new_id

  # Add each component
  add(*components)

  self.class.data[id] = self
end

Public Instance Methods

add(*components_to_add) click to toggle source

Add any number components to the Entity. @param components_to_add [Component] Any number of components created from any component manager @return [Boolean] true

# File lib/felflame/entity_manager.rb, line 57
def add(*components_to_add)
  components_to_add.each do |component|
    if components[component.class].nil?
      components[component.class] = [component]
      component.entities.push self
      check_systems component, :addition_triggers
    elsif !components[component.class].include? component
      components[component.class].push component
      component.entities.push self
      check_systems component, :addition_triggers
    end
  end
  true
end
check_systems(component, trigger_type) click to toggle source

triggers every system associated with this component's trigger @return [Boolean] true @!visibility private

# File lib/felflame/entity_manager.rb, line 75
def check_systems(component, trigger_type)
  component_calls = component.class.send(trigger_type)
  component.send(trigger_type).each do |system|
    component_calls |= [system]
  end
  component_calls.sort_by(&:priority).reverse.each(&:call)
  true
end
components() click to toggle source

A hash that uses component manager constant names as keys, and where the values of those keys are arrays that contain the {FelFlame::ComponentManager#id IDs} of the components attached to this entity. @return [Hash<Component_Manager, Array<Integer>>]

# File lib/felflame/entity_manager.rb, line 30
def components
  @components ||= {}
end
delete() click to toggle source

Removes this Entity from the list and purges all references to this Entity from other Components, as well as its {id ID} and data. @return [Boolean] true

# File lib/felflame/entity_manager.rb, line 42
def delete
  components.each do |component_manager, component_array|
    component_array.each do |component|
      component.entities.delete(self)
    end
  end
  FelFlame::Entities.data[id] = nil
  @components = {}
  @id = nil
  true
end
remove(*components_to_remove) click to toggle source

Remove a component from the Entity @param components_to_remove [Component] A component created from any component manager @return [Boolean] true

# File lib/felflame/entity_manager.rb, line 87
def remove(*components_to_remove)
  components_to_remove.each do |component|
    check_systems component, :removal_triggers if component.entities.include? self
    component.entities.delete self
    components[component.class].delete component
  end
  true
end
to_i() click to toggle source

An alias for the {#id ID reader} @return [Integer]

# File lib/felflame/entity_manager.rb, line 36
def to_i
  id
end