Class: Darkholme::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/darkholme/entity.rb

Overview

An Entity contains Components, which hold data which is manipulated by a System

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntity

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_bitsObject

Returns the value of attribute component_bits.



5
6
7
# File 'lib/darkholme/entity.rb', line 5

def component_bits
  @component_bits
end

#componentsObject

Returns the value of attribute components.



5
6
7
# File 'lib/darkholme/entity.rb', line 5

def components
  @components
end

#engineObject

Returns the value of attribute engine.



5
6
7
# File 'lib/darkholme/entity.rb', line 5

def engine
  @engine
end

#family_bitsObject

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

Parameters:

  • source (Object)

    An object that works with MultiJson.load

Returns:



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

Parameters:

  • component (Component)

    The component being added

Returns:

  • (Component)

    The component that was added



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

Parameters:

  • engine (Engine)

    The engine the system was added to



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

Parameters:

  • component_class (Class)

    The class of the component to get

Returns:



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

Parameters:

  • component_class (Class)

    The class of the component to check for

Returns:

  • (Boolean)

    Whether or not the entity has the 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

Parameters:

  • component_class (Class)

    The class of the component being removed

Returns:

  • (Component)

    The component that was removed



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

Parameters:

  • engine (Engine)

    The engine the entity was removed from



47
48
49
# File 'lib/darkholme/entity.rb', line 47

def removed_from_engine(engine)
  self.engine = nil
end