-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreasureHunterOld
384 lines (332 loc) · 15.8 KB
/
TreasureHunterOld
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
public enum AttachmentRule { KeepRelative, KeepWorld, SnapToTarget }
public class TreasureHunter : MonoBehaviour
{
// Start is called before the first frame update
//public Collectible[] collectibles;
public TreasureHunterInventory inventory;
//public CollectibleTreasure[] collectiblesInScene;
public List<CollectibleTreasure> collectiblesInScene;
public StringIntDictionary strDict;
public CollectibleIntDictionary collDict;
//public Text scoreText;
public GameObject leftPointerObject;
public GameObject rightPointerObject;
//public TextMesh text;
//public TextMesh debugText;
public TextMesh testText;
Vector3 previousPointerPos; //using this for velocity since Unity's broken physics engine won't give it to me otherwise
CollectibleTreasure thingIGrabbed;
public LayerMask collectiblesMask;
public Light light;
//public GameObject ground;
private bool lost;
private bool win;
//private AudioSource scream;
int totalPoints = 0;
int totalItems = 0;
int d;
//public GameObject collectible1;
void Start()
{
d = D(Camera.position+prevForwardVector, Camera.position, Camera.position + Camera.forward);
prevForwardVector = Camera.forward;
prevYawRelativeToCenter = angleBetweenVectors(Camera.forward,VRTrackingOrigin.position-Camera.position);
/*
OLD CODE
*/
lost = false;
win = false;
//testText.text = "WHer eis yext";
//scream = GetComponent<AudioSource>();
/*
OLD CODE
*/
} //end of start
// Update is called once per frame
void Update()
{
howMuchUserRotated = angleBetweenVectors(prevForwardVector,Camera.forward);
directionUserRotated = (d(Camera.position+prevForwardVector, Camera.position, Camera.position + Camera.forward)<0)?1:-1;
deltaYawRelativeToCenter = prevYawRelativeToCenter-angleBetweenVectors(Camera.forward,VRTrackingOrigin.position-Camera.position);
distanceFromCenter = (Camera.position-VRTrackingOrigin.position).magnitude;
longestDimensionOfPE = 5;
howMuchToAccelerate=((deltaYawRelativeToCenter<0)? -decelerateThreshold [.13]: accelerateThreshold[.30]) * howMuchUserRotated * directionUserRotated * clamp(distanceFromCenter/longestDimensionOfPE/2,0,1);
VRTrackingOrigin.RotateAround(Camera.position,(0,1,0),howMuchToAccel);
prevForwardVector=Camera.forward;
prevYawRelativeToCenter=angleBetweenVectors(Camera.forward,VRTrackingOrigin.position-Camera.position);
/*
OLD CODE
*/
if (!lost)
{
if (!win)
{
if (OVRInput.GetDown(OVRInput.RawButton.LIndexTrigger))
{
//RaycastHit outHit;
forceGrab(false);
//if (Physics.Raycast(leftPointerObject.transform.position, leftPointerObject.transform.forward, out outHit, 100.0f))
//{
/*totalPoints += outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Value;
totalItems++;
//Debug.DrawRay(cameraTransform.position, cameraTransform.forward * 100.0f, Color.yellow);
print(outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Name);
if (!strDict.ContainsKey(outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Name))
{
strDict.Add(outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Name, 1);
}
else
{
strDict[outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Name] = strDict[outHit.collider.gameObject.GetComponent<CollectibleTreasure>().Name] + 1;
}
Destroy(outHit.collider.gameObject);
*/
//text.text = "You have " + totalItems + " items worth " + totalPoints + " points! -Evan & Lily";
//}
// change text.text
}
else if (OVRInput.GetDown(OVRInput.RawButton.X))
{
Collider[] overlappingThings = Physics.OverlapSphere(leftPointerObject.transform.position, 10, collectiblesMask);
if (overlappingThings.Length > 0)
{
CollectibleTreasure nearestCollectible = getClosestHitObject(overlappingThings);
attachGameObjectToAChildGameObject(nearestCollectible.gameObject, leftPointerObject, AttachmentRule.SnapToTarget, AttachmentRule.SnapToTarget, AttachmentRule.KeepWorld, true);
//I'm not bothering to check for nullity because layer mask should ensure I only collect collectibles.
thingIGrabbed = nearestCollectible.gameObject.GetComponent<CollectibleTreasure>();
}
//scoreText.text = "You have " + totalItems + " items worth " + totalPoints + " points! -Evan & Lily";
}
else if (OVRInput.GetDown(OVRInput.RawButton.Y))
{
forceGrab(true);
}
else if (OVRInput.GetDown(OVRInput.RawButton.LThumbstick))
{
Collider[] overlappingThings = Physics.OverlapSphere(leftPointerObject.transform.position, 0.01f, collectiblesMask);
if (overlappingThings.Length > 0)
{
attachGameObjectToAChildGameObject(overlappingThings[0].gameObject, leftPointerObject, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, true);
//I'm not bothering to check for nullity because layer mask should ensure I only collect collectibles.
thingIGrabbed = overlappingThings[0].gameObject.GetComponent<CollectibleTreasure>();
}
}
else if (OVRInput.GetUp(OVRInput.RawButton.LIndexTrigger) || OVRInput.GetUp(OVRInput.RawButton.X) || OVRInput.GetUp(OVRInput.RawButton.Y) || OVRInput.GetUp(OVRInput.RawButton.LThumbstick))
{
// if position is at belt
// CenterEyeAnchor VR camera in unity
var relDiffPos = Camera.main.transform.InverseTransformPoint(thingIGrabbed.gameObject.transform.position);
//text.text = "coin let go";
if (relDiffPos.y < -0.2 && relDiffPos.y > -1.5 && relDiffPos.x < 1 && relDiffPos.x > -1 && relDiffPos.z < 1 && relDiffPos.z > -1)
{
//debugText.text = thingIGrabbed.Name;
updateScore(thingIGrabbed);
if (thingIGrabbed.isCursed)
{
GameObject ground = GameObject.Find("/Static/Ground");
light.intensity = 8f;
light.color = Color.red;
Camera.main.backgroundColor = Color.black;
thingIGrabbed.gameObject.GetComponent<AudioSource>().Play();
Destroy(thingIGrabbed.gameObject, thingIGrabbed.gameObject.GetComponent<AudioSource>().clip.length);
thingIGrabbed = null;
lost = true;
Destroy(ground);
}
else
{
Destroy(thingIGrabbed.gameObject);
thingIGrabbed = null;
}
}
/*
var relDiffPos = Camera.main.transform.position;
var objectPosition = leftPointerObject.gameObject.transform.position;
var d = 0.2;
var waist = (Camera.main.transform.position.x, Camera.main.transform.position.y-d, Camera.main.transform.position.z);
// d is y coordinate of object position; waist area (x, y-d, z)
if () {
}
*/
else
{
letGo();
}
}
if (win)
{
testText.text = "Winner";
}
}
previousPointerPos = leftPointerObject.gameObject.transform.position;
}
else
{
testText.text = "Loser";
}
/*
OLD CODE
*/
} //end of update
public int D(Vector3 A,Vector3 B,Vector3 C){ //right < 0 = true, left > 0 = false
return (A.x - B.x)(C.z - B.z) - (A.z - B.z)(C.x - B.x);
}
public int angleBetweenVectors(Vector2 A, Vector2 B){
return arccos(dot(normalize(A),normalize(B)));
}
/*public int directionUserRotated(int d){ //right < 0 = true, left > 0 = false
return d<0?1:-1;
}*/
//d(Vector3/Vector2 A,Vector3/Vector2 B,Vector3/Vector2 C)=(A.x−B.x)(C.y−B.y)−(A.y−B.y)(C.x−B.x)
//d(Camera.position+prevForwardVector, Camera.position, Camera.position + Camera.forward)
/*
OLD CODE
*/
void letGo()
{
if (thingIGrabbed)
{
Collider[] overlappingThingsWithLeftHand = Physics.OverlapSphere(leftPointerObject.transform.position, 0.01f, collectiblesMask);
detachGameObject(thingIGrabbed.gameObject, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld);
simulatePhysics(thingIGrabbed.gameObject, (leftPointerObject.gameObject.transform.position - previousPointerPos) / Time.deltaTime, true);
thingIGrabbed = null;
}
}
public static void detachGameObject(GameObject GOToDetach, AttachmentRule locationRule, AttachmentRule rotationRule, AttachmentRule scaleRule)
{
//making the parent null sets its parent to the world origin (meaning relative & global transforms become the same)
GOToDetach.transform.parent = null;
handleAttachmentRules(GOToDetach, locationRule, rotationRule, scaleRule);
}
public static void handleAttachmentRules(GameObject GOToHandle, AttachmentRule locationRule, AttachmentRule rotationRule, AttachmentRule scaleRule)
{
GOToHandle.transform.localPosition =
(locationRule == AttachmentRule.KeepRelative) ? GOToHandle.transform.position :
//technically don't need to change anything but I wanted to compress into ternary
(locationRule == AttachmentRule.KeepWorld) ? GOToHandle.transform.localPosition :
new Vector3(0, 0, 0);
//localRotation in Unity is actually a Quaternion, so we need to specifically ask for Euler angles
GOToHandle.transform.localEulerAngles =
(rotationRule == AttachmentRule.KeepRelative) ? GOToHandle.transform.eulerAngles :
//technically don't need to change anything but I wanted to compress into ternary
(rotationRule == AttachmentRule.KeepWorld) ? GOToHandle.transform.localEulerAngles :
new Vector3(0, 0, 0);
GOToHandle.transform.localScale =
(scaleRule == AttachmentRule.KeepRelative) ? GOToHandle.transform.lossyScale :
//technically don't need to change anything but I wanted to compress into ternary
(scaleRule == AttachmentRule.KeepWorld) ? GOToHandle.transform.localScale :
new Vector3(1, 1, 1);
}
public void simulatePhysics(GameObject target, Vector3 oldParentVelocity, bool simulate)
{
Rigidbody rb = target.GetComponent<Rigidbody>();
if (rb)
{
if (!simulate)
{
Object.Destroy(rb);
}
}
else
{
if (simulate)
{
//there's actually a problem here relative to the UE4 version since Unity doesn't have this simple "simulate physics" option
//The object will NOT preserve momentum when you throw it like in UE4.
//need to set its velocity itself.... even if you switch the kinematic/gravity settings around instead of deleting/adding rb
Rigidbody newRB = target.AddComponent<Rigidbody>();
newRB.velocity = oldParentVelocity;
}
}
}
void forceGrab(bool pressedA)
{
RaycastHit outHit;
//notice I'm using the layer mask again
if (Physics.Raycast(leftPointerObject.transform.position, leftPointerObject.transform.forward, out outHit, 100.0f, collectiblesMask))
{
AttachmentRule howToAttach = pressedA ? AttachmentRule.KeepWorld : AttachmentRule.SnapToTarget;
attachGameObjectToAChildGameObject(outHit.collider.gameObject, leftPointerObject.gameObject, howToAttach, howToAttach, AttachmentRule.KeepWorld, true);
thingIGrabbed = outHit.collider.gameObject.GetComponent<CollectibleTreasure>();
}
}
public void updateScore(CollectibleTreasure treasure)
{
//debugText.text = treasure.GetComponent<CollectibleTreasure>().Name;
totalPoints += treasure.Value;
totalItems++;
//Debug.DrawRay(cameraTransform.position, cameraTransform.forward * 100.0f, Color.yellow);
//print(treasure.Name);
/*if (!strDict.ContainsKey(treasure.Name))
{
strDict.Add(treasure.Name, 1);
}
else {
strDict[treasure.Name] = strDict[treasure.Name] + 1;
}*/
//testText.text = "Updating Score";
string name = treasure.Name;
//CollectibleTreasure tres2 = Resources.Load(name) as CollectibleTreasure;
var tres2 = Resources.Load<CollectibleTreasure>(name);
//debugText.text = tres2.Name + tres2.Value;
//if (tres2.isCursed){
// scream.Play();
//}
if (!collDict.ContainsKey(tres2))
{
collDict.Add(tres2, 1);
}
else
{
collDict[tres2] = collDict[tres2] + 1;
}
string message = "Item | Count | Value\n";
foreach (CollectibleTreasure key in collDict.Keys)
{
message += key.Name + " | " + collDict[key] + " | " + key.Value + "\n";
}
message += "You have " + totalItems + " items worth " + totalPoints + " points! -Evan & Lily";
testText.text = message;
if (totalItems >= 5)
{
win = true;
}
//text.text = "You have " + totalItems + " items worth " + totalPoints + " points! -Evan & Lily";
}
public void attachGameObjectToAChildGameObject(GameObject GOToAttach, GameObject newParent, AttachmentRule locationRule, AttachmentRule rotationRule, AttachmentRule scaleRule, bool weld)
{
GOToAttach.transform.parent = newParent.transform;
handleAttachmentRules(GOToAttach, locationRule, rotationRule, scaleRule);
if (weld)
{
simulatePhysics(GOToAttach, Vector3.zero, false);
}
}
CollectibleTreasure getClosestHitObject(Collider[] hits)
{
float closestDistance = 10000.0f;
CollectibleTreasure closestObjectSoFar = null;
foreach (Collider hit in hits)
{
CollectibleTreasure c = hit.gameObject.GetComponent<CollectibleTreasure>();
if (c)
{
float distanceBetweenHandAndObject = (c.gameObject.transform.position - leftPointerObject.gameObject.transform.position).magnitude;
if (distanceBetweenHandAndObject < closestDistance)
{
closestDistance = distanceBetweenHandAndObject;
closestObjectSoFar = c;
}
}
}
return closestObjectSoFar;
}
/*
OLD CODE
*/
}