Class: Mittsu::Clock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_start = true) ⇒ Clock

Returns a new instance of Clock.



5
6
7
8
9
10
11
# File 'lib/mittsu/core/clock.rb', line 5

def initialize(auto_start = true)
  @auto_start = auto_start
  @start_time = 0
  @old_time = 0
  @elapsed_time = 0
  @running = false
end

Instance Attribute Details

#auto_startObject

Returns the value of attribute auto_start.



3
4
5
# File 'lib/mittsu/core/clock.rb', line 3

def auto_start
  @auto_start
end

#elapsed_timeObject

Returns the value of attribute elapsed_time.



3
4
5
# File 'lib/mittsu/core/clock.rb', line 3

def elapsed_time
  @elapsed_time
end

#old_timeObject

Returns the value of attribute old_time.



3
4
5
# File 'lib/mittsu/core/clock.rb', line 3

def old_time
  @old_time
end

#runningObject

Returns the value of attribute running.



3
4
5
# File 'lib/mittsu/core/clock.rb', line 3

def running
  @running
end

#start_timeObject

Returns the value of attribute start_time.



3
4
5
# File 'lib/mittsu/core/clock.rb', line 3

def start_time
  @start_time
end

Instance Method Details

#get_deltaObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mittsu/core/clock.rb', line 29

def get_delta
  diff = 0
  if @auto_start && ! @running
    self.start
  end
  if @running
    new_time = Time.now
    diff = 0.001 * (new_time - @old_time)
    @old_time = new_time
    @elapsed_time += diff
  end
  diff
end

#get_elapsed_timeObject



24
25
26
27
# File 'lib/mittsu/core/clock.rb', line 24

def get_elapsed_time
  self.get_delta
  @elapsed_time
end

#startObject



13
14
15
16
17
# File 'lib/mittsu/core/clock.rb', line 13

def start
  @start_time = Time.now
  @old_time = @start_time
  @running = true
end

#stopObject



19
20
21
22
# File 'lib/mittsu/core/clock.rb', line 19

def stop
  self.get_elapsed_time
  @running = false
end