Class: Chione::Entity

Inherits:
Object
  • Object
show all
Extended by:
Deprecatable, Loggability
Includes:
Inspection
Defined in:
lib/chione/entity.rb

Overview

The Entity (identity) class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspection

#inspect

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.



30
31
32
33
# File 'lib/chione/entity.rb', line 30

def initialize( world, id=nil )
	@world = world
	@id    = id || self.class.make_new_id
end

Instance Attribute Details

#idObject (readonly)

The Entity’s ID



41
42
43
# File 'lib/chione/entity.rb', line 41

def id
  @id
end

#worldObject (readonly)

The World the Entity belongs to



44
45
46
# File 'lib/chione/entity.rb', line 44

def world
  @world
end

Class Method Details

.make_new_idObject

Return an ID for an Entity.



23
24
25
# File 'lib/chione/entity.rb', line 23

def self::make_new_id
	return Chione.uuid.generate
end

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.



85
86
87
88
# File 'lib/chione/entity.rb', line 85

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.



57
58
59
# File 'lib/chione/entity.rb', line 57

def add_component( component, **init_values )
	self.world.add_component_to( self, component, init_values )
end

#componentsObject

Return the components that the entity’s World has registered for it as a Hash keyed by the Component class.



49
50
51
# File 'lib/chione/entity.rb', line 49

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.



64
65
66
# File 'lib/chione/entity.rb', line 64

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.

Returns:

  • (Boolean)


78
79
80
# File 'lib/chione/entity.rb', line 78

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.



72
73
74
# File 'lib/chione/entity.rb', line 72

def remove_component( component_class )
	return self.world.remove_component_from( self, component_class )
end