diff options
| author | realtradam <[email protected]> | 2021-05-18 04:49:47 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-18 04:49:47 -0400 |
| commit | 120b693ffd02bc5c7f41ff2b9657facc7117daae (patch) | |
| tree | 59e9f982dc2ee994cbbb65411065b35445048502 /app/ECS/entity_manager.rb | |
| parent | 24524ad0b1c7a2aeea0bad28092e946cef8026fa (diff) | |
| download | typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.tar.gz typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.zip | |
.
Diffstat (limited to 'app/ECS/entity_manager.rb')
| -rw-r--r-- | app/ECS/entity_manager.rb | 98 |
1 files changed, 48 insertions, 50 deletions
diff --git a/app/ECS/entity_manager.rb b/app/ECS/entity_manager.rb index ba3561f..7d0d1ff 100644 --- a/app/ECS/entity_manager.rb +++ b/app/ECS/entity_manager.rb @@ -1,64 +1,62 @@ -class ECS - class Entity - attr_accessor :id +class Entity + attr_accessor :id - def initialize(*signature) - final_signature = 0 - signature.each do |sig| - final_signature += sig - end - @id = ECS::Entity.generate_new_id - self.class.all.push self - self.class.signatures.push final_signature - ECS::Components.entity_created(@id) + def initialize(*signature) + final_signature = 0 + signature.each do |sig| + final_signature += sig end + @id = Entity.generate_new_id + self.class.all.push self + self.class.signatures.push final_signature + Components.entity_created(@id) + end - class <<self - # All entities that exist - def all - @all ||= [] - end + class <<self + # All entities that exist + def all + @all ||= [] + end - def id_queue - @id_queue ||= [] - end + def id_queue + @id_queue ||= [] + end - def generate_new_id - if id_queue.empty? - all.size - else - id_queue.shift - end + def generate_new_id + if id_queue.empty? + all.size + else + id_queue.shift end + end - # What components a given entity uses - def signatures - @signatures ||= [] - end + # What components a given entity uses + def signatures + @signatures ||= [] + end - def destroy_entity(entity_id) - if all[entity_id].nil? - puts 'Entity can not be destroyed, id out of bounds' - elsif entity_id < all.size - 1 - ECS::Components.constants.each do |constant| - unless (signatures[entity_id] & ECS::Components::const_get(constant).id).zero? - ECS::Components::const_get(constant).delete(entity_id) - end + def destroy_entity(entity_id) + if all[entity_id].nil? + puts 'Entity can not be destroyed, id out of bounds' + elsif entity_id < all.size - 1 + Components.constants.each do |constant| + unless (signatures[entity_id] & Components::const_get(constant).id).zero? + Components::const_get(constant).delete(entity_id) end - all[entity_id] = nil - signatures[entity_id] = nil - id_queue.push entity_id - elsif entity_id == all.size - 1 - ECS::Components.constants.each do |constant| - unless (signatures[entity_id] & ECS::Components::const_get(constant).id).zero? - ECS::Components::const_get(constant).delete(entity_id) - end + end + all[entity_id] = nil + signatures[entity_id] = nil + id_queue.push entity_id + elsif entity_id == all.size - 1 + Components.constants.each do |constant| + unless (signatures[entity_id] & Components::const_get(constant).id).zero? + Components::const_get(constant).delete(entity_id) end - all.pop - signatures.pop - else - puts 'Unknown error with destroy_entity, entity not destroyed' end + all.pop + signatures.pop + else + puts 'Unknown error with destroy_entity, entity not destroyed' end end end |
