Class: FelFlame::Systems
- Inherits:
-
Object
- Object
- FelFlame::Systems
- Extended by:
- Enumerable
- Defined in:
- felflame.rb,
system_manager.rb
Overview
Creates an manages Systems.
TODO: Improve System overview
Instance Attribute Summary collapse
-
#addition_triggers ⇒ Array<Component>
readonly
Stores references to components or their managers that trigger this component when a component or component from that manager is added to an entity.
-
#attr_triggers ⇒ Hash<Symbol, Array<Symbol>>
readonly
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.
-
#const_name ⇒ Object
The Constant name assigned to this System.
-
#priority ⇒ Object
How early this System should be executed in a list of Systems.
-
#removal_triggers ⇒ Array<Component>
readonly
Stores references to components or their managers that trigger this component when a component or component from that manager is removed from an entity.
Class Method Summary collapse
-
.each(&block) ⇒ Enumerator
Iterate over all Systems, sorted by their priority.
Instance Method Summary collapse
-
#call ⇒ Object
Manually execute the system a single time.
-
#clear_triggers(*trigger_types, component_or_manager: nil) ⇒ Boolean
Removes triggers from this system.
-
#initialize(name, priority: 0, &block) ⇒ Systems
constructor
Creates a new System which can be accessed as a constant under the namespace Systems.
-
#redefine(&block) ⇒ Object
Redefine what code is executed by this System when it is called upon.
-
#trigger_when_added(component_or_manager) ⇒ Boolean
Add a component or component manager so that it triggers this system when the component or a component from the component manager is added to an entity.
-
#trigger_when_is_changed(component_or_manager, attr) ⇒ Object
Add a component or component manager so that it triggers this system when a component's attribute is changed.
-
#trigger_when_removed(component_or_manager) ⇒ Boolean
Add a component or component manager so that it triggers this system when the component or a component from the component manager is removed from an entity.
Constructor Details
#initialize(name, priority: 0, &block) ⇒ Systems
Creates a new System which can be accessed as a constant under the namespace FelFlame::Systems. The name given is what constant the system is assigned to
67 68 69 70 71 72 |
# File 'system_manager.rb', line 67 def initialize(name, priority: 0, &block) FelFlame::Systems.const_set(name, self) @const_name = name @priority = priority @block = block end |
Instance Attribute Details
#addition_triggers ⇒ Array<Component>
Stores references to components or their managers that trigger this component when a component or component from that manager is added to an entity. Do not edit this hash as it is managed by FelFlame automatically.
20 21 22 |
# File 'system_manager.rb', line 20 def addition_triggers @addition_triggers ||= [] end |
#attr_triggers ⇒ Hash<Symbol, Array<Symbol>>
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.
38 39 40 |
# File 'system_manager.rb', line 38 def attr_triggers @attr_triggers ||= {} end |
#const_name ⇒ Object
The Constant name assigned to this System
7 8 9 |
# File 'system_manager.rb', line 7 def const_name @const_name end |
#priority ⇒ Object
How early this System should be executed in a list of Systems
4 5 6 |
# File 'system_manager.rb', line 4 def priority @priority end |
#removal_triggers ⇒ Array<Component>
Stores references to components or their managers that trigger this component when a component or component from that manager is removed from an entity. Do not edit this hash as it is managed by FelFlame automatically.
29 30 31 |
# File 'system_manager.rb', line 29 def removal_triggers @removal_triggers ||= [] end |
Class Method Details
.each(&block) ⇒ Enumerator
Iterate over all Systems, sorted by their priority. You also call other enumerable methods instead of each, such as each_with_index or select
47 48 49 |
# File 'system_manager.rb', line 47 def each(&block) constants.map { |sym| const_get(sym) }.sort_by(&:priority).reverse.each(&block) end |
Instance Method Details
#call ⇒ Object
Manually execute the system a single time
75 76 77 |
# File 'system_manager.rb', line 75 def call @block.call end |
#clear_triggers(*trigger_types, component_or_manager: nil) ⇒ Boolean
Removes triggers from this system. This function is fairly flexible so it can accept a few different inputs For addition and removal triggers, you can optionally pass in a component, or a manager to clear specifically the relevant triggers for that one component or manager. If you do not pass a component or manager then it will clear triggers for all components and managers. For attr_triggers
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'system_manager.rb', line 107 def clear_triggers(*trigger_types, component_or_manager: nil) trigger_types = [: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 component_or_manager.nil? #remove all attrs self.attr_triggers.each do |cmp_or_mgr, attrs| attrs.each do |attr| next if cmp_or_mgr.attr_triggers[attr].nil? cmp_or_mgr.attr_triggers[attr].delete self end 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| component_or_manager.attr_triggers[attr].delete self end self.attr_triggers[component_or_manager] = [] end end else 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 #self.attr_triggers[component_or_manager].each do |attr| # component_or_manager.attr_triggers[attr].delete self #end end self.attr_triggers[component_or_manager] -= trigger_types unless self.attr_triggers[component_or_manager].nil? end end end (trigger_types & [: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| trigger.send(trigger_type).delete self end self.send("#{trigger_type.to_s}=", []) else #remove removal trigger relevant to comp/man self.send(trigger_type).delete component_or_manager component_or_manager.send(trigger_type).delete self end end end |
#redefine(&block) ⇒ Object
Redefine what code is executed by this System when it is called upon.
80 81 82 |
# File 'system_manager.rb', line 80 def redefine(&block) @block = block end |
#trigger_when_added(component_or_manager) ⇒ Boolean
Add a component or component manager so that it triggers this system when the component or a component from the component manager is added to an entity
180 181 182 183 184 |
# File 'system_manager.rb', line 180 def trigger_when_added(component_or_manager) self.addition_triggers |= [component_or_manager] component_or_manager.addition_triggers |= [self] true end |
#trigger_when_is_changed(component_or_manager, attr) ⇒ Object
Add a component or component manager so that it triggers this system when a component's attribute is changed.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'system_manager.rb', line 196 def trigger_when_is_changed(component_or_manager, attr) if component_or_manager.attr_triggers[attr].nil? component_or_manager.attr_triggers[attr] = [self] else component_or_manager.attr_triggers[attr] |= [self] end if self.attr_triggers[component_or_manager].nil? self.attr_triggers[component_or_manager] = [attr] else self.attr_triggers[component_or_manager] |= [attr] end end |
#trigger_when_removed(component_or_manager) ⇒ Boolean
Add a component or component manager so that it triggers this system when the component or a component from the component manager is removed from an entity
189 190 191 192 193 |
# File 'system_manager.rb', line 189 def trigger_when_removed(component_or_manager) self.removal_triggers |= [component_or_manager] component_or_manager.removal_triggers |= [self] true end |