-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsteroid.cs
More file actions
141 lines (118 loc) · 4.26 KB
/
Copy pathAsteroid.cs
File metadata and controls
141 lines (118 loc) · 4.26 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
using UnityEngine;
using UnityEngine.Diagnostics;
using System.Collections.Generic;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(OffScreenWrapper))]
public class Asteroid : MonoBehaviour
{
[Header("Set Dynamically")]
public int size = 3;
public bool immune = false;
public int minVel = 5;
public int maxVel = 10;
public int maxAngularVel = 10;
Rigidbody rigid; // protected
OffScreenWrapper offScreenWrapper;
void Awake()
{
rigid = GetComponent<Rigidbody>();
offScreenWrapper = GetComponent<OffScreenWrapper>();
}
// Start is called before the first frame update
void Start()
{
this.transform.rotation = Random.rotation;
InitVelocity();
}
// Update is called once per frame
void Update()
{
}
public void InitVelocity()
{
Vector3 vel;
// The initial velocity depends on whether the Asteroid is currently off screen or not
if (ScreenBounds.OOB(transform.position))
{
// If the Asteroid is out of bounds, just point it toward a point near the center of the sceen
vel = ((Vector3)Random.insideUnitCircle * 4) - transform.position;
vel.Normalize();
}
else
{
// If in bounds, choose a random direction, and make sure that when you Normalize it, it doesn't
// have a length of 0 (which might happen if Random.insideUnitCircle returned [0,0,0].
do
{
vel = Random.insideUnitCircle;
vel.Normalize();
} while (Mathf.Approximately(vel.magnitude, 0f));
}
// Multiply the unit length of vel by the correct speed (randomized) for this size of Asteroid
vel = vel * Random.Range(this.minVel, this.maxVel) / (float)size;
rigid.linearVelocity = vel;
rigid.angularVelocity = Random.insideUnitSphere * this.maxAngularVel;
}
public void OnCollisionEnter(Collision coll)
{
if (immune)
{
return;
}
GameObject otherGO = coll.gameObject;
if (otherGO.tag == "Bullet")
{
AsteroidHitByBullet(otherGO);
}
else if (otherGO.tag == "Player")
{
Destroy(gameObject);
PlayerShip.S.health -= 5;
AsteraX.GetBacktraceClient()["shipHealth"] = "" + PlayerShip.S.health;
if (PlayerShip.S.health <= 90) {
#if (!UNITY_EDITOR)
CrashOnAndroid();
// this crashes the entire game
Utils.ForceCrash(ForcedCrashCategory.AccessViolation);
#endif
}
if (PlayerShip.S.health <= 0) {
Destroy(otherGO);
AsteraX.backtraceClient.Breadcrumbs.Info("Player Died!", new Dictionary<string, string>() {
{"application.version", AsteraX.backtraceClient["application.version"]},
});
}
}
else if (otherGO.tag == "Asteroid")
{
//Destroy(otherGO);
//Destroy(gameObject);
}
}
void CrashOnAndroid()
{
// https://stackoverflow.com/questions/17511070/android-force-crash-with-uncaught-exception-in-thread
var message = new AndroidJavaObject("java.lang.String", "This is a test crash, ignore.");
var exception = new AndroidJavaObject("java.lang.Exception", message);
var looperClass = new AndroidJavaClass("android.os.Looper");
var mainLooper = looperClass.CallStatic<AndroidJavaObject>("getMainLooper");
var mainThread = mainLooper.Call<AndroidJavaObject>("getThread");
var exceptionHandler = mainThread.Call<AndroidJavaObject>("getUncaughtExceptionHandler");
exceptionHandler.Call("uncaughtException", mainThread, exception);
}
void AsteroidHitByBullet(GameObject otherGO)
{
Destroy(otherGO);
Destroy(gameObject);
PlayerShip.S.bullets += 2;
AsteraX.score += 10;
try
{
throw new System.NullReferenceException("Parameter cannot be null");
}
catch (System.NullReferenceException nre)
{
AsteraX.GetBacktraceClient().Send(nre);
}
}
}