Class: FelFlame::Systems

Inherits:
Object
  • Object
show all
Defined in:
lib/felflame.rb,
lib/felflame/system_manager.rb

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 Systems namespace as Constants. You can use array methods directly on this class to access Systems.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Examples:

FelFlame::Systems.new('PassiveHeal', priority: -2) do
  FelFlame::Components::Health.each do |component|
    component.hp += 5
  end
end
# Give it a low priority so other systems such as a
#   Poison system would kill the player first

Parameters:

  • name (String)

    The name this system will use. Needs to to be in the Ruby Constant format.

  • priority (Integer) (defaults to: 0)

    Which priority order this system should be executed in relative to other systems. Higher means executed earlier.

  • 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.



115
116
117
118
119
120
121
# File 'lib/felflame/system_manager.rb', line 115

def initialize(name, priority: 0, &block)
  FelFlame::Systems.const_set(name, self)
  FelFlame::Systems.update_const_cache
  @priority = priority
  @block = block
  @scenes = []
end

Instance Attribute Details

#addition_triggersArray<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.

Returns:

  • (Array<Component>)


35
36
37
# File 'lib/felflame/system_manager.rb', line 35

def addition_triggers
  @addition_triggers ||= []
end

#attr_triggersHash<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.

Returns:

  • (Hash<Symbol, Array<Symbol>>)


52
53
54
# File 'lib/felflame/system_manager.rb', line 52

def attr_triggers
  @attr_triggers ||= {}
end

#priorityObject

How early this System should be executed in a list of Systems



6
7
8
# File 'lib/felflame/system_manager.rb', line 6

def priority
  @priority
end

#removal_triggersArray<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.

Returns:

  • (Array<Component>)


44
45
46
# File 'lib/felflame/system_manager.rb', line 44

def removal_triggers
  @removal_triggers ||= []
end

#scenesObject



19
20
21
# File 'lib/felflame/system_manager.rb', line 19

def scenes
  @scenes ||= []
end

Class Method Details

.const_cacheObject

Stores the systems in Components. This is needed because calling ‘FelFlame::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



63
64
65
# File 'lib/felflame/system_manager.rb', line 63

def const_cache
  @const_cache || update_const_cache
end

Instance Method Details

#callObject

Manually execute the system a single time



124
125
126
# File 'lib/felflame/system_manager.rb', line 124

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

Examples:

# To clear all triggers that execute this system when a component is added:
FelFlame::Systems::ExampleSystem.clear_triggers :addition_triggers
# Same as above but for when a component is removed instead
FelFlame::Systems::ExampleSystem.clear_triggers :removal_triggers
# Same as above but for when a component has a certain attribute changed
FelFlame::Systems::ExampleSystem.clear_triggers :attr_triggers

# Clear a trigger from a specific component
FelFlame::Systems::ExampleSystem.clear_triggers :addition_triggers, FelFlame::Component::ExampleComponent[0]
# Clear a trigger from a specific component manager
FelFlame::Systems::ExampleSystem.clear_triggers :addition_triggers, FelFlame::Component::ExampleComponent

# Clear the trigger that executes a system when the ':example_attr' is changes
FelFlame::Systems::ExampleSystem.clear_triggers :attr_triggers, :example_attr

Parameters:

  • trigger_types (:Symbols)

    One or more of the following trigger types: :addition_triggers, :removal_triggers, or :attr_triggers. If attr_triggers is used then you may pass attributes you wish to be cleared as symbols in this parameter as well

  • component_or_manager (Component or ComponentManager) (defaults to: nil)

    The object to clear triggers from. Use Nil to clear triggers from all components associated with this system.

Returns:

  • (Boolean)

    true



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/felflame/system_manager.rb', line 157

def clear_triggers(*trigger_types, component_or_manager: nil)
  trigger_types = %i[addition_triggers removal_triggers attr_triggers] if trigger_types.empty?

  if trigger_types.include? :attr_triggers
    if (trigger_types - %i[addition_triggers
                           removal_triggers
                           attr_triggers]).empty?

      if component_or_manager.nil?
        # remove all attrs
        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 attr_triggers[component_or_manager].nil?
          attr_triggers[component_or_manager].each do |attr|
            component_or_manager.attr_triggers[attr].delete self
          end
          attr_triggers[component_or_manager] = []
        end
      end

    elsif component_or_manager.nil?

      (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
      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 & %i[removal_triggers addition_triggers] - [:attr_triggers]).each do |trigger_type|
    if component_or_manager.nil?
      # remove all removal triggers
      send(trigger_type).each do |trigger|
        trigger.send(trigger_type).delete self
      end
      send("#{trigger_type}=", [])
    else
      # remove removal trigger relevant to comp/man
      send(trigger_type).delete component_or_manager
      component_or_manager.send(trigger_type).delete self
    end
  end
  true
end

#redefine(&block) ⇒ Object

Redefine what code is executed by this System when it is called upon.

Parameters:

  • 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.



130
131
132
# File 'lib/felflame/system_manager.rb', line 130

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

Parameters:

  • component_or_manager (Component or ComponentManager)

    The component or component manager to trigger this system when added

Returns:

  • (Boolean)

    true



227
228
229
230
231
# File 'lib/felflame/system_manager.rb', line 227

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) ⇒ Boolean

Add a component or component manager so that it triggers this system when a component’s attribute is changed.

Returns:

  • (Boolean)

    true



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/felflame/system_manager.rb', line 244

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 attr_triggers[component_or_manager].nil?
    attr_triggers[component_or_manager] = [attr]
  else
    attr_triggers[component_or_manager] |= [attr]
  end
  true
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

Parameters:

  • component_or_manager (Component or ComponentManager)

    The component or component manager to trigger this system when removed

Returns:

  • (Boolean)

    true



236
237
238
239
240
# File 'lib/felflame/system_manager.rb', line 236

def trigger_when_removed(component_or_manager)
  self.removal_triggers |= [component_or_manager]
  component_or_manager.removal_triggers |= [self]
  true
end