diff options
| author | realtradam <[email protected]> | 2021-05-20 05:04:21 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-20 05:04:21 -0400 |
| commit | f97a9ca95e464e728bba9337b579bc380c33bc7d (patch) | |
| tree | dd45a6e615e6e873cee15cb41daaca24b3357ea6 /app/lib/entity_manager.rb | |
| parent | fee80f42f0889f2d484e25f4366f14b68c65ba70 (diff) | |
| download | typemon-code-f97a9ca95e464e728bba9337b579bc380c33bc7d.tar.gz typemon-code-f97a9ca95e464e728bba9337b579bc380c33bc7d.zip | |
implemented hitboxes
Diffstat (limited to 'app/lib/entity_manager.rb')
| -rw-r--r-- | app/lib/entity_manager.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/app/lib/entity_manager.rb b/app/lib/entity_manager.rb new file mode 100644 index 0000000..7d0d1ff --- /dev/null +++ b/app/lib/entity_manager.rb @@ -0,0 +1,63 @@ +class Entity + attr_accessor :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 + + def id_queue + @id_queue ||= [] + 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 + + 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 + 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 + end + all.pop + signatures.pop + else + puts 'Unknown error with destroy_entity, entity not destroyed' + end + end + end +end |
