-
Notifications
You must be signed in to change notification settings - Fork 0
/
randChoose1.cs
477 lines (421 loc) · 15.5 KB
/
randChoose1.cs
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class randChoose1 : MonoBehaviour {
public GameObject hintText;
public GameObject hintTextBack;
public GameObject progressHintText;
public SteamVR_Action_Boolean trigger;
public AudioSource audioHint;
public Transform leftController;
public Transform rightController;
private Vector3 leftPos1;
private Vector3 rightPos1;
private Vector3 leftPos2;
private Vector3 rightPos2;
private Vector3 leftPos1r;
private Vector3 rightPos1r;
private Vector3 leftPos2r;
private Vector3 rightPos2r;
private Vector3 offset;
//public Vector3 Offset;
public static int randNum = -1;
private HashSet<int> numbers;
private GameObject []findBall;
private GameObject [] findSideBall;
private float scaleOfInterfaceBall = 0.01f;
private float gapBetweenBalls = 0.04f;
private float gapBetweenCenter = 0.1f;
private float gapForY = 0.05f;
private float Yoffset = -0.25f;
private float Zoffset = 1f;
private int limit = 0;
private int triedNum = 1;
private string hintString = "Hey U r done";
private string lastHintString;
private string progressHintString;
private bool ballsDisappeared = false;
private bool start = false;
public static bool isWaiting = false;
int[] randomNumbers;
int hasused = 0;
static string triggerPress = "扣下任意扳机";
static string measureArmLengthLeft = "双臂水平平举,扣左侧扳机";
static string measureArmLengthRight = "双臂水平平举,扣右侧扳机";
static string measureArmHeightLeft = "双臂竖直上举,扣左侧扳机";
static string measureArmHeightRight = "双臂竖直上举,扣右侧扳机";
static string afterTurn = "记忆两侧小球位置,按下空格以继续";
static string afterLab ="实验完成";
public Transform cameraTransform;
int bigTurnNum = 0;
int labState = 0;//取值0-3
int smallState = 0;//取值0-4
int maxLabState = 3;
int maxSmallState = 4;
float proTime = 0.0f;
float NextTime = 0.0f;
int timer_i = 0;
float timerGap = 1f;//计时器间隔
int maxLimit = 50;
float dirLimit = 0.2f;
float disFromOriDirection = 0;
public GameObject warningText;
bool sideBallsDisappeared = false;
Color backBall = Color.white;
Color lightBall = Color.red;
bool waitingForSpace = false;
// Use this for initialization
void Start () {
findBall = new GameObject[50];
findSideBall = new GameObject[50];
for(int i=0;i<50;i++)
{
findSideBall[i] = GameObject.Find("Sphere (" + i + ")");
findBall[i] = GameObject.Find("sphere (" + i + ")");
}
numbers = new HashSet<int>();
//hintText = GameObject.Find("New Text");
initBallInterface();
init();
//changeRandNum(); // testing
cameraTransform = GameObject.Find("Camera").transform;
labState = 0;
smallState = 0;
audioHint = this.GetComponent<AudioSource>();
//audioHint.Play();
changeHint(triggerPress);
}
void timer() {
timer_i += 1;
Debug.Log(timer_i);
}
// Update is called once per frame
void Update () {
//hintText.transform.position += Offset;
initBallInterface();
if (trigger.GetStateDown(SteamVR_Input_Sources.Any))
{
isWaiting = false;
if (IsInvoking())
{
return;
}
if (labState == 0)
{
if (smallState == 0)
{
changeHint(measureArmLengthLeft);
smallState = 1;
}
else if (smallState == 1 && trigger.GetStateDown(SteamVR_Input_Sources.LeftHand))
{
leftPos1 = leftController.position;
changeHint(measureArmLengthRight);
smallState = 2;
}
else if (smallState == 2 && trigger.GetStateDown(SteamVR_Input_Sources.RightHand))
{
rightPos1 = rightController.position;
changeHint(measureArmHeightLeft);
smallState = 3;
}
else if (smallState == 3 && trigger.GetStateDown(SteamVR_Input_Sources.LeftHand))
{
leftPos2 = leftController.position;
changeHint(measureArmHeightRight);
smallState = 4;
}
else if (smallState == 4 && trigger.GetStateDown(SteamVR_Input_Sources.RightHand))
{
rightPos2 = rightController.position;
Vector3 o = (leftPos1 + rightPos1) / 2;
GameObject.Find("OriPos").transform.position = new Vector3(o.x, -1, o.z);
leftPos1r = leftPos1 - o;
leftPos2r = leftPos2 - o;
rightPos1r = rightPos1 - o;
rightPos2r = rightPos2 - o;
float rex = (Mathf.Abs(leftPos2r.x) + Mathf.Abs(rightPos2r.x)) / 2;
float rey = (Mathf.Abs(leftPos1r.y) + Mathf.Abs(rightPos1r.y)) / 2;
float rez = (Mathf.Abs(leftPos2r.z) + Mathf.Abs(rightPos2r.z)) / 2;
move.centralPoint1 = new Vector3(-rex, rey, rez) + o ;
move.centralPoint2 = new Vector3(rex,rey,rez) + o;
float rer = ((leftPos1-move.centralPoint1).magnitude + (leftPos2 - move.centralPoint1).magnitude + (rightPos2 - move.centralPoint2).magnitude + (rightPos1 - move.centralPoint2).magnitude) / 4;
move.halfHandDistance = rer;
LogOut.printLog("radius:" + rer);
nextLabState();
}
}
else if(smallState == 0)
{
//do nothing
}
else if (limit <= maxLimit - 1)//smallState != 0, 已經進入點擊階段
{
limit++;
updateProgressHint();
changeBackColor();
Invoke("changeRandNum", timerGap);
}
else
{
if(smallState >= maxSmallState)
{
nextLabState();
}
else
{
waitingForSpace = true;
changeHint(afterTurn);
ballsDisappear();
sideBallsAppear();
hintText.GetComponent<MeshRenderer>().enabled = true;
}
}
Debug.Log("limit : " + limit);
Debug.Log("labState : " + labState);
Debug.Log("smallState : " + smallState);
}
//Debug.Log("???"+waitingForSpace);
if (Input.GetKeyDown(KeyCode.Space) && waitingForSpace && !(labState == 0))
{
sideBallsDisappear();
LogOut.printLog("round "+labState+
", central 1:(" +move.centralPoint1.x+","+
+move.centralPoint1.y + "," +
+move.centralPoint1.z +")"+
", central 2:(" + move.centralPoint2.x + "," +
+move.centralPoint2.y + "," +
+move.centralPoint2.z + ")");
nextSmallState();
waitingForSpace = false;
}
if(!LogOut.fixedMode && labState != 0)
{
move.moveBall(labState - 1);
}
disFromOriDirection = new Vector4(cameraTransform.rotation.w-1, cameraTransform.rotation.x, cameraTransform.rotation.y, cameraTransform.rotation.z).magnitude;
//Debug.Log(disFromOriDirection);
//Debug.Log(cameraTransform.rotation);
if (Input.GetKeyDown(KeyCode.Backspace))
{
if (labState >=1)
{
limit = 1;
updateProgressHint();
LogOut.printLog("回退成功");
changeBackColor();
Invoke("changeRandNum", timerGap);
changeHint("回退成功" + triedNum);
triedNum++;
Debug.Log("ddddddddddddddddd");
}
}
if (Input.GetKeyDown(KeyCode.DownArrow) && smallState == 0) {
Vector3 offset1 = new Vector3(0, -0.05f, 0);
for (int i = 0; i < 50; i++)
{
findSideBall[i].transform.position += offset1;
}
move.centralPoint1 += offset1;
move.centralPoint2 += offset1;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && smallState == 0)
{
Vector3 offset2 = new Vector3(0, 0.05f, 0);
for (int i = 0; i < 50; i++)
{
findSideBall[i].transform.position += offset2;
}
move.centralPoint1 += offset2;
move.centralPoint2 += offset2;
}
}
void nextLabState()
{
switch (labState)
{
case 3:
numbers.Clear();
ballsDisappear();
sideBallsDisappear();
hintText.GetComponent<MeshRenderer>().enabled = true;
changeHint(afterLab);
break;
default:
limit = 0;
labState++;
smallState = 0;
updateProgressHint();
numbers.Clear();
ballsDisappear();
sideBallsAppear();
move.moveBall(labState - 1);
hintText.GetComponent<MeshRenderer>().enabled = true;
changeHint(afterTurn);
waitingForSpace = true;
break;
}
}
void nextSmallState()
{
if(smallState != maxSmallState)
{
smallState++;
limit = 1;
updateProgressHint();
numbers.Clear();
ballsAppear();
sideBallsDisappear();
hintText.GetComponent<MeshRenderer>().enabled = false;
Invoke("changeRandNum", timerGap);
}
else
{
sideBallsDisappear();
nextLabState();
}
}
void ready()
{
ballsAppear();
sideBallsDisappear();
changeBackColor();
hintText.GetComponent<MeshRenderer>().enabled = false;
Invoke("changeRandNum", timerGap);
limit++;
updateProgressHint();
smallState = 1;
}
void init() {
// for initialize the whole flow
changeHint(measureArmLengthLeft);
ballsDisappear();
hintText.GetComponent<MeshRenderer>().enabled = true;
}
// auto setting coordinate of balls in interface
void initBallInterface() {
for (int i = 0; i < 50; i++) {
findBall[i].transform.SetPositionAndRotation(new Vector3(getX(i), getY(i), getZ(i)), findBall[i].transform.rotation);
}
}
void changeHint(string newhint)
{
hintString = newhint;
hintText.GetComponent<TextMesh>().text = hintString;
hintTextBack.GetComponent<TextMesh>().text = hintString;
LogOut.printLog("frontHint changed to " + hintString);
}
void updateProgressHint() {
progressHintString = "当前小球 " + limit.ToString() + "/"+ maxLimit + " 当前轮数 " + smallState.ToString() + "/"+maxSmallState.ToString()+ " 当前距离 "+labState.ToString()+"/"+maxLabState.ToString();
progressHintText.GetComponent<TextMesh>().text = progressHintString;
LogOut.printLog("progress changed to " + progressHintString);
}
//
void changeRandNum() {
changeBackColor();
if(randomNumbers == null || hasused == maxLimit)
{
randomNumbers = GetRandomUnrepeatArray(0, 49, maxLimit);
hasused = 0;
}
randNum = randomNumbers[hasused];
hasused++;
//changeHint("请点击"+randNum+"号小球");
//numbers.Add(randNum);
changeColor();
audioHint.Play();
isWaiting = true;
LogOut.printLog(randNum + "号小球亮起");
}
// change into a new color
void changeColor() {
if (ballsDisappeared) {
return;
}
Renderer render1 = findSideBall[randNum].GetComponent<Renderer>();
Renderer render2 = findBall[randNum].GetComponent<Renderer>();
render1.material.color = lightBall;
render2.material.color = lightBall;
}
void changeBackColor() {
for (int i = 0; i < 50; i++)
{
Renderer render1 = findSideBall[i].GetComponent<Renderer>();
Renderer render2 = findBall[i].GetComponent<Renderer>();
render1.material.color = backBall;
render2.material.color = backBall;
}
}
void ballsDisappear() {
if (ballsDisappeared) {
return ;
}
changeBackColor();
//for (int i = 0; i < 50; i++) {
// if (findBall[i] != null) {
// findBall[i].GetComponent<MeshRenderer>().enabled = false;
// }
//}
ballsDisappeared = true;
progressHintText.GetComponent<MeshRenderer>().enabled = false;
}
void sideBallsDisappear()
{
if (sideBallsDisappeared)
{
return;
}
for (int i = 0; i < 50; i++)
{
if (findSideBall[i] != null)
{
findSideBall[i].GetComponent<MeshRenderer>().enabled = false;
}
}
sideBallsDisappeared = true;
}
void sideBallsAppear()
{
if (!sideBallsDisappeared)
{
return;
}
for (int i = 0; i < 50; i++)
{
if (findSideBall[i] != null)
{
findSideBall[i].GetComponent<MeshRenderer>().enabled = true;
}
}
sideBallsDisappeared = false;
}
void ballsAppear() {
for (int i = 0; i < 50; i++)
{
if (findBall[i] != null)
{
findBall[i].GetComponent<MeshRenderer>().enabled = true;
}
}
ballsDisappeared = false;
progressHintText.GetComponent<MeshRenderer>().enabled = true;
}
float getSide(int i) {
return i / 25 == 0 ? -1 : 1;
}
float getX(int i) {
float offset = 4 - (i % 5);
return offset * getSide(i) * gapBetweenBalls + gapBetweenCenter * getSide(i);
}
float getY(int i)
{
float offset = 4 - (i / 5) % 5;
return offset * gapBetweenBalls * 1.414f + gapBetweenCenter + Yoffset;
}
float getZ(int i)
{
float offset = i % 5;
return offset * gapBetweenBalls + Zoffset;
}
public int[] GetRandomUnrepeatArray(int minValue, int maxValue, int count) { System.Random rnd = new System.Random(); int length = maxValue - minValue + 1; byte[] keys = new byte[length]; rnd.NextBytes(keys); int[] items = new int[length]; for (int i = 0; i < length; i++) { items[i] = i + minValue; } System.Array.Sort(keys, items); int[] result = new int[count]; System.Array.Copy(items, result, count); return result; }
}