module FelECS::Components
Creates component managers and allows accessing them them under the {FelECS::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 {FelECS::ComponentManager} documentation.
Public Class Methods
Stores the components managers in {FelECS::Components}. This is needed because calling ‘FelECS::Components.constants` will not let you iterate over the value of the constants but will instead give you an array of symbols. This caches the convertion of those symbols to the actual value of the constants @!visibility private
# File lib/felecs/component_manager.rb, line 60 def const_cache @const_cache || update_const_cache end
Makes component module behave like arrays with additional methods for managing the array @!visibility private
# File lib/felecs/component_manager.rb, line 88 def method_missing(method, *args, **kwargs, &block) if const_cache.respond_to? method const_cache.send(method, *args, **kwargs, &block) else super end end
Creates a new {FelECS::ComponentManager component manager}.
@example
# Here color is set to default to red # while max and current are nil until set. # When you make a new component using this component manager # these are the values and accessors it will have. FelECS::Component.new('Health', :max, :current, color: 'red')
@param component_name [String] Name of your new component manager. Must be stylized in the format of constants in Ruby @param attrs [:Symbols] New components made with this manager will include these symbols as accessors, the values of these accessors will default to nil @param attrs_with_defaults [Keyword: DefaultValue] New components made with this manager will include these keywords as accessors, their defaults set to the values given to the keywords @return [ComponentManager]
# File lib/felecs/component_manager.rb, line 20 def new(component_name, *attrs, **attrs_with_defaults) if FelECS::Components.const_defined?(component_name) raise(NameError.new, "Component Manager '#{component_name}' is already defined") end const_set(component_name, Class.new(FelECS::ComponentManager) {}) update_const_cache attrs.each do |attr| if FelECS::Components.const_get(component_name).method_defined?(attr.to_s) || FelECS::Components.const_get(component_name).method_defined?("#{attr}=") raise NameError, "The attribute name \"#{attr}\" is already a method" end FelECS::Components.const_get(component_name).attr_accessor attr end attrs_with_defaults.each do |attr, _default| attrs_with_defaults[attr] = _default.dup FelECS::Components.const_get(component_name).attr_reader attr FelECS::Components.const_get(component_name).define_method("#{attr}=") do |value| unless value.equal? send(attr) instance_variable_set("@#{attr}", value) attr_changed_trigger_systems(attr) end end end FelECS::Components.const_get(component_name).define_method(:set_defaults) do attrs_with_defaults.each do |attr, default| instance_variable_set("@#{attr}", default.dup) end end FelECS::Components.const_get(component_name) end
Forwards undefined methods to the array of constants if the array can handle the request. Otherwise tells the programmer their code errored @!visibility private
# File lib/felecs/component_manager.rb, line 77 def respond_to_missing?(method, *) if const_cache.respond_to? method true else super end end
Updates the array that stores the constants. Used internally by FelECS
@!visibility private
# File lib/felecs/component_manager.rb, line 67 def update_const_cache @const_cache = constants.map do |constant| const_get constant end end