Class: Zif::Services::ActionService
- Inherits:
-
Object
- Object
- Zif::Services::ActionService
- Defined in:
- lib/zif/services/action_service.rb
Overview
This service facilitates keeping track of and running Action::Actionables that need updating every tick.
Specifically, every tick Game will invoke #run_all_actions on this service. In turn, this calls Action::Actionable#perform_actions on all Action::Actionables objects which have been previously registered using #register_actionable.
Instance Attribute Summary collapse
-
#actionables ⇒ Array<Zif::Action::Actionable>
readonly
The list of Action::Actionables to check each tick.
1. Public Interface collapse
-
#demote_actionable(actionable) ⇒ Object
Moves an Actions::Actionable to the end of the #actionables array, so it is processed last.
-
#initialize ⇒ ActionService
constructor
Calls #reset_actionables.
-
#promote_actionable(actionable) ⇒ Object
Moves an Actions::Actionable to the start of the #actionables array, so it is processed first.
-
#register_actionable(actionable) ⇒ Object
Adds an Actions::Actionable to the #actionables array.
-
#remove_actionable(actionable) ⇒ Object
Removes an Actions::Actionable from the #actionables array.
-
#reset_actionables ⇒ Object
Resets the #actionables array.
2. Private-ish methods collapse
-
#run_all_actions ⇒ Object
private
Iterate through #actionables and invoke Action::Actionable#perform_actions Unless you are doing something advanced, this should be invoked automatically by Game#standard_tick.
Constructor Details
#initialize ⇒ ActionService
Calls #reset_actionables
17 18 19 |
# File 'lib/zif/services/action_service.rb', line 17 def initialize reset_actionables end |
Instance Attribute Details
#actionables ⇒ Array<Zif::Action::Actionable> (readonly)
Returns The list of Action::Actionables to check each tick.
11 12 13 |
# File 'lib/zif/services/action_service.rb', line 11 def actionables @actionables end |
Instance Method Details
#demote_actionable(actionable) ⇒ Object
Moves an Actions::Actionable to the end of the #actionables array, so it is processed last
51 52 53 |
# File 'lib/zif/services/action_service.rb', line 51 def demote_actionable(actionable) @actionables.push(remove_actionable(actionable)) end |
#promote_actionable(actionable) ⇒ Object
Moves an Actions::Actionable to the start of the #actionables array, so it is processed first
45 46 47 |
# File 'lib/zif/services/action_service.rb', line 45 def promote_actionable(actionable) @actionables.unshift(remove_actionable(actionable)) end |
#register_actionable(actionable) ⇒ Object
Adds an Actions::Actionable to the #actionables array.
28 29 30 31 32 33 34 35 |
# File 'lib/zif/services/action_service.rb', line 28 def register_actionable(actionable) unless actionable.is_a?(Zif::Actions::Actionable) raise ArgumentError, 'Zif::Services::ActionService#register_actionable:' / " #{actionable} is not a Zif::Actions::Actionable" end @actionables << actionable end |
#remove_actionable(actionable) ⇒ Object
Removes an Actions::Actionable from the #actionables array.
39 40 41 |
# File 'lib/zif/services/action_service.rb', line 39 def remove_actionable(actionable) @actionables.delete(actionable) end |
#reset_actionables ⇒ Object
Resets the #actionables array.
22 23 24 |
# File 'lib/zif/services/action_service.rb', line 22 def reset_actionables @actionables = [] end |
#run_all_actions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iterate through #actionables and invoke Action::Actionable#perform_actions Unless you are doing something advanced, this should be invoked automatically by Game#standard_tick
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/zif/services/action_service.rb', line 61 def run_all_actions actionables_count = @actionables&.length return false unless actionables_count&.positive? # Avoid blocks here. idx = 0 while idx < actionables_count @actionables[idx].perform_actions idx += 1 end true end |