Class: Moon::EntitySystem::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/moon/packages/entity_system/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(world) ⇒ Entity

Returns a new instance of Entity.


6
7
8
9
# File 'lib/moon/packages/entity_system/entity.rb', line 6

def initialize(world)
  @world = world
  @id = @world.random.base64(16) # right within ruby's optimal string length
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


4
5
6
# File 'lib/moon/packages/entity_system/entity.rb', line 4

def id
  @id
end

Instance Method Details

#==(obj) ⇒ Object


11
12
13
# File 'lib/moon/packages/entity_system/entity.rb', line 11

def ==(obj)
  self.id == obj.id
end

#[]=(key, component) ⇒ Object


37
38
39
# File 'lib/moon/packages/entity_system/entity.rb', line 37

def []=(key, component)
  @world.set_component(self, key, component)
end

#add(component, options = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moon/packages/entity_system/entity.rb', line 19

def add(component, options = {})
  case component
  when Hash
    component.map do |k, v|
      @world.add_component(self, Component.manager.fetch(k).new(v))
    end
  when Symbol
    @world.add_component(self, Component.manager.fetch(component).new(options))
  else
    @world.add_component(self, component)
  end
end

#comp(*keys) {|components| ... } ⇒ Object

Yields:


41
42
43
44
45
# File 'lib/moon/packages/entity_system/entity.rb', line 41

def comp(*keys)
  components = keys.map { |key| get_component(key) }
  yield(*components) if components.all? if block_given?
  components
end

#componentsObject


15
16
17
# File 'lib/moon/packages/entity_system/entity.rb', line 15

def components
  @world.get_components(self)
end

#exportObject


53
54
55
# File 'lib/moon/packages/entity_system/entity.rb', line 53

def export
  to_h.stringify_keys
end

#get_component(key) ⇒ Object Also known as: []


32
33
34
# File 'lib/moon/packages/entity_system/entity.rb', line 32

def get_component(key)
  @world.get_component(self, key)
end

#import(data) ⇒ Object


57
58
59
60
# File 'lib/moon/packages/entity_system/entity.rb', line 57

def import(data)
  @id = data["id"]
  self
end

#inherit(entity) ⇒ Object


62
63
64
65
66
# File 'lib/moon/packages/entity_system/entity.rb', line 62

def inherit(entity)
  entity.components.each do |component|
    add(component.as_inheritance)
  end
end

#to_hObject


47
48
49
50
51
# File 'lib/moon/packages/entity_system/entity.rb', line 47

def to_h
  {
    id: @id
  }
end