-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendMenuManager.cs
More file actions
131 lines (112 loc) · 3.89 KB
/
endMenuManager.cs
File metadata and controls
131 lines (112 loc) · 3.89 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
using System.Collections;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class EndMenuManager : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private TextMeshProUGUI usernameText;
[SerializeField] private TextMeshProUGUI completionTimeText;
[SerializeField] private TextMeshProUGUI messageText;
[SerializeField] private TextMeshProUGUI improvementText;
private string username;
private float completionTime;
private bool scoreSaveAttempted = false;
private void Start()
{
// Load saved data from PlayerPrefs
username = PlayerPrefs.GetString("CompletionUsername", "Guest");
completionTime = PlayerPrefs.GetFloat("CompletionTime", 0f);
// Display username
if (usernameText != null)
{
if (string.IsNullOrEmpty(username) || username == "Guest")
{
usernameText.text = "Well done, Player!";
}
else
{
usernameText.text = "Well done, " + username + "!";
}
}
// Display completion time if available
if (completionTimeText != null && completionTime > 0)
{
completionTimeText.text = "Completion Time: " + FormatTime(completionTime);
}
else if (completionTimeText != null)
{
completionTimeText.gameObject.SetActive(false);
}
if (improvementText != null)
{
improvementText.gameObject.SetActive(false);
}
// Check if user is logged in
if (!SQLManager.IsLoggedIn && messageText != null)
{
messageText.text = "Log in to save your scores!";
}
else if (messageText != null)
{
messageText.text = "Checking your score...";
AttemptSaveScore();
}
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
private void AttemptSaveScore()
{
if (SQLManager.IsLoggedIn && completionTime > 0 && !scoreSaveAttempted)
{
scoreSaveAttempted = true;
string difficulty = PlayerPrefs.GetString("GameDifficulty", "NORMAL");
StartCoroutine(SaveScoreCoroutine(completionTime, difficulty));
}
}
private IEnumerator SaveScoreCoroutine(float time, string difficulty)
{
// Start the async operation to save the score
var task = ScoreManager.SaveScore(time, difficulty);
// Wait until the task completes
while (!task.IsCompleted)
yield return null;
try
{
var result = task.Result;
// Update the message text with the result
if (messageText != null)
{
messageText.text = result.success
? result.message
: "Error saving your score. Please try again.";
}
// Show improvement text if applicable
if (result.success && result.data != null && result.data.improved &&
improvementText != null && !result.data.firstTime)
{
improvementText.text = $"You improved by {FormatTime(result.data.improvement)}!";
improvementText.gameObject.SetActive(true);
}
}
catch (System.Exception)
{
if (messageText != null)
{
messageText.text = "Error saving your score. Please try again.";
}
}
}
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);
}
// Button methods
public void GoToMainMenu()
{
SceneManager.LoadScene("Start Menu");
}
}