Class: Three::Clock
- Inherits:
-
Object
- Object
- Three::Clock
- Defined in:
- lib/three/core/clock.rb
Instance Attribute Summary collapse
-
#auto_start ⇒ Object
Returns the value of attribute auto_start.
-
#elapsed_time ⇒ Object
readonly
Returns the value of attribute elapsed_time.
-
#old_time ⇒ Object
readonly
Returns the value of attribute old_time.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #get_delta ⇒ Object
- #get_elapsed_time ⇒ Object
-
#initialize(auto_start: true, time_source: nil) ⇒ Clock
constructor
A new instance of Clock.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(auto_start: true, time_source: nil) ⇒ Clock
Returns a new instance of Clock.
8 9 10 11 12 13 14 15 |
# File 'lib/three/core/clock.rb', line 8 def initialize(auto_start: true, time_source: nil) @auto_start = auto_start @time_source = time_source || proc { Process.clock_gettime(Process::CLOCK_MONOTONIC) } @start_time = 0 @old_time = 0 @elapsed_time = 0 @running = false end |
Instance Attribute Details
#auto_start ⇒ Object
Returns the value of attribute auto_start.
5 6 7 |
# File 'lib/three/core/clock.rb', line 5 def auto_start @auto_start end |
#elapsed_time ⇒ Object (readonly)
Returns the value of attribute elapsed_time.
6 7 8 |
# File 'lib/three/core/clock.rb', line 6 def elapsed_time @elapsed_time end |
#old_time ⇒ Object (readonly)
Returns the value of attribute old_time.
6 7 8 |
# File 'lib/three/core/clock.rb', line 6 def old_time @old_time end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
6 7 8 |
# File 'lib/three/core/clock.rb', line 6 def running @running end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
6 7 8 |
# File 'lib/three/core/clock.rb', line 6 def start_time @start_time end |
Instance Method Details
#get_delta ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/three/core/clock.rb', line 37 def get_delta if @auto_start && !@running start return 0 end return 0 unless @running current_time = now diff = current_time - @old_time @old_time = current_time @elapsed_time += diff diff end |
#get_elapsed_time ⇒ Object
32 33 34 35 |
# File 'lib/three/core/clock.rb', line 32 def get_elapsed_time get_delta @elapsed_time end |
#start ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/three/core/clock.rb', line 17 def start @start_time = now @old_time = @start_time @elapsed_time = 0 @running = true self end |
#stop ⇒ Object
25 26 27 28 29 30 |
# File 'lib/three/core/clock.rb', line 25 def stop get_elapsed_time @running = false @auto_start = false self end |