-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedCubeIntercept.cs
361 lines (311 loc) · 9.92 KB
/
RedCubeIntercept.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
// Copyright 2016 Raymond Neilson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using System.Collections;
public class RedCubeIntercept : MonoBehaviour {
private GameObject target;
private Scorer scorer;
private float deltaTime;
private DeathType dying = DeathType.None;
private Vector3 bearing = Vector3.zero;
private Vector3 closing = Vector3.zero;
private Vector3 prevPos = Vector3.zero;
private Vector3 currPos = Vector3.zero;
private float currSpeed = 0.0f;
private float avgSpeed = 0.0f;
private RedCubeGroundControl control;
private bool debugInfo;
private float avgWeight = 0.82f;
private float currWeight = 0.18f;
private const bool spin = true;
private Vector3 spinRef = Vector3.forward;
public Vector3 spinAxis = Vector3.forward;
public float torque = 0.015f;
// Unity 5 API changes
//private AudioSource myAudioSource;
private Rigidbody myRigidbody;
// Type/instance management stuff
public string thisTypeName = "Interceptor";
private EnemyType thisType;
private EnemyInst thisInst;
// Public parameters
public float speed;
public float drag;
public GameObject burster;
public GameObject bursterQuiet;
public GameObject deathFade;
public float shrapnelForce = 50f;
public float shrapnelLifetime = 0.5f;
public GameObject shrapnelSparker;
public bool scatterChildren = true;
// Bomb parameters
public float bombHeight = 0.51f;
public float bombForce = 50f;
public float bombPushRadius = 0.5f;
public GameObject bombEffect;
private bool armed;
// Prefab detach & delay-kill
//public int numChildren;
//public GameObject[] allChildren;
// Use this for initialization
void Start () {
// Unity 5 API changes
//myAudioSource = GetComponent<AudioSource>();
myRigidbody = GetComponent<Rigidbody>();
myRigidbody.drag = drag;
if (!scorer) {
FindControl(GameObject.FindGameObjectWithTag("GameController"));
if (scorer.GlobalDebug) {
Debug.Log("Scorer not passed on spawn!", gameObject);
}
}
debugInfo = control.DebugInfo;
currPos = transform.position;
// Add to control's list
thisType = EnemyList.AddOrGetType(thisTypeName);
thisInst = new EnemyInst(thisType.typeNum, gameObject);
control.AddInstanceToList(thisInst);
/*
// Some debug
if (transform.childCount > 0) {
numChildren = transform.childCount;
allChildren = new GameObject[numChildren];
for (int i = 0; i < numChildren; i++) {
allChildren[i] = transform.GetChild(i).gameObject;
}
}
*/
armed = true;
}
// Update is called once per frame
void Update () {
if (dying != DeathType.None) {
BlowUp();
}
else if ((debugInfo) || (scorer.GlobalDebug)) {
Debug.DrawLine(transform.position, closing + transform.position, Color.magenta);
Debug.DrawLine(closing + transform.position, bearing + transform.position, Color.blue);
}
}
//Put movement in FixedUpdate
void FixedUpdate () {
// Update dT, position, and velocity info
deltaTime = Time.fixedDeltaTime;
prevPos = currPos;
currPos = transform.position;
Vector3 currVel = (currPos - prevPos) / deltaTime;
currSpeed = currVel.magnitude;
// Average speed
avgSpeed = avgSpeed * avgWeight + currSpeed * currWeight;
if (target) {
UpdateTracking();
//bearing = target.transform.position - transform.position;
myRigidbody.AddForce(bearing.normalized * speed);
// Spin menacingly (if spinning enabled)
}
else {
// Try and acquire new target
NewTarget(scorer.Player);
}
if (spin) {
if (target) {
myRigidbody.AddTorque(SpinVector(bearing) * torque);
}
else {
myRigidbody.AddTorque(spinAxis * torque);
}
}
}
void BlowUp () {
if (dying == DeathType.Loudly) {
Destroy(Instantiate(burster, transform.position, Quaternion.Euler(-90, 0, 0)), 0.5f);
}
else {
Destroy(Instantiate(bursterQuiet, transform.position, Quaternion.Euler(-90, 0, 0)), 0.5f);
}
if (deathFade) {
Destroy(Instantiate(deathFade, transform.position, Quaternion.identity), 1.0f);
}
if (dying != DeathType.Silently) {
scorer.AddKill();
}
KillRelatives(1.0f);
// Drop da bomb
if (armed) {
//Vector3 deathPos = new Vector3(transform.position.x, bombHeight, transform.position.z);
DropBomb(transform.position);
}
// Remove from control's list
control.RemoveInstanceFromList(thisInst);
// Destroy ourselves
Destroy(gameObject);
}
void Clear () {
dying = DeathType.Silently;
BlowUp();
}
void Die (bool loudly) {
if (dying == DeathType.None) {
dying = (loudly) ? DeathType.Loudly : DeathType.Quietly;
}
}
void KillRelatives (float delay) {
// Detach from parent and/or children and destroy them after a delay
GameObject tmp;
int numChildren = transform.childCount;
// Parent first
if (transform.parent) {
tmp = transform.parent.gameObject;
Destroy(tmp, delay);
}
// Next the kids, if any
for (int i = transform.childCount - 1; i >= 0 ; i--) {
tmp = transform.GetChild(i).gameObject;
tmp.transform.parent = null;
// Add rigidbody and setup
var rb = tmp.AddComponent<Rigidbody>();
if (scatterChildren) {
// Get relative velocity
Vector3 relativeVel = myRigidbody.GetRelativePointVelocity(tmp.transform.position - transform.position);
// Pick a slight offset for death force position
float off = 0.02f;
Vector3 deathPos = transform.position + new Vector3(Random.Range(-off, off), -2.0f * off, Random.Range(-off, off));
// Setup child rigidbody and apply scatter force
SetupChildRigid(myRigidbody, rb, numChildren + 1, relativeVel);
rb.AddExplosionForce(shrapnelForce, deathPos, 0f);
}
else {
// Just give the child a rigidbody and zero velocity
SetupChildRigid(myRigidbody, rb, numChildren + 1, Vector3.zero);
}
// Enable renderer
var rend = tmp.GetComponent<Renderer>();
rend.enabled = true;
// "...but then again, who does?"
tmp.GetComponent<DelayedDeath>().DieInTime(Random.Range(0.75f, 1.25f) * shrapnelLifetime, shrapnelSparker);
}
}
void SetupChildRigid (Rigidbody source, Rigidbody dest, int portion, Vector3 newVelocity) {
dest.mass = source.mass / (float) portion;
dest.useGravity = false;
dest.velocity = newVelocity;
}
void ClearTarget () {
target = null;
}
void NewTarget (GameObject newTarget) {
target = newTarget;
}
void FindControl (GameObject controller) {
scorer = controller.GetComponent<Scorer>();
control = controller.GetComponent<RedCubeGroundControl>();
NewTarget(scorer.Player);
}
void UpdateTracking() {
Vector3 bearingOption = Vector3.zero;
Vector3 closingOption = Vector3.zero;
float closingDiff = 0.0f;
float diffOption = 0.0f;
float avgDeltaTime = control.AvgDeltaTime; // Public for debug
float frameSpeed = avgSpeed * avgDeltaTime;
// Next we'll default to heading straight for the target's position
bearing = control.Prediction(0) - transform.position;
closing = Vector3.zero;
closingDiff = bearing.magnitude;
// Then, we evaluate all predicted target positions and pick the one that gets us closest
for (int i = 1; i < control.PredictionLength; i++) {
// Exclude positions out of bounds
if (InBounds(control.Prediction(i))) {
// Find vector to target position
bearingOption = control.Prediction(i) - transform.position;
// Find how far we'll get towards that in the given time (well, number of (fixed) frames)
closingOption = (bearingOption.normalized * frameSpeed * (float) i);
// Check the distance between them
diffOption = (bearingOption - closingOption).magnitude;
// Now compare with current closest option
if (diffOption < closingDiff) {
bearing = bearingOption;
closing = closingOption;
closingDiff = diffOption;
}
}
}
}
bool InBounds(Vector3 pos) {
if (pos.x > scorer.MaxDisplacement) {
return false;
}
else if (pos.x < -scorer.MaxDisplacement) {
return false;
}
else if (pos.z > scorer.MaxDisplacement) {
return false;
}
else if (pos.z < -scorer.MaxDisplacement) {
return false;
}
else {
return true;
}
}
Vector3 SpinVector (Vector3 bearingVec) {
if (spinAxis == Vector3.up) {
return spinAxis;
}
else {
Quaternion rot = Quaternion.FromToRotation(spinRef, bearingVec.normalized);
return rot * spinAxis;
}
}
void DropBomb (Vector3 pos) {
GameObject daBomb;
Vector3 bombPos = new Vector3 (pos.x, bombHeight, pos.z);
// Spawn effect
// At player position because it looks better
daBomb = Instantiate(bombEffect, pos, Quaternion.Euler(-90, 0, 0)) as GameObject;
Destroy(daBomb, 1.0f);
// Turn down flash if dying quietly
if (dying != DeathType.Loudly) {
var lp = daBomb.GetComponent<LightPulse>();
if (lp) {
lp.ChangeTargetRelative(-1.2f);
}
}
// Turn down volume if dying quietly
if (dying == DeathType.Quietly) {
var audio = daBomb.GetComponent<AudioSource>();
if (audio) {
audio.volume *= 0.1f;
}
}
// Mute if dying silently
if (dying == DeathType.Silently) {
var audio = daBomb.GetComponent<AudioSource>();
if (audio) {
audio.volume *= 0.0f;
}
}
// We're dropping, make sure we're now disarmed
armed = false;
// Only push things if we're dying loudly (do push player)
if (dying == DeathType.Loudly) {
int pushmask = (1 << LayerMask.NameToLayer("Enemy")) | (1 << LayerMask.NameToLayer("Player"));
// Push things in outer radius
Collider[] things = Physics.OverlapSphere(pos, bombPushRadius, pushmask);
for (int i=0; i<things.Length; i++) {
things[i].GetComponent<Rigidbody>().AddExplosionForce(bombForce, bombPos, 0f);
}
}
}
}