-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelTimer.cs
More file actions
154 lines (133 loc) · 3.69 KB
/
LevelTimer.cs
File metadata and controls
154 lines (133 loc) · 3.69 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using UnityEngine;
using TMPro;
public class LevelTimer : MonoBehaviour
{
[Header("Timer Display")]
[SerializeField] private TextMeshProUGUI timerText;
[Header("Level Settings")]
[SerializeField] private stageControl levelController;
private float startTime;
private float endTime;
private float currentTime;
private bool timerRunning = false;
private bool timerFinished = false;
// Singleton pattern to easily access timer from other scripts
public static LevelTimer Instance { get; private set; }
private void Awake()
{
// Implement singleton pattern
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
// Find level controller if not assigned
if (levelController == null)
{
levelController = FindObjectOfType<stageControl>();
}
}
private void Start()
{
// Initialize timer display
if (timerText != null)
{
timerText.text = "00:00.000";
}
// Automatically start the timer when the level loads
StartTimer();
}
private void Update()
{
// Update timer while it's running
if (timerRunning && !timerFinished)
{
currentTime = Time.time - startTime;
UpdateTimerDisplay();
}
// Enhanced check for level completion
if (levelController != null && levelController.stageComplete && timerRunning && !timerFinished)
{
EndTimer();
}
}
public void StartTimer()
{
if (!timerRunning && !timerFinished)
{
startTime = Time.time;
timerRunning = true;
}
}
private void StopTimer()
{
if (timerRunning)
{
endTime = currentTime;
timerRunning = false;
}
}
public void EndTimer()
{
if (timerRunning && !timerFinished)
{
StopTimer();
timerFinished = true;
// Store completion time in PlayerPrefs for use on end screen
PlayerPrefs.SetFloat("CompletionTime", endTime);
PlayerPrefs.SetString("CompletionUsername", SQLManager.CurrentUsername);
PlayerPrefs.Save();
// Save level completion time to database
SaveLevelTime();
}
}
public void ResetTimer()
{
timerRunning = false;
timerFinished = false;
currentTime = 0f;
if (timerText != null)
{
timerText.text = "00:00.000";
}
}
private void UpdateTimerDisplay()
{
if (timerText != null)
{
timerText.text = FormatTime(currentTime);
}
}
private string FormatTime(float timeInSeconds)
{
int minutes = (int)(timeInSeconds / 60);
int seconds = (int)(timeInSeconds % 60);
int milliseconds = (int)((timeInSeconds * 1000) % 1000);
return string.Format("{0:00}:{1:00}.{2:000}", minutes, seconds, milliseconds);
}
public float GetCurrentTime()
{
return timerRunning ? currentTime : endTime;
}
private async void SaveLevelTime()
{
// check if user is logged in
if (SQLManager.IsLoggedIn)
{
try
{
// Get current difficulty setting from PlayerPrefs
string difficulty = PlayerPrefs.GetString("GameDifficulty", "NORMAL");
// Use ScoreManager to save the score
var result = await ScoreManager.SaveScore(endTime, difficulty);
}
catch
{
}
}
}
}