Module: Slickr::EntityManager
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.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#entities_with(*components) ⇒ Object
Retrieve all registered entities that include all of the components passed.
-
#register(entity, type) ⇒ Object
Register an
Entityas conforming to a specificComponentbehavior. -
#reset! ⇒ Object
Remove all entities.
Instance Attribute Details
#entities ⇒ Object
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.
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.
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 |