Class: Chione::Entity
- Inherits:
-
Object
- Object
- Chione::Entity
- Extended by:
- Deprecatable, Loggability
- Includes:
- Inspection
- Defined in:
- lib/chione/entity.rb
Overview
The Entity (identity) class
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
The Entity’s ID.
-
#world ⇒ Object
readonly
The World the Entity belongs to.
Class Method Summary collapse
-
.make_new_id ⇒ Object
Return an ID for an Entity.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Equality operator – returns
trueif the receiver andotherbelong to the same world, have the same ID, and have equal components. -
#add_component(component, **init_values) ⇒ Object
Add the specified
componentto the entity. -
#components ⇒ Object
Return the components that the entity’s World has registered for it as a Hash keyed by the Component class.
-
#get_component(component_class) ⇒ Object
Fetch the component of the specified
component_classthat corresponds with the receiving entity. -
#has_component?(component_class) ⇒ Boolean
Returns
trueif this entity has a component of the specifiedcomponent_class. -
#initialize(world, id = nil) ⇒ Entity
constructor
Create a new Entity for the specified
world, and use the specifiedidif given. -
#remove_component(component_class) ⇒ Object
Remove the component of the specified
component_classthat corresponds with the receiving entity.
Methods included from Inspection
Constructor Details
#initialize(world, id = nil) ⇒ Entity
Create a new Entity for the specified world, and use the specified id if given. If no id is given, one will be automatically generated.
29 30 31 32 |
# File 'lib/chione/entity.rb', line 29 def initialize( world, id=nil ) @world = world @id = id || self.class.make_new_id end |
Instance Attribute Details
#id ⇒ Object (readonly)
The Entity’s ID
40 41 42 |
# File 'lib/chione/entity.rb', line 40 def id @id end |
#world ⇒ Object (readonly)
The World the Entity belongs to
43 44 45 |
# File 'lib/chione/entity.rb', line 43 def world @world end |
Class Method Details
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Equality operator – returns true if the receiver and other belong to the same world, have the same ID, and have equal components.
84 85 86 87 |
# File 'lib/chione/entity.rb', line 84 def ==( other ) return other.instance_of?( self.class ) && self.id == other.id end |
#add_component(component, **init_values) ⇒ Object
Add the specified component to the entity. The component can be a subclass of Chione::Component, an instance of such a subclass, or the name of a subclass. It will replace any existing component of the same type.
56 57 58 |
# File 'lib/chione/entity.rb', line 56 def add_component( component, **init_values ) self.world.add_component_to( self, component, **init_values ) end |
#components ⇒ Object
Return the components that the entity’s World has registered for it as a Hash keyed by the Component class.
48 49 50 |
# File 'lib/chione/entity.rb', line 48 def components return self.world.components_for( self ) end |
#get_component(component_class) ⇒ Object
Fetch the component of the specified component_class that corresponds with the receiving entity. Returns nil if so much component exists.
63 64 65 |
# File 'lib/chione/entity.rb', line 63 def get_component( component_class ) return self.world.get_component_for( self, component_class ) end |
#has_component?(component_class) ⇒ Boolean
Returns true if this entity has a component of the specified component_class.
77 78 79 |
# File 'lib/chione/entity.rb', line 77 def has_component?( component_class ) return self.world.has_component_for?( self, component_class ) end |
#remove_component(component_class) ⇒ Object
Remove the component of the specified component_class that corresponds with the receiving entity. Returns the component instance if it was removed, or nil if no Component of the specified type was registered to the entity.
71 72 73 |
# File 'lib/chione/entity.rb', line 71 def remove_component( component_class ) return self.world.remove_component_from( self, component_class ) end |