Class: Three::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/three/core/clock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_startObject

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_timeObject (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_timeObject (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

#runningObject (readonly)

Returns the value of attribute running.



6
7
8
# File 'lib/three/core/clock.rb', line 6

def running
  @running
end

#start_timeObject (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_deltaObject



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_timeObject



32
33
34
35
# File 'lib/three/core/clock.rb', line 32

def get_elapsed_time
  get_delta
  @elapsed_time
end

#startObject



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

#stopObject



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