Class: Baku::Entity

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/baku/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #remove_event_listener

Constructor Details

#initialize(tags = []) ⇒ Entity

Returns a new instance of Entity.



7
8
9
10
# File 'lib/baku/entity.rb', line 7

def initialize(tags = [])
  @tags = tags
  @components = {}
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



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

def components
  @components
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

#add_component(component) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/baku/entity.rb', line 12

def add_component(component)
  if @components.has_key?(component.class)
    raise StandardError.
      new("Entity already has component: #{component.class}")
  end

  components[component.class] = component

  update_component_mask
  
  dispatch_event(:component_added, self, component)
end

#component_maskObject



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

def component_mask
  @component_mask ||= ComponentMask.from_components(@components)
end

#get_component(component_class) ⇒ Object



43
44
45
# File 'lib/baku/entity.rb', line 43

def get_component(component_class)
  @components[component_class]
end

#has_component?(component_class) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/baku/entity.rb', line 39

def has_component?(component_class)
  @components.has_key?(component_class)
end

#remove_component(component_class) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/baku/entity.rb', line 25

def remove_component(component_class)
  if !@components.has_key?(component_class)
    raise StandardError.
      new("Entity does not have component: #{component_class}")
  end

  removed_component = @components[component_class]
  @components.delete(component_class)

  update_component_mask
  
  dispatch_event(:component_removed, self, removed_component)
end