From a0f792d8feadf919290b8349dbc0eac143545927 Mon Sep 17 00:00:00 2001 From: _Tradam Date: Mon, 3 Jan 2022 08:26:24 -0500 Subject: Major 4.0 Update (#16) See Changelog --- lib/felflame/component_manager.rb | 169 ++++++++++++++++++++++---------------- 1 file changed, 98 insertions(+), 71 deletions(-) (limited to 'lib/felflame/component_manager.rb') diff --git a/lib/felflame/component_manager.rb b/lib/felflame/component_manager.rb index 43932d2..6ac7463 100644 --- a/lib/felflame/component_manager.rb +++ b/lib/felflame/component_manager.rb @@ -1,8 +1,9 @@ -class FelFlame - class Components +# frozen_string_literal: true + +module FelFlame + module Components @component_map = [] - class < 1 + Warning.warn("This component belongs to MANY entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.") + end + entities.first + end + # Update attribute values using a hash or keywords. # @return [Hash] Hash of updated attributes def update_attrs(**opts) @@ -200,39 +231,35 @@ class FelFlame # Execute systems that have been added to execute on variable change # @return [Boolean] +true+ + # @!visibility private def attr_changed_trigger_systems(attr) systems_to_execute = self.class.attr_triggers[attr] systems_to_execute = [] if systems_to_execute.nil? systems_to_execute |= attr_triggers[attr] unless attr_triggers[attr].nil? - systems_to_execute.sort_by(&:priority).reverse.each(&:call) + systems_to_execute.sort_by(&:priority).reverse_each(&:call) true end - # Removes this component from the list and purges all references to this Component from other Entities, as well as its {id ID} and data. + # Removes this component from the list and purges all references to this Component from other Entities, as well as its data. # @return [Boolean] +true+. def delete addition_triggers.each do |system| system.clear_triggers component_or_manager: self end - # This needs to be cloned because indices get deleted as - # the remove command is called, breaking the loop if it - # wasn't referencing a clone(will get Nil errors) - iter = entities.map(&:clone) - iter.each do |entity| - #FelFlame::Entities[entity_id].remove self #unless FelFlame::Entities[entity_id].nil? + entities.reverse_each do |entity| entity.remove self end - self.class.data[id] = nil + self.class._data.delete self instance_variables.each do |var| instance_variable_set(var, nil) end true end - # @return [Hash] A hash, where all the keys are attributes linked to their respective values. - def attrs + # @return [Hash] A hash, where all the keys are attributes storing their respective values. + def to_h return_hash = instance_variables.each_with_object({}) do |key, final| final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key) end @@ -243,8 +270,8 @@ class FelFlame # Export all data into a JSON String, which could then later be loaded or saved to a file # TODO: This function is not yet complete # @return [String] a JSON formatted String - #def to_json + # def to_json # # should return a json or hash of all data in this component - #end + # end end end -- cgit v1.2.3