Module: Slickr::EntityManager

Extended by:
EntityManager
Included in:
EntityManager
Defined in:
lib/slickr/entity_manager.rb

Overview

Provides a central repository for storing and querying for Entities that exhibit some behavior.

Each scene of your game will probably have many entities. When you instantiate one of those Entities, it’s automatically registered to the EntityManager, with the behaviors it implements.

Systems and Renderers are the two things that will likely need to alter or manipulate entities. Systems change the data associated with an entity and Renderers draw that to the screen. These objects can ask the EntityManager for any entities that care about the behaviors they affect.

The Input system cares about entities that can be moved by keyboard or mouse input and have spatial awareness; that is, things that can move. The above System tells an entity to move up if the up key is pressed. What “moving up” means is determined by the component. The System doesn’t care; it just reacts to events in the world and tells entities to do stuff based on that.

It’s important to view :controllable and :spatial as behaviors. Systems and Renderers expect these objects to conform to some standard API. We don’t care where the object came from, what type it is or what it does outside of what we expect. We’re going for good object oriented design through duck typing here.

Examples:


class Boss < Slickr::Entity
  use Spatial, x: 0, y: 0
end

boss = Boss.new
# => #<Boss components=[:spatial]>

EntityManager.entities
# => [#<Boss components=[:spatial]>]

class Systems::Input
  def call(delta)
    entities.each { |e| e.up(delta) } if up_key?
  end

  def entities
    EntityManager.entities_with(:controllable, :spatial)
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entitiesObject



58
59
60
# File 'lib/slickr/entity_manager.rb', line 58

def entities
  @entities ||= reset!
end

Instance Method Details

#entities_with(*components) ⇒ Object

Retrieve all registered entities that include all of the components passed.

Examples:

EntityManager.entities
=> { #<Entity components=[:spatial, :size]>, #<Entity components=[:spatial]> }

EntityManager.entities_with(:spatial, :size)
=> #<Entity components=[:spatial, :size]>

Parameters:

  • components (Array<Component>)

    List of components



84
85
86
# File 'lib/slickr/entity_manager.rb', line 84

def entities_with(*components)
  entities.select { |entity, types| includes_all?(types, Set[*components]) }.keys
end

#register(entity, type) ⇒ Object

Register an Entity as conforming to a specific Component behavior.

Parameters:

  • entity (Entity)

    The Entity that includes the behavior

  • type (Module)

    A Component module.



68
69
70
# File 'lib/slickr/entity_manager.rb', line 68

def register(entity, type)
  entities[entity] << symbolize(type)
end

#reset!Object

Remove all entities.



90
91
92
# File 'lib/slickr/entity_manager.rb', line 90

def reset!
  @entities = Hash.new { |h,k| h[k] = Set.new }
end