Class: BeltsEngine::Ecs::Collection

Inherits:
Hash
  • Object
show all
Defined in:
lib/belts_engine/ecs/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(with: [], without: []) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
# File 'lib/belts_engine/ecs/collection.rb', line 5

def initialize(with: [], without: [])
  @with = with.sort
  @without = without.sort
  @key = {with: with, without: without}
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/belts_engine/ecs/collection.rb', line 3

def key
  @key
end

Instance Method Details

#add_entity(entity) ⇒ Object



11
12
13
14
15
16
# File 'lib/belts_engine/ecs/collection.rb', line 11

def add_entity(entity)
  return if (@with - entity.keys).any?
  return if (@without & entity.keys).any?

  self[entity[:id]] = entity.slice(:id, *@with)
end

#each_with_componentsObject



22
23
24
25
26
# File 'lib/belts_engine/ecs/collection.rb', line 22

def each_with_components
  each do |k, v|
    yield **v
  end
end

#remove_entity(entity_id) ⇒ Object



18
19
20
# File 'lib/belts_engine/ecs/collection.rb', line 18

def remove_entity(entity_id)
  self.delete(entity_id)
end