Class: Darkholme::Entity
- Inherits:
-
Object
- Object
- Darkholme::Entity
- Defined in:
- lib/darkholme/entity.rb
Overview
An Entity contains Components, which hold data which is manipulated by a System
Instance Attribute Summary collapse
-
#component_bits ⇒ Object
Returns the value of attribute component_bits.
-
#components ⇒ Object
Returns the value of attribute components.
-
#engine ⇒ Object
Returns the value of attribute engine.
-
#family_bits ⇒ Object
Returns the value of attribute family_bits.
Class Method Summary collapse
-
.load(source) ⇒ Entity
Create a new Entity from a JSON manifest.
Instance Method Summary collapse
-
#add_component(component) ⇒ Component
Add a component to an entity.
-
#added_to_engine(engine) ⇒ Object
Callback called after the entity has been added to an Engine.
-
#component_for(component_class) ⇒ Component
Retrieve a component of a certain class from an entity.
-
#has_component?(component_class) ⇒ Boolean
Check to see if an entity contains a type of component.
-
#initialize ⇒ Entity
constructor
Create a new Entity.
-
#remove_component(component_class) ⇒ Component
Remove a component from an entity.
-
#removed_from_engine(engine) ⇒ Object
Callback called after the engine has been removed from an Engine.
Constructor Details
#initialize ⇒ Entity
Create a new Entity
30 31 32 33 34 35 |
# File 'lib/darkholme/entity.rb', line 30 def initialize self.components = {} self.component_bits = Bitset.new self.family_bits = Bitset.new self.engine = nil end |
Instance Attribute Details
#component_bits ⇒ Object
Returns the value of attribute component_bits.
5 6 7 |
# File 'lib/darkholme/entity.rb', line 5 def component_bits @component_bits end |
#components ⇒ Object
Returns the value of attribute components.
5 6 7 |
# File 'lib/darkholme/entity.rb', line 5 def components @components end |
#engine ⇒ Object
Returns the value of attribute engine.
5 6 7 |
# File 'lib/darkholme/entity.rb', line 5 def engine @engine end |
#family_bits ⇒ Object
Returns the value of attribute family_bits.
5 6 7 |
# File 'lib/darkholme/entity.rb', line 5 def family_bits @family_bits end |
Class Method Details
.load(source) ⇒ Entity
Create a new Entity from a JSON manifest
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/darkholme/entity.rb', line 12 def self.load(source) data = MultiJson.load(source) components = [] data["components"].each do |component_class, data| components << class_from_string(component_class).from_manifest(data) end entity = new components.each do |component| entity.add_component(component) end entity end |
Instance Method Details
#add_component(component) ⇒ Component
Add a component to an entity
56 57 58 59 60 61 62 |
# File 'lib/darkholme/entity.rb', line 56 def add_component(component) self.components[component.class] = component self.component_bits.set(component.bit) self.engine.component_added(self, component) if self.engine component end |
#added_to_engine(engine) ⇒ Object
Callback called after the entity has been added to an Engine
40 41 42 |
# File 'lib/darkholme/entity.rb', line 40 def added_to_engine(engine) self.engine = engine end |
#component_for(component_class) ⇒ Component
Retrieve a component of a certain class from an entity
93 94 95 |
# File 'lib/darkholme/entity.rb', line 93 def component_for(component_class) self.components[component_class] end |
#has_component?(component_class) ⇒ Boolean
Check to see if an entity contains a type of component
84 85 86 |
# File 'lib/darkholme/entity.rb', line 84 def has_component?(component_class) self.components.include? component_class end |
#remove_component(component_class) ⇒ Component
Remove a component from an entity
69 70 71 72 73 74 75 76 77 |
# File 'lib/darkholme/entity.rb', line 69 def remove_component(component_class) if removed_component = component_for(component_class) self.component_bits.clear(removed_component.bit) self.components.delete(component_class) self.engine.component_removed(self, removed_component) if self.engine end removed_component end |
#removed_from_engine(engine) ⇒ Object
Callback called after the engine has been removed from an Engine
47 48 49 |
# File 'lib/darkholme/entity.rb', line 47 def removed_from_engine(engine) self.engine = nil end |