diff options
| author | realtradam <[email protected]> | 2022-01-03 07:34:38 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-01-03 07:34:38 -0500 |
| commit | 55f1db30e7a609cebd2fab27c55ece1da45be232 (patch) | |
| tree | 286045c958877ea872c69990d289dbf4c44c3c32 /lib | |
| parent | 809028c3d89993b2bb0651076ba723c11734cf3c (diff) | |
| download | FelECS-55f1db30e7a609cebd2fab27c55ece1da45be232.tar.gz FelECS-55f1db30e7a609cebd2fab27c55ece1da45be232.zip | |
.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/felflame.rb | 15 | ||||
| -rw-r--r-- | lib/felflame/component_manager.rb | 46 | ||||
| -rw-r--r-- | lib/felflame/entity_manager.rb | 34 | ||||
| -rw-r--r-- | lib/felflame/order.rb | 6 | ||||
| -rw-r--r-- | lib/felflame/scene_manager.rb | 4 | ||||
| -rw-r--r-- | lib/felflame/stage_manager.rb | 6 | ||||
| -rw-r--r-- | lib/felflame/system_manager.rb | 83 | ||||
| -rw-r--r-- | lib/felflame/version.rb | 2 |
8 files changed, 100 insertions, 96 deletions
diff --git a/lib/felflame.rb b/lib/felflame.rb index 54084bf..b7a386a 100644 --- a/lib/felflame.rb +++ b/lib/felflame.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative 'felflame/entity_manager' require_relative 'felflame/component_manager' require_relative 'felflame/system_manager' @@ -8,7 +10,7 @@ require_relative 'felflame/version' # The FelFlame namespace where all its functionality resides under. module FelFlame - class <<self + class << self # :nocov: # An alias for {FelFlame::Stage.call}. It executes a single frame in the game. @@ -18,22 +20,21 @@ module FelFlame # :nocov: end - # Creates and manages Entities. Allows iterating or accessing Entities using array methods directly on the class. Entities are just collections of Components. + # Creates and manages Entities. Entities are just collections of Components. + # You can use array methods directly on this class to access Entities. class Entities; end # Creates component managers and allows accessing them them under the {FelFlame::Components} namespace as Constants. + # You can use array methods directly on this class to access Component Managers. # # To see how component managers are used please look at the {FelFlame::ComponentManager} documentation. module Components; end - # Creates an manages Systems. Systems are the logic of the game and do not contain any data within them. Any systems you create are accessable under the {FelFlame::Systems} namespace as Constants. - # - # TODO: Improve Systems overview + # Creates and manages Systems. Systems are the logic of the game and do not contain any data within them. Any systems you create are accessable under the {FelFlame::Systems} namespace as Constants. + # You can use array methods directly on this class to access Systems. class Systems; end # Creates and manages Scenes. Scenes are collections of Systems, and execute all the Systems when called upon. Any scenes you create are accessable under the {FelFlame::Scenes} namespace as Constants. - # - # TODO: Improve Scenes overview class Scenes; end # Stores Scenes you add to it which you want to execute on each frame. When called upon will execute all Systems in the Scenes in the Stage and will execute them according to their priority order. diff --git a/lib/felflame/component_manager.rb b/lib/felflame/component_manager.rb index d015d99..04faccb 100644 --- a/lib/felflame/component_manager.rb +++ b/lib/felflame/component_manager.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module FelFlame module Components @component_map = [] - class <<self + class << self # Creates a new {FelFlame::ComponentManager component manager}. # # @example @@ -20,14 +22,14 @@ module FelFlame raise(NameError.new, "Component Manager '#{component_name}' is already defined") end - const_set(component_name, Class.new(FelFlame::ComponentManager) {}) update_const_cache attrs.each do |attr| - if FelFlame::Components.const_get(component_name).method_defined?("#{attr}") || FelFlame::Components.const_get(component_name).method_defined?("#{attr}=") - raise NameError.new "The attribute name \"#{attr}\" is already a method" + if FelFlame::Components.const_get(component_name).method_defined?(attr.to_s) || FelFlame::Components.const_get(component_name).method_defined?("#{attr}=") + raise NameError, "The attribute name \"#{attr}\" is already a method" end + FelFlame::Components.const_get(component_name).attr_accessor attr end attrs_with_defaults.each do |attr, _default| @@ -49,24 +51,24 @@ module FelFlame # Makes component module behave like an array of component # managers with additional methods for managing the array # @!visibility private - ##def respond_to_missing?(method, *) + # #def respond_to_missing?(method, *) # if constants.respond_to? method # true # else # super # end - #end + # end ## Makes component module behave like arrays with additional ## methods for managing the array ## @!visibility private - #def method_missing(method, *args, **kwargs, &block) + # def method_missing(method, *args, **kwargs, &block) # if constants.respond_to? method # constants.send(method, *args, **kwargs, &block) # else # super # end - #end + # end # Stores the components managers in {FelFlame::Components}. This # is needed because calling `FelFlame::Components.constants` @@ -83,8 +85,8 @@ module FelFlame # Used internally by FelFlame # @!visibility private def update_const_cache - @const_cache = self.constants.map do |constant| - self.const_get constant + @const_cache = constants.map do |constant| + const_get constant end end @@ -110,16 +112,12 @@ module FelFlame super end end - - - end end # Component Managers are what is used to create individual components which can be attached to entities. - # When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending (e.g [email protected] = 5+), or by using the {#attrs} and {#update_attrs} methods instead. + # When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending (e.g [email protected] = 5+), or by using the {#to_h} and {#update_attrs} methods instead. class ComponentManager - # Allows overwriting the storage of triggers, such as for clearing. # This method should generally only need to be used internally and # not by a game developer. @@ -168,13 +166,12 @@ module FelFlame self.class.push self end - class <<self - + class << self # Makes component managers behave like arrays with additional # methods for managing the array # @!visibility private def respond_to_missing?(method, *) - if self._data.respond_to? method + if _data.respond_to? method true else super @@ -185,14 +182,13 @@ module FelFlame # methods for managing the array # @!visibility private def method_missing(method, *args, **kwargs, &block) - if self._data.respond_to? method - self._data.send(method, *args, **kwargs, &block) + if _data.respond_to? method + _data.send(method, *args, **kwargs, &block) else super end end - # Allows overwriting the storage of triggers, such as for clearing. # This method should generally only need to be used internally and # not by a game developer. @@ -257,6 +253,7 @@ module 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? @@ -266,6 +263,7 @@ module FelFlame 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 data. # @return [Boolean] +true+. def delete @@ -282,7 +280,7 @@ module FelFlame true end - # @return [Hash<Symbol, Value>] A hash, where all the keys are attributes linked to their respective values. + # @return [Hash<Symbol, Value>] 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) @@ -294,8 +292,8 @@ module 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 diff --git a/lib/felflame/entity_manager.rb b/lib/felflame/entity_manager.rb index 2ae0849..ef70510 100644 --- a/lib/felflame/entity_manager.rb +++ b/lib/felflame/entity_manager.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: true + module FelFlame class Entities - # Creating a new Entity # @param components [Components] Can be any number of components, identical duplicates will be automatically purged however different components from the same component manager are allowed. # @return [Entity] @@ -16,9 +17,9 @@ module FelFlame @components ||= {} end - # A single component from a component manager. Use this if you expect the component to only belong to one entity and you want to access it. Access the component using either parameter notation or array notation. + # A single component from a component manager. Use this if you expect the component to only belong to one entity and you want to access it. Access the component using either parameter notation or array notation. Array notation is conventional for better readablility. # @example - # @entity.component[@component_manager] # array notation + # @entity.component[@component_manager] # array notation(the standard) # @entity.component(@component_manager) # method notation # @param manager [ComponentManager] If you pass nil you can then use array notation to access the same value. # @return [Component] @@ -32,15 +33,15 @@ module FelFlame elsif components[manager].length > 1 Warning.warn("This entity has MANY of this component but you called the method that is intended for having a single of this component type.\nYou may have a bug in your logic.") end + components[manager].first end end - # Removes this Entity from the list and purges all references to this Entity from other Components, as well as its data. # @return [Boolean] +true+ def delete - components.each do |component_manager, component_array| + components.each do |_component_manager, component_array| component_array.reverse_each do |component| component.entities.delete(self) end @@ -88,9 +89,7 @@ module FelFlame check_systems component, :removal_triggers if component.entities.include? self component.entities.delete self components[component.class].delete component - if components[component.class].empty? - components.delete component.class - end + components.delete component.class if components[component.class].empty? end true end @@ -98,14 +97,14 @@ module FelFlame # Export all data into a JSON String which can then be saved into a file # TODO: This function is not yet complete # @return [String] A JSON formatted String - #def to_json() end + # def to_json() end - class <<self + class << self # Makes component managers behave like arrays with additional # methods for managing the array # @!visibility private def respond_to_missing?(method, *) - if self._data.respond_to? method + if _data.respond_to? method true else super @@ -116,8 +115,8 @@ module FelFlame # methods for managing the array # @!visibility private def method_missing(method, *args, **kwargs, &block) - if self._data.respond_to? method - self._data.send(method, *args, **kwargs, &block) + if _data.respond_to? method + _data.send(method, *args, **kwargs, &block) else super end @@ -129,7 +128,6 @@ module FelFlame # @!visibility private def component_redirect if @component_redirect - @component_redirect else @component_redirect = Object.new @component_redirect.instance_variable_set(:@entity, nil) @@ -140,14 +138,12 @@ module FelFlame instance_variable_set(:@entity, value) end @component_redirect.define_singleton_method(:[]) do |component_manager| - self.entity.component(component_manager) + entity.component(component_manager) end - @component_redirect end + @component_redirect end - - # @return [Array<Entity>] Array of all Entities that exist # @!visibility private def _data @@ -158,7 +154,7 @@ module FelFlame # TODO: This function is not yet complete # @param json_string [String] A string that was exported originally using the {FelFlame::Entities#to_json to_json} function # @param opts [Keywords] What values(its {FelFlame::Entities#id ID} or the {FelFlame::ComponentManager#id component IDs}) should be overwritten TODO: this might change - #def from_json(json_string, **opts) end + # def from_json(json_string, **opts) end end end end diff --git a/lib/felflame/order.rb b/lib/felflame/order.rb new file mode 100644 index 0000000..0bed66b --- /dev/null +++ b/lib/felflame/order.rb @@ -0,0 +1,6 @@ +module FelFlame + module Order + def sort(*sortables) + end + end +end diff --git a/lib/felflame/scene_manager.rb b/lib/felflame/scene_manager.rb index 9ac7924..0024815 100644 --- a/lib/felflame/scene_manager.rb +++ b/lib/felflame/scene_manager.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FelFlame class Scenes # Allows overwriting the storage of systems, such as for clearing. @@ -60,7 +62,7 @@ module FelFlame system.scenes.delete self end systems.clear - #FelFlame::Stage.update_systems_list if FelFlame::Stage.scenes.include? self + # FelFlame::Stage.update_systems_list if FelFlame::Stage.scenes.include? self true end end diff --git a/lib/felflame/stage_manager.rb b/lib/felflame/stage_manager.rb index 50dad5f..a192b67 100644 --- a/lib/felflame/stage_manager.rb +++ b/lib/felflame/stage_manager.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module FelFlame module Stage - class <<self + class << self # Allows clearing of scenes and systems. # Used internally by FelFlame and shouldn't need to be ever used by developers # @!visibility private @@ -28,7 +30,7 @@ module FelFlame true end - # Executes one frame of the game. This executes all the Systems in the Scenes added to the Stage. Systems that exist in two or more different Scenes will still only get executed once. + # Executes one frame of the game. This executes all the Scenes added to the Stage in order of their priority. # @return [Boolean] +true+ def call self.scenes.each(&:call) diff --git a/lib/felflame/system_manager.rb b/lib/felflame/system_manager.rb index 480b84a..8982589 100644 --- a/lib/felflame/system_manager.rb +++ b/lib/felflame/system_manager.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FelFlame class Systems # How early this System should be executed in a list of Systems @@ -43,7 +45,6 @@ module FelFlame @removal_triggers ||= [] end - # Stores references to systems that should be triggered when an # attribute from this manager is changed # Do not edit this hash as it is managed by FelFlame automatically. @@ -52,8 +53,7 @@ module FelFlame @attr_triggers ||= {} end - class <<self - + class << self # Stores the systems in {FelFlame::Components}. This # is needed because calling `FelFlame::Components.constants` # will not let you iterate over the value of the constants @@ -68,8 +68,8 @@ module FelFlame # Used internally by FelFlame # @!visibility private def update_const_cache - @const_cache = self.constants.map do |constant| - self.const_get constant + @const_cache = constants.map do |constant| + const_get constant end end @@ -95,7 +95,6 @@ module FelFlame super end end - end # Creates a new System which can be accessed as a constant under the namespace {FelFlame::Systems}. @@ -125,6 +124,7 @@ module FelFlame def call @block.call end + # Redefine what code is executed by this System when it is called upon. # @param block [Proc] The code you wish to be executed when the system is triggered. Can be defined by using a +do end+ block or using +{ }+ braces. def redefine(&block) @@ -155,16 +155,16 @@ module FelFlame # @param component_or_manager [Component or ComponentManager] The object to clear triggers from. Use Nil to clear triggers from all components associated with this system. # @return [Boolean] +true+ def clear_triggers(*trigger_types, component_or_manager: nil) - trigger_types = [:addition_triggers, :removal_triggers, :attr_triggers] if trigger_types.empty? + trigger_types = %i[addition_triggers removal_triggers attr_triggers] if trigger_types.empty? if trigger_types.include? :attr_triggers - if (trigger_types - [:addition_triggers, - :removal_triggers, - :attr_triggers]).empty? + if (trigger_types - %i[addition_triggers + removal_triggers + attr_triggers]).empty? if component_or_manager.nil? - #remove all attrs - self.attr_triggers.each do |cmp_or_mgr, attrs| + # remove all attrs + attr_triggers.each do |cmp_or_mgr, attrs| attrs.each do |attr| next if cmp_or_mgr.attr_triggers[attr].nil? @@ -173,49 +173,48 @@ module FelFlame self.attr_triggers = {} end else - #remove attrs relevant to comp_or_man - unless self.attr_triggers[component_or_manager].nil? - self.attr_triggers[component_or_manager].each do |attr| + # remove attrs relevant to comp_or_man + unless attr_triggers[component_or_manager].nil? + attr_triggers[component_or_manager].each do |attr| component_or_manager.attr_triggers[attr].delete self end - self.attr_triggers[component_or_manager] = [] + attr_triggers[component_or_manager] = [] end end - else + elsif component_or_manager.nil? - if component_or_manager.nil? - (trigger_types - [:addition_triggers, :removal_triggers, :attr_triggers]).each do |attr| - #remove attr - self.attr_triggers.each do |cmp_or_mgr, attrs| - cmp_or_mgr.attr_triggers[attr].delete self - end - end - self.attr_triggers.delete (trigger_types - [:addition_triggers, - :removal_triggers, - :attr_triggers]) - else - #remove attr from component_or_manager - (trigger_types - [:addition_triggers, :removal_triggers, :attr_triggers]).each do |attr| - next if component_or_manager.attr_triggers[attr].nil? - component_or_manager.attr_triggers[attr].delete self + (trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr| + # remove attr + attr_triggers.each do |cmp_or_mgr, _attrs| + cmp_or_mgr.attr_triggers[attr].delete self end - self.attr_triggers[component_or_manager] -= trigger_types unless self.attr_triggers[component_or_manager].nil? end + attr_triggers.delete(trigger_types - %i[addition_triggers + removal_triggers + attr_triggers]) + else + # remove attr from component_or_manager + (trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr| + next if component_or_manager.attr_triggers[attr].nil? + + component_or_manager.attr_triggers[attr].delete self + end + attr_triggers[component_or_manager] -= trigger_types unless attr_triggers[component_or_manager].nil? end end - (trigger_types & [:removal_triggers, :addition_triggers] - [:attr_triggers]).each do |trigger_type| + (trigger_types & %i[removal_triggers addition_triggers] - [:attr_triggers]).each do |trigger_type| if component_or_manager.nil? - #remove all removal triggers - self.send(trigger_type).each do |trigger| + # remove all removal triggers + send(trigger_type).each do |trigger| trigger.send(trigger_type).delete self end - self.send("#{trigger_type.to_s}=", []) + send("#{trigger_type}=", []) else - #remove removal trigger relevant to comp/man - self.send(trigger_type).delete component_or_manager + # remove removal trigger relevant to comp/man + send(trigger_type).delete component_or_manager component_or_manager.send(trigger_type).delete self end end @@ -248,10 +247,10 @@ module FelFlame else component_or_manager.attr_triggers[attr] |= [self] end - if self.attr_triggers[component_or_manager].nil? - self.attr_triggers[component_or_manager] = [attr] + if attr_triggers[component_or_manager].nil? + attr_triggers[component_or_manager] = [attr] else - self.attr_triggers[component_or_manager] |= [attr] + attr_triggers[component_or_manager] |= [attr] end true end diff --git a/lib/felflame/version.rb b/lib/felflame/version.rb index 882774f..b200c26 100644 --- a/lib/felflame/version.rb +++ b/lib/felflame/version.rb @@ -4,6 +4,6 @@ # Keeps the version of the Gem module Felflame # The version of the Gem - VERSION = "3.0.0" + VERSION = '3.0.0' end # :nocov: |
