Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
364 lines (281 loc) · 11.3 KB

File metadata and controls

364 lines (281 loc) · 11.3 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using UnityEngine;
public class MovementReader : MonoBehaviour {
private class Movement
{
public Queue<MovementCreatorController.Record> movement;
public MovementCreatorController.Record start;
public MovementCreatorController.Record end;
public float binSize;
public Movement(Queue<MovementCreatorController.Record> m, MovementCreatorController.Record s, MovementCreatorController.Record e, float b)
{
movement = m;
start = s;
end = e;
binSize = b;
}
}
private Movement movement;
private bool started;
private bool finished;
private bool hasMovement;
private MovementCreatorController.Record currentRecord;
private Queue<MovementCreatorController.Record> records;
public GameObject sword;
private GameObject indicatorSword;
private GameObject checkSword;
public GameObject label;
private Queue<Transform> steadyChecker;
private float startTime;
private float finalGrade;
private int totalMovements;
private int steadyFrameCount;
private int steadyDegreeVariation;
private int steadyPositionVariation;
// Use this for initialization
void Start () {
movement = createMovementFromFile(PlayerPrefs.GetString("name"));
records = movement.movement;
totalMovements = records.Count;
started = false;
finished = false;
finalGrade = 0;
steadyChecker = new Queue<Transform>();
checkSword = new GameObject();
//Render start position
indicatorSword = Instantiate(sword);
indicatorSword.name = "indicatorSword";
indicatorSword.AddComponent<GhostSwordController>().sword = indicatorSword;
indicatorSword.transform.position = movement.start.position;
indicatorSword.transform.rotation = movement.start.rotation;
//-- Here lie the magic numbers --//
//Frames before potentially "steady"
steadyFrameCount = Application.targetFrameRate * 2;
//Acceptable variation in degrees before "steady"
steadyDegreeVariation = 10;
//Acceptable variation in position before "steady"
steadyPositionVariation = 10;
//Cap the engine at 60 frames per second, to allow better use of Update()
Application.targetFrameRate = 60;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("escape"))
SystemController.StaticLoadLevel("Start Menu");
bool stable = isStable();
if (!started)
{
//wait till steady in start pos
if (stable && areTransformsSimilar(indicatorSword.transform, sword.transform))
{
//in start position
started = true;
startTime = Time.time;
Text txt = label.GetComponent<Text>();
txt.text = "Go!";
//move indicator sword to end position
indicatorSword.transform.position = movement.end.position;
indicatorSword.transform.rotation = movement.end.rotation;
}
} else
{
if (!finished)
{
//has started movement
//get next record if needed, finish if no more records
if (currentRecord == null)
{
if (records.Count == 0)
{
finished = true;
Text txt = label.GetComponent<Text>();
txt.text = "Good Job!\n Accuracy : " + getLetterGrade(finalGrade / totalMovements);
return;
}
else
{
currentRecord = records.Dequeue();
}
}
float t = Time.time;
//check for valid time range for a given record
if ((t - startTime) <= currentRecord.time + (movement.binSize / 2) && (t - startTime) >= currentRecord.time - (movement.binSize / 2))
{
//at valid time for pos rot check
checkSword.transform.position = currentRecord.position;
checkSword.transform.rotation = currentRecord.rotation;
if (areTransformsSimilar(checkSword.transform, sword.transform))
{
//valid check within time interval, so remove the current transform
currentRecord = null;
finalGrade += getAccuracy(checkSword.transform, sword.transform);
}
}
else if ((t - startTime) > currentRecord.time + (movement.binSize / 2))
{
//failed movement check
started = false;
//reset movement checking queue
records = movement.movement;
Text txt = label.GetComponent<Text>();
txt.text = "Enter Start Position...";
indicatorSword.transform.position = movement.start.position;
indicatorSword.transform.rotation = movement.start.rotation;
}
}
}
}
string getLetterGrade(float grade)
{
if(grade < 0.5)
{
return "F";
} else if (grade < 0.5 + (1/24))
{
return "D-";
}
else if (grade < 0.5 + (2 / 24))
{
return "D";
}
else if (grade < 0.5 + (3 / 24))
{
return "D+";
}
else if (grade < 0.5 + (4 / 24))
{
return "C-";
}
else if (grade < 0.5 + (5 / 24))
{
return "C";
}
else if (grade < 0.5 + (6 / 24))
{
return "C+";
}
else if (grade < 0.5 + (7 / 24))
{
return "B-";
}
else if (grade < 0.5 + (8 / 24))
{
return "B";
}
else if (grade < 0.5 + (9 / 24))
{
return "B+";
}
else if (grade < 0.5 + (10 / 24))
{
return "A-";
}
else if (grade < 0.5 + (11 / 24))
{
return "A";
}
return "A+";
}
float getAccuracy(Transform t1, Transform t2)
{
float ret = 0;
ret += 1-(Quaternion.Angle(t1.rotation, t2.rotation) / steadyDegreeVariation);
ret += 1-(Vector3.Distance(t1.position, t2.position) / steadyPositionVariation);
return ret / 2;
}
bool areTransformsSimilar(Transform t1, Transform t2)
{
bool similar = true;
if (Quaternion.Angle(t1.rotation, t2.rotation) > steadyDegreeVariation)
{
//The angle between some pair of the last steadyFrameCount sword positions is greater than steadyDegreeVariation degrees
similar = false;
}
if (Vector3.Distance(t1.position, t2.position) > steadyPositionVariation)
{
//The distance between some pair of the last steadyFrameCount sword positions is greater than steadyPositionVariation (units unknown)
similar = false;
}
return similar;
}
//Returns true if the sword object has remained mostly stable for the past 30 frames (0.5 seconds at 60fps), will only return true up to once every (steadyFrameCount/framerate) seconds, false otherwise
bool isStable()
{
//Add the current sword location to the steadyChecker
steadyChecker.Enqueue(sword.transform);
//Return false if there aren't enough sample points yet
if (steadyChecker.Count < steadyFrameCount)
return false;
//Assumed true
bool isStable = true;
//remove excess positions if present
while (steadyChecker.Count > steadyFrameCount)
{
steadyChecker.Dequeue();
}
for (int i = 0; i < steadyChecker.Count - 1; i++)
{
for (int j = i + 1; j < steadyChecker.Count; j++)
{
isStable = areTransformsSimilar(steadyChecker.ToArray()[i], steadyChecker.ToArray()[j]);
}
}
if (isStable)
steadyChecker.Clear();
return isStable;
}
Movement createMovementFromFile(string filename)
{
string fullPath = "Assets/Resources/" + filename + ".txt";
if (!File.Exists(fullPath))
{
throw new Exception();
}
//Write some text to file
StreamReader reader = new StreamReader(fullPath);
string input = reader.ReadLine();
string[] inputSplit = input.Split(',');
float binSize = float.Parse(inputSplit[0]);
string[] parseObject;
Vector3 vec;
Quaternion quat;
input = reader.ReadLine();
inputSplit = input.Split('|');
//create vector from input line
parseObject = inputSplit[0].Substring(1, inputSplit[0].Length - 2).Split(',');
vec = new Vector3(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]));
//create quaternion from input line
parseObject = inputSplit[1].Substring(1, inputSplit[1].Length - 2).Split(',');
quat = new Quaternion(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]), float.Parse(parseObject[3]));
MovementCreatorController.Record startPos = new MovementCreatorController.Record(vec, quat, 0);
input = reader.ReadLine();
inputSplit = input.Split('|');
//create vector from input line
parseObject = inputSplit[0].Substring(1, inputSplit[0].Length - 2).Split(',');
vec = new Vector3(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]));
//create quaternion from input line
parseObject = inputSplit[1].Substring(1, inputSplit[1].Length - 2).Split(',');
quat = new Quaternion(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]), float.Parse(parseObject[3]));
MovementCreatorController.Record endPos = new MovementCreatorController.Record(vec, quat, 0);
Queue<MovementCreatorController.Record> movementObject = new Queue<MovementCreatorController.Record>();
while (reader.Peek() != -1)
{
input = reader.ReadLine();
inputSplit = input.Split('|');
parseObject = inputSplit[0].Substring(1, inputSplit[0].Length - 2).Split(',');
//create vector from input line
vec = new Vector3(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]));
parseObject = inputSplit[1].Substring(1, inputSplit[1].Length - 2).Split(',');
//create quaternion from input line
quat = new Quaternion(float.Parse(parseObject[0]), float.Parse(parseObject[1]), float.Parse(parseObject[2]), float.Parse(parseObject[3]));
movementObject.Enqueue(new MovementCreatorController.Record(vec, quat, float.Parse(inputSplit[2])));
}
reader.Close();
return new Movement(movementObject, startPos, endPos, binSize);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.