-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonManager.cs
More file actions
257 lines (204 loc) · 8.9 KB
/
Copy pathButtonManager.cs
File metadata and controls
257 lines (204 loc) · 8.9 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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace UserInTheBox
{
public class ButtonManager : MonoBehaviour
{
public Transform button1;
public Transform button2;
public Transform button3;
public Transform button4;
public Transform button5;
// Array to simplify looping over all 5
private Transform[] allButtons;
public List<int> buttonSequence; // The predefined sequence (e.g., [1, 2, 3])
private int currentSequenceIndex = 0; // Current position in the sequence
// Used by other scripts, keep public
public int currentTargetButtonIndex = 0; // 0-based index (0..4) of current target
private int currentTargetButton; // Which button number (1..5) is currently the target
private int currentButton; // For GetPreviousButton()
private Transform currentActiveButton; // The button that's currently "active"
public float targetActivationtime = 0;
public bool isComplete = false;
public TextMeshProUGUI sequenceText;
public Transform sphereMarker; // Drag your green sphere here in the Inspector
public void Start()
{
// Initialise array of all buttons (indices 0..4 ↔ button numbers 1..5)
allButtons = new Transform[] { button1, button2, button3, button4, button5 };
// Only initialise if no sequence has been set externally
if (buttonSequence == null || buttonSequence.Count == 0)
{
InitialiseButtonSequence();
}
}
void LateUpdate()
{
if (sphereMarker == null || currentActiveButton == null) return;
// Ensure the sphere stays parented to the current active button
if (sphereMarker.parent != currentActiveButton)
{
sphereMarker.SetParent(currentActiveButton, false);
sphereMarker.localPosition = Vector3.zero;
}
}
/// <summary>
/// Picks 3 random distinct buttons out of 5, builds a sequence of length 3,
/// then shuffles that sequence using ShuffleSequence().
/// </summary>
public void InitialiseButtonSequence()
{
if (allButtons == null || allButtons.Length < 3)
{
Debug.LogError("[ButtonManager] Need at least 3 buttons configured.");
return;
}
// --- Step 1: pick 3 unique random indices from 0..4 (5 buttons) ---
HashSet<int> picks = new HashSet<int>();
while (picks.Count < 3)
{
picks.Add(Random.Range(0, allButtons.Length)); // 0..4
}
// Convert to 1-based button numbers (1..5)
List<int> chosenButtonNumbers = new List<int>();
foreach (int idx in picks)
{
chosenButtonNumbers.Add(idx + 1);
}
// Create the initial sequence of length 3 (will be shuffled next)
buttonSequence = new List<int>
{
chosenButtonNumbers[0],
chosenButtonNumbers[1],
chosenButtonNumbers[2]
};
// --- Step 2: shuffle the sequence order using your existing method ---
ShuffleSequence();
// --- Step 3: reset state and activate the first target ---
currentSequenceIndex = 0;
isComplete = false;
currentTargetButton = buttonSequence[currentSequenceIndex];
currentTargetButtonIndex = currentTargetButton - 1; // 0..4, used by other scripts
SetActiveButton(currentTargetButton);
UpdateSequenceText();
}
public void UpdateSequenceText()
{
// Show the full sequence with the current target highlighted
string display = "";
for (int i = 0; i < buttonSequence.Count; i++)
{
if (i == currentSequenceIndex)
display += $"<color=black>{buttonSequence[i]}</color> "; // highlight current target
else
display += $"{buttonSequence[i]} ";
}
if (sequenceText != null)
sequenceText.text = display;
}
public void SetActiveButton(int buttonNumber)
{
// buttonNumber is 1..5
currentTargetButton = buttonNumber;
currentTargetButtonIndex = buttonNumber - 1; // keep compatible with CheckTrigger / CSVGridSequenceController
currentActiveButton = GetButtonTransform(currentTargetButtonIndex);
targetActivationtime = Time.time;
if (sphereMarker != null && currentActiveButton != null)
{
sphereMarker.SetParent(currentActiveButton, worldPositionStays: false);
// Center on the button
sphereMarker.localPosition = Vector3.zero;
// If your Canvas is very small (e.g., scale 0.001), bump the scale up
sphereMarker.localScale = Vector3.one * 15f;
}
// Loop through all 5 buttons and disable the non-target ones (still visible)
if (allButtons != null)
{
for (int i = 0; i < allButtons.Length; i++)
{
Transform btn = allButtons[i];
if (btn == null) continue;
Button buttonComponent = btn.GetComponent<Button>();
if (buttonComponent == null) continue;
bool isTarget = (i + 1) == buttonNumber; // because i is 0-based, buttonNumber is 1-based
if (isTarget)
{
buttonComponent.interactable = true; // clickable
btn.GetComponent<CanvasRenderer>()?.SetAlpha(1f);
btn.gameObject.SetActive(true); // stays visible
}
else
{
buttonComponent.interactable = false; // NOT clickable
btn.GetComponent<CanvasRenderer>()?.SetAlpha(0.75f);
btn.gameObject.SetActive(true); // still visible
}
}
}
}
public Transform GetButtonTransform(int buttonIndex)
{
// buttonIndex is 0-based: 0..4
if (allButtons == null) return null;
if (buttonIndex < 0 || buttonIndex >= allButtons.Length) return null;
return allButtons[buttonIndex];
}
public bool IsSequenceComplete()
{
bool complete = currentSequenceIndex >= buttonSequence.Count;
return complete;
}
public int GetCurrentTargetButton()
{
return currentTargetButton;
}
public int GetPreviousButton()
{
// Assumes currentSequenceIndex > 0 when called
currentButton = buttonSequence[currentSequenceIndex - 1];
return currentButton;
}
public void MoveToNextButton()
{
currentSequenceIndex++;
if (currentSequenceIndex < buttonSequence.Count)
{
int nextButtonNumber = buttonSequence[currentSequenceIndex];
currentTargetButton = nextButtonNumber;
currentTargetButtonIndex = nextButtonNumber - 1;
SetActiveButton(nextButtonNumber);
UpdateSequenceText();
}
else
{
isComplete = true;
// If you want a new random 3-of-5 sequence automatically, you can instead do:
// InitialiseButtonSequence();
// UpdateSequenceText();
}
}
private void ShuffleSequence()
{
System.Random rng = new System.Random();
int n = buttonSequence.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
int value = buttonSequence[k];
buttonSequence[k] = buttonSequence[n];
buttonSequence[n] = value;
}
// ✅ fixed line: no weird escaping
Debug.Log($"New shuffled sequence: {string.Join(", ", buttonSequence)}");
}
public void NewSequence()
{
// You said you want to choose random 3 buttons from 5,
// then shuffle that sequence of 3. So reuse InitialiseButtonSequence().
InitialiseButtonSequence();
}
}
}