Class: Zif::Services::ActionService

Inherits:
Object
  • Object
show all
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.

See Also:

  • Action::Actionable

Instance Attribute Summary collapse

1. Public Interface collapse

2. Private-ish methods collapse

Constructor Details

#initializeActionService



17
18
19
# File 'lib/zif/services/action_service.rb', line 17

def initialize
  reset_actionables
end

Instance Attribute Details

#actionablesArray<Zif::Action::Actionable> (readonly)

Returns The list of Action::Actionables to check each tick.

Returns:

  • (Array<Zif::Action::Actionable>)

    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

Parameters:



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

Parameters:



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.

Parameters:



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.

Parameters:



39
40
41
# File 'lib/zif/services/action_service.rb', line 39

def remove_actionable(actionable)
  @actionables.delete(actionable)
end

#reset_actionablesObject

Resets the #actionables array.



22
23
24
# File 'lib/zif/services/action_service.rb', line 22

def reset_actionables
  @actionables = []
end

#run_all_actionsObject

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