Class: AdventureRL::TimingHandler

Inherits:
Object
  • Object
show all
Includes:
Helpers::Error
Defined in:
lib/AdventureRL/TimingHandler.rb

Overview

The TimingHandler has nice methods to handle timing. It can #set_timeout or #set_interval for methods.

Constant Summary

Constants included from Helpers::Error

Helpers::Error::PADDING, Helpers::Error::STACK_TRACE_PADDING, Helpers::Error::STACK_TRACE_SIZE

Instance Method Summary collapse

Methods included from Helpers::Error

directory_exists?, error, error_no_directory, error_no_file, file_exists?

Constructor Details

#initializeTimingHandler

Returns a new instance of TimingHandler.



7
8
9
10
11
12
13
14
15
# File 'lib/AdventureRL/TimingHandler.rb', line 7

def initialize
  @queue = {
    timeouts:  [],
    intervals: []
  }
  @elapsed_seconds = 0.0
  @deltatime = Deltatime.new
  @is_running = true
end

Instance Method Details

#continueObject



28
29
30
31
32
# File 'lib/AdventureRL/TimingHandler.rb', line 28

def continue
  return  if (is_running?)
  @is_running = true
  reset
end

#has_interval?(id) ⇒ Boolean

Returns true if the given id exists as an interval, and false if not.

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/AdventureRL/TimingHandler.rb', line 134

def has_interval? id
  return @queue[:intervals].any? do |interval|
    next interval[:id] == id
  end
end

#has_timeout?(id) ⇒ Boolean

Returns true if the given id exists as a timeout, and false if not.

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/AdventureRL/TimingHandler.rb', line 126

def has_timeout? id
  return @queue[:timeouts].any? do |timeout|
    next timeout[:id] == id
  end
end

#is_paused?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/AdventureRL/TimingHandler.rb', line 43

def is_paused?
  return !is_running?
end

#is_running?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/AdventureRL/TimingHandler.rb', line 39

def is_running?
  return !!@is_running
end

#pauseObject



34
35
36
37
# File 'lib/AdventureRL/TimingHandler.rb', line 34

def pause
  return  if (is_paused?)
  @is_running = false
end

#remove_interval(id) ⇒ Object Also known as: clear_interval

If you passed an :id to your interval when you set it with #set_interval, then you can remove / clear it by calling this method and passing the same id. If you did not pass an id, then your interval will be running endlessly!



117
118
119
120
121
# File 'lib/AdventureRL/TimingHandler.rb', line 117

def remove_interval id
  @queue[:intervals].reject! do |interval|
    next interval[:id] == id
  end
end

#remove_timeout(id) ⇒ Object Also known as: clear_timeout

If you passed an :id to your timeout when you set it with #set_timeout, then you can remove / clear it before it executes by calling this method and passing the same id.



106
107
108
109
110
# File 'lib/AdventureRL/TimingHandler.rb', line 106

def remove_timeout id
  @queue[:timeouts].reject! do |timeout|
    next timeout[:id] == id
  end
end

#resetObject

Reset the Deltatime



48
49
50
# File 'lib/AdventureRL/TimingHandler.rb', line 48

def reset
  @deltatime.reset
end

#set_interval(args = {}, &block) ⇒ Object Also known as: every

Set an interval for a method. Call a method in regular intervals.

The passed args Hash should include the following keys:

:method

The method to be called. Can be one of the following:

  • a Method – method(:my_method)

  • a Proc – Proc.new { puts 'My method!' }

  • a method name as a Symbol – :my_method

:seconds or :secs

Integer or Float. The time to wait in seconds, before calling the method.

:arguments or :args

Optional Array of arguments, which will be passed to the target method.

:id

Optional value which can be used to remove the interval afterwards. See #remove_interval.

You can also pass a block to the method, which will be used instead of the :method key’s value.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/AdventureRL/TimingHandler.rb', line 89

def set_interval args = {}, &block
  validate_args args, !!block
  _args = get_unified_args args, &block
  at    = get_time_in _args[:seconds]
  @queue[:intervals] << {
    method:    _args[:method],
    interval:  _args[:seconds],
    at:        at,
    arguments: _args[:arguments],
    id:        _args[:id]
  }
end

#set_timeout(args = {}, &block) ⇒ Object Also known as: in

Set a timeout for a method. Call a method after a specified amount of time has passed.

The passed args Hash should include the following keys:

:method

The method to be called. Can be one of the following:

  • a Method – method(:my_method)

  • a Proc – Proc.new { puts 'My method!' }

  • a method name as a Symbol – :my_method

:seconds or :secs

Integer or Float. The time to wait in seconds, before calling the method.

:arguments or :args

Optional Array of arguments, which will be passed to the target method.

:id

Optional value which can be used to remove the timeout afterwards. See #remove_timeout.

You can also pass a block to the method, which will be used instead of the :method key’s value.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/AdventureRL/TimingHandler.rb', line 64

def set_timeout args = {}, &block
  validate_args args, !!block
  _args = get_unified_args args, &block
  at    = get_time_in _args[:seconds]
  @queue[:timeouts] << {
    method:    _args[:method],
    at:        at,
    arguments: _args[:arguments],
    id:        _args[:id]
  }
end

#updateObject

#update should be called every frame, this is where it checks if any methods need to be called and calls them if necessary.



20
21
22
23
24
25
26
# File 'lib/AdventureRL/TimingHandler.rb', line 20

def update
  return  if (is_paused?)
  handle_timeouts
  handle_intervals
  @elapsed_seconds += @deltatime.dt
  @deltatime.update
end