sokoban
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
extends Label

var time_elapsed := 0.0


func _ready():
	MainInstances.stopwatch = self
	set_process(false)


func start():
	set_process(true)


func _exit_tree():
	MainInstances.stopwatch = null


func reset():
	time_elapsed = 0.0
	text = _format_seconds(time_elapsed, true)
	set_process(false)


func _process(delta):
	time_elapsed += delta
	text = _format_seconds(time_elapsed, true)


func _format_seconds(time: float, use_milliseconds: bool) -> String:
	var minutes := time / 60
	var seconds := fmod(time, 60)

	if not use_milliseconds:
		return "%02d:%02d" % [minutes, seconds]

	var milliseconds := fmod(time, 1) * 100

	return "%02d:%02d:%02d" % [minutes, seconds, milliseconds]