diff options
| author | realtradam <[email protected]> | 2021-07-03 00:36:00 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-07-03 00:36:00 -0400 |
| commit | a96733c75ce7caff16a1d79a9caa1894ed95d2ca (patch) | |
| tree | c4c6ed488c1b100eca0de065348a7d056f521134 /component_manager.rb | |
| parent | ba707eebb995eb46141d3c5e1701cd7252ba81c8 (diff) | |
| download | FelECS-a96733c75ce7caff16a1d79a9caa1894ed95d2ca.tar.gz FelECS-a96733c75ce7caff16a1d79a9caa1894ed95d2ca.zip | |
completed systems functionality
Diffstat (limited to 'component_manager.rb')
| -rw-r--r-- | component_manager.rb | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/component_manager.rb b/component_manager.rb index a29aac8..14975e9 100644 --- a/component_manager.rb +++ b/component_manager.rb @@ -30,7 +30,12 @@ class FelFlame FelFlame::Components.const_get(component_name).attr_accessor attr end attrs_with_defaults.each do |attr, _default| - FelFlame::Components.const_get(component_name).attr_accessor attr + #FelFlame::Components.const_get(component_name).attr_accessor attr + FelFlame::Components.const_get(component_name).attr_reader attr + FelFlame::Components.const_get(component_name).define_method("#{attr}=") do |value| + attr_changed_trigger_systems(attr) unless value.equal? send(attr) + instance_variable_set("@#{attr}", value) + end end FelFlame::Components.const_get(component_name).define_method(:set_defaults) do attrs_with_defaults.each do |attr, default| @@ -56,18 +61,20 @@ class FelFlame # @return [Integer] attr_accessor :id - attr_writer :addition_triggers + attr_writer :addition_triggers, :removal_triggers, :attr_triggers def addition_triggers @addition_triggers ||= [] end - attr_writer :removal_triggers - def removal_triggers @removal_triggers ||= [] end + def attr_triggers + @attr_triggers ||= {} + end + # Creates a new component and sets the values of the attributes given to it. If an attritbute is not passed then it will remain as the default. # @param attrs [Keyword: Value] You can pass any number of Keyword-Value pairs # @return [Component] @@ -92,18 +99,20 @@ class FelFlame end class <<self - attr_writer :addition_triggers + attr_writer :addition_triggers, :removal_triggers, :attr_triggers def addition_triggers @addition_triggers ||= [] end - attr_writer :removal_triggers - def removal_triggers @removal_triggers ||= [] end + def attr_triggers + @attr_triggers ||= {} + end + # @return [Array<Component>] Array of all Components that belong to a given component manager # @!visibility private def data @@ -149,6 +158,19 @@ class FelFlame end end + # Execute systems that have been added to execute on variable change + 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) + #self.attr_triggers.each do |system| + # systems_to_execute |= [system] + #end + 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. # @return [Boolean] true. def delete @@ -171,9 +193,11 @@ class FelFlame # @return [Hash] A hash, where all the keys are attributes linked to their respective values. def attrs - instance_variables.each_with_object({}) do |key, final| + return_hash = instance_variables.each_with_object({}) do |key, final| final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key) end + return_hash.delete(:attr_triggers) + return_hash end # Export all data into a JSON String, which could then later be loaded or saved to a file |
