-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRestartLevelController.cs
More file actions
354 lines (294 loc) · 10.8 KB
/
Copy pathRestartLevelController.cs
File metadata and controls
354 lines (294 loc) · 10.8 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// Handles level restart functionality.
/// Press A button 5 times to show restart confirmation dialog.
/// Press A again to restart, or any other button to cancel.
/// </summary>
public class RestartLevelController : MonoBehaviour
{
[Header("Settings")]
[Tooltip("Number of A button presses required to trigger restart prompt")]
public int pressesRequired = 5;
[Tooltip("Time window in seconds to complete the button presses")]
public float pressTimeWindow = 3f;
[Tooltip("Cooldown time after canceling before counting can resume")]
public float cooldownAfterCancel = 1f;
[Header("UI References")]
[Tooltip("Panel to show when restart is prompted")]
public GameObject restartPromptPanel;
[Tooltip("Text to display restart confirmation message")]
public TextMeshProUGUI promptText;
[Tooltip("Text to show current press count (optional)")]
public TextMeshProUGUI countText;
[Header("Scene Settings")]
[Tooltip("Name of the scene to load for restart. If empty, reloads current scene.")]
public string sceneToLoad = "";
// State tracking
private int pressCount = 0;
private float lastPressTime = 0f;
private float lastCancelTime = 0f;
private bool isPromptShowing = false;
// Button state tracking to prevent holding from counting multiple times
private bool wasAPressed = false;
private bool wasBPressed = false;
private bool wasXPressed = false;
private bool wasYPressed = false;
private bool wasLeftTriggerPressed = false;
private bool wasRightTriggerPressed = false;
private bool wasLeftGripPressed = false;
private bool wasRightGripPressed = false;
void Awake()
{
// CRITICAL: Reset all state when the scene loads
// This ensures the counter is always 0 at scene start
ResetState();
}
void Start()
{
// Create UI if not assigned
if (restartPromptPanel == null)
{
CreateDefaultUI();
}
// Hide prompt initially
if (restartPromptPanel != null)
{
restartPromptPanel.SetActive(false);
}
if (countText != null)
{
countText.gameObject.SetActive(false);
}
}
/// <summary>
/// Resets all state variables to their initial values.
/// Called on Awake to ensure clean state when scene loads.
/// </summary>
private void ResetState()
{
pressCount = 0;
lastPressTime = 0f;
lastCancelTime = 0f;
isPromptShowing = false;
// Reset all button states
wasAPressed = false;
wasBPressed = false;
wasXPressed = false;
wasYPressed = false;
wasLeftTriggerPressed = false;
wasRightTriggerPressed = false;
wasLeftGripPressed = false;
wasRightGripPressed = false;
Debug.Log("[RestartLevelController] State reset - counter is now 0");
}
void Update()
{
// Don't process input if in cooldown
if (Time.time - lastCancelTime < cooldownAfterCancel && !isPromptShowing)
{
return;
}
// Check for A button press (Button.One on right controller)
bool aPressed = OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.RTouch);
if (aPressed && !wasAPressed)
{
OnAButtonPressed();
}
wasAPressed = aPressed;
// If prompt is showing, check for other buttons to cancel
if (isPromptShowing)
{
CheckCancelButtonPresses();
}
}
void OnAButtonPressed()
{
if (isPromptShowing)
{
// Confirm restart
RestartLevel();
return;
}
// Check if we're within the time window
// Only reset if we've pressed before (lastPressTime > 0) and too much time has passed
if (lastPressTime > 0f && Time.time - lastPressTime > pressTimeWindow)
{
// Reset count if too much time has passed
pressCount = 0;
Debug.Log("[RestartLevelController] Time window expired, counter reset to 0");
}
pressCount++;
lastPressTime = Time.time;
// Update count display
if (countText != null)
{
countText.gameObject.SetActive(true);
countText.text = $"Restart: {pressCount}/{pressesRequired}";
}
Debug.Log($"[RestartLevelController] A button pressed: {pressCount}/{pressesRequired}");
// Check if we've reached the required count
if (pressCount >= pressesRequired)
{
ShowRestartPrompt();
pressCount = 0;
if (countText != null)
{
countText.gameObject.SetActive(false);
}
}
}
void CheckCancelButtonPresses()
{
// Check B button (Button.Two on right controller)
bool bPressed = OVRInput.Get(OVRInput.Button.Two, OVRInput.Controller.RTouch);
if (bPressed && !wasBPressed)
{
CancelRestart();
wasBPressed = bPressed;
return;
}
wasBPressed = bPressed;
// Check X button (Button.One on left controller)
bool xPressed = OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.LTouch);
if (xPressed && !wasXPressed)
{
CancelRestart();
wasXPressed = xPressed;
return;
}
wasXPressed = xPressed;
// Check Y button (Button.Two on left controller)
bool yPressed = OVRInput.Get(OVRInput.Button.Two, OVRInput.Controller.LTouch);
if (yPressed && !wasYPressed)
{
CancelRestart();
wasYPressed = yPressed;
return;
}
wasYPressed = yPressed;
// Check triggers
bool leftTrigger = OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.LTouch);
bool rightTrigger = OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.RTouch);
if (leftTrigger && !wasLeftTriggerPressed)
{
CancelRestart();
wasLeftTriggerPressed = leftTrigger;
return;
}
wasLeftTriggerPressed = leftTrigger;
if (rightTrigger && !wasRightTriggerPressed)
{
CancelRestart();
wasRightTriggerPressed = rightTrigger;
return;
}
wasRightTriggerPressed = rightTrigger;
// Check grips
bool leftGrip = OVRInput.Get(OVRInput.Button.PrimaryHandTrigger, OVRInput.Controller.LTouch);
bool rightGrip = OVRInput.Get(OVRInput.Button.PrimaryHandTrigger, OVRInput.Controller.RTouch);
if (leftGrip && !wasLeftGripPressed)
{
CancelRestart();
wasLeftGripPressed = leftGrip;
return;
}
wasLeftGripPressed = leftGrip;
if (rightGrip && !wasRightGripPressed)
{
CancelRestart();
wasRightGripPressed = rightGrip;
return;
}
wasRightGripPressed = rightGrip;
}
void ShowRestartPrompt()
{
isPromptShowing = true;
if (restartPromptPanel != null)
{
restartPromptPanel.SetActive(true);
}
Debug.Log("Restart prompt shown. Press A to restart, or any other button to cancel.");
}
void CancelRestart()
{
isPromptShowing = false;
lastCancelTime = Time.time;
if (restartPromptPanel != null)
{
restartPromptPanel.SetActive(false);
}
Debug.Log("Restart canceled.");
}
void RestartLevel()
{
Debug.Log("Restarting level...");
// Hide prompt
if (restartPromptPanel != null)
{
restartPromptPanel.SetActive(false);
}
// Load the specified scene or reload current scene
string sceneName = string.IsNullOrEmpty(sceneToLoad) ? SceneManager.GetActiveScene().name : sceneToLoad;
SceneManager.LoadScene(sceneName);
}
void CreateDefaultUI()
{
// Create a simple canvas for the restart prompt
GameObject canvasObj = new GameObject("RestartPromptCanvas");
canvasObj.transform.SetParent(transform);
Canvas canvas = canvasObj.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = Camera.main;
// Set canvas size and position
RectTransform canvasRect = canvasObj.GetComponent<RectTransform>();
canvasRect.sizeDelta = new Vector2(1f, 0.6f);
canvasRect.position = new Vector3(0, 1.5f, 2f);
canvasRect.rotation = Quaternion.Euler(0, 180, 0);
// Add canvas scaler
CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
scaler.dynamicPixelsPerUnit = 1000;
// Create panel
GameObject panelObj = new GameObject("RestartPromptPanel");
panelObj.transform.SetParent(canvasObj.transform, false);
RectTransform panelRect = panelObj.AddComponent<RectTransform>();
panelRect.anchorMin = Vector2.zero;
panelRect.anchorMax = Vector2.one;
panelRect.sizeDelta = Vector2.zero;
Image panelImage = panelObj.AddComponent<Image>();
panelImage.color = new Color(0.1f, 0.1f, 0.1f, 0.9f);
// Create text
GameObject textObj = new GameObject("PromptText");
textObj.transform.SetParent(panelObj.transform, false);
RectTransform textRect = textObj.AddComponent<RectTransform>();
textRect.anchorMin = Vector2.zero;
textRect.anchorMax = Vector2.one;
textRect.sizeDelta = new Vector2(-0.1f, -0.1f);
promptText = textObj.AddComponent<TextMeshProUGUI>();
promptText.text = "Restart Level?\n\nPress A to confirm\nPress any other button to cancel";
promptText.alignment = TextAlignmentOptions.Center;
promptText.fontSize = 0.08f;
promptText.color = Color.white;
// Assign references
restartPromptPanel = panelObj;
Debug.Log("Created default restart prompt UI");
}
// Public method to manually trigger restart (useful for UI buttons)
public void TriggerRestart()
{
RestartLevel();
}
// Public method to manually show prompt
public void ShowPrompt()
{
ShowRestartPrompt();
}
// Public method to manually cancel
public void CancelPrompt()
{
CancelRestart();
}
}