-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBullet.cs
More file actions
381 lines (321 loc) · 12.2 KB
/
Bullet.cs
File metadata and controls
381 lines (321 loc) · 12.2 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
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
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Bullet : MonoBehaviour
{
[HideInInspector]
public GameObject go;
[HideInInspector]
public Transform tform;
[HideInInspector]
public Rigidbody2D rb;
[SerializeField]
public float speed = 5f;
[SerializeField]
public float verticalSpeed = 0f;
[SerializeField]
public bool useVertical = false;
[SerializeField]
public float lifetime = 0f;
[SerializeField]
public PrevRotWrapper prw = new PrevRotWrapper();
[SerializeField]
public BulletAction[] actions;
[SerializeField]
public float param = 0f;
[SerializeField]
public int actionIndex = 0;
[SerializeField]
public BulletPattern master;
void Awake()
{
go = gameObject;
tform = transform;
rb = GetComponent<Rigidbody2D>();
//make sure this bullet knows whos his daddy
//actually its so 100 bullet objects dont clutter the hierarchy window
tform.parent = BulletManager.instance.transform;
}
void FixedUpdate()
{
var targetVelocity = tform.forward * speed;
// dont bother with vertical speed unless its been tampered with (see ChangeSpeedVertical..or was it SpeedChangeVertical?)
if (useVertical)
{
targetVelocity += BulletManager.instance.MainCam.up * verticalSpeed;
}
var velocityChange = (targetVelocity - (Vector3)rb.velocity);
rb.AddForce(velocityChange, ForceMode2D.Force);
//if you uncomment this stuff the bullet willl die automatically die after awhile
// look at me tempting you like this
//lifetime += Time.deltaTime;
//if(lifetime > BulletManager.instance.bulletLifespan)
// Deactivate();
}
IEnumerator RunActions()
{
for (actionIndex = 0; actionIndex < actions.Length; actionIndex++)
{
switch (actions[actionIndex].type)
{
case (BulletActionType.Wait):
float waitT;
if (actions[actionIndex].randomWait)
waitT = Random.Range(actions[actionIndex].waitTime.x, actions[actionIndex].waitTime.y);
else
waitT = actions[actionIndex].waitTime.x;
if (actions[actionIndex].rankWait)
waitT += BulletManager.instance.rank * actions[actionIndex].waitTime.z;
waitT *= BulletManager.instance.timePerFrame;
yield return new WaitForSeconds(waitT);
break;
case (BulletActionType.ChangeDirection):
if (actions[actionIndex].waitForChange)
yield return StartCoroutine(ChangeDirection(actionIndex));
else
StartCoroutine(ChangeDirection(actionIndex));
break;
case (BulletActionType.ChangeSpeed):
if (actions[actionIndex].waitForChange)
yield return StartCoroutine(ChangeSpeed(actionIndex, false));
else
StartCoroutine(ChangeSpeed(actionIndex, false));
break;
case (BulletActionType.StartRepeat):
yield return StartCoroutine(RunNestedActions());
break;
case (BulletActionType.Fire):
if (master != null)
master.Fire(tform, actions[actionIndex], param, prw);
break;
case (BulletActionType.VerticalChangeSpeed):
if (actions[actionIndex].waitForChange)
yield return StartCoroutine(ChangeSpeed(actionIndex, true));
else
StartCoroutine(ChangeSpeed(actionIndex, true));
break;
case (BulletActionType.Deactivate):
Deactivate();
break;
}
}
}
IEnumerator RunNestedActions()
{
var startIndex = actionIndex;
var endIndex = 0;
actionIndex++;
float repeatC = actions[startIndex].repeatCount.x;
if (actions[startIndex].rankRepeat)
repeatC += actions[startIndex].repeatCount.y * BulletManager.instance.rank;
repeatC = Mathf.Floor(repeatC);
for (var y = 0; y < repeatC; y++)
{
while (actions[actionIndex].type != BulletActionType.EndRepeat)
{
switch (actions[actionIndex].type)
{
case (BulletActionType.Wait):
float waitT;
if (actions[actionIndex].randomWait)
waitT = Random.Range(actions[actionIndex].waitTime.x, actions[actionIndex].waitTime.y);
else
waitT = actions[actionIndex].waitTime.x;
if (actions[actionIndex].rankWait)
waitT += BulletManager.instance.rank * actions[actionIndex].waitTime.z;
waitT *= BulletManager.instance.timePerFrame;
yield return new WaitForSeconds(waitT);
break;
case (BulletActionType.ChangeDirection):
if (actions[actionIndex].waitForChange)
yield return ChangeDirection(actionIndex);
else
StartCoroutine(ChangeDirection(actionIndex));
break;
case (BulletActionType.ChangeSpeed):
if (actions[actionIndex].waitForChange)
yield return ChangeSpeed(actionIndex, false);
else
StartCoroutine(ChangeSpeed(actionIndex, false));
break;
case (BulletActionType.EndRepeat):
yield return RunNestedActions();
break;
case (BulletActionType.Fire):
if (master != null)
master.Fire(tform, actions[actionIndex], param, prw);
break;
case (BulletActionType.VerticalChangeSpeed):
if (actions[actionIndex].waitForChange)
yield return ChangeSpeed(actionIndex, true);
else
StartCoroutine(ChangeSpeed(actionIndex, true));
break;
case (BulletActionType.Deactivate):
Deactivate();
break;
}
actionIndex++;
}
endIndex = actionIndex;
actionIndex = startIndex + 1;
}
actionIndex = endIndex;
}
//activate a bullet, wow what would you do without these comments?
public void Activate()
{
BulletManager.instance.bulletCount++;
lifetime = 0f;
verticalSpeed = 0f;
useVertical = false;
prw.prevRotationNull = true;
StartCoroutine(RunActions());
}
//we dont actually destroy bullets, were all about recycling
void Deactivate()
{
if (go.activeSelf)
{
BulletManager.instance.bulletCount--;
go.SetActive(false);
}
}
//im feeling adventurous so im going to try putting comments INSIDE the function, you ready?
IEnumerator ChangeDirection(int i)
{
var t = 0f;
//determine how long this operation will take
float d;
if (actions[i].randomWait)
d = Random.Range(actions[i].waitTime.x, actions[i].waitTime.y);
else
d = actions[i].waitTime.x;
if (actions[i].rankWait)
d += BulletManager.instance.rank * actions[i].waitTime.z;
d *= BulletManager.instance.timePerFrame;
var originalRot = tform.localRotation;
// determine offset
float ang;
if (actions[i].randomAngle)
ang = Random.Range(actions[i].angle.x, actions[i].angle.y);
else
ang = actions[i].angle.x;
if (actions[i].rankAngle)
ang += BulletManager.instance.rank * actions[i].angle.z;
Quaternion newRot = Quaternion.identity;
//and set rotation depending on angle
switch (actions[i].direction)
{
case (DirectionType.TargetPlayer):
var dotHeading = Vector3.Dot(tform.up, BulletManager.instance.player.position - tform.position);
int dir;
if (dotHeading > 0)
dir = -1;
else
dir = 1;
var angleDif = Vector3.Angle(tform.forward, BulletManager.instance.player.position - tform.position);
newRot = originalRot * Quaternion.AngleAxis((dir * angleDif) - ang, Vector3.right);
break;
case (DirectionType.Absolute):
newRot = Quaternion.Euler(-(ang - 270), 270, 0);
break;
case (DirectionType.Relative):
newRot = originalRot * Quaternion.AngleAxis(-ang, Vector3.right);
break;
}
//Sequence has its own thing going on, continually turning a set amount until time is up
if (actions[i].direction == DirectionType.Sequence)
{
newRot = Quaternion.AngleAxis(-ang, Vector3.right);
while (t < d)
{
tform.localRotation *= newRot;
t += Time.deltaTime;
yield return new WaitForFixedUpdate();
}
}
//all the others just linearly progress to destination rotation
else if (d > 0)
{
while (t < d)
{
tform.localRotation = Quaternion.Slerp(originalRot, newRot, t / d);
t += Time.deltaTime;
yield return new WaitForFixedUpdate();
}
tform.localRotation = newRot;
}
}
//its basically the same as the above except without rotations
IEnumerator ChangeSpeed(int i, bool isVertical)
{
var t = 0f;
var s = 0f;
if (isVertical)
useVertical = true;
float d;
if (actions[i].randomWait)
d = Random.Range(actions[i].waitTime.x, actions[i].waitTime.y);
else
d = actions[i].waitTime.x;
if (actions[i].rankWait)
d += BulletManager.instance.rank * actions[i].waitTime.z;
d *= BulletManager.instance.timePerFrame;
var originalSpeed = speed;
float newSpeed;
if (actions[i].randomSpeed)
newSpeed = Random.Range(actions[i].speed.x, actions[i].speed.y);
else
newSpeed = actions[i].speed.x;
if (actions[i].rankSpeed)
d += BulletManager.instance.rank * actions[i].speed.z;
if (d > 0)
{
while (t < d)
{
s = Mathf.Lerp(originalSpeed, newSpeed, t / d);
if (isVertical) verticalSpeed = s;
else speed = s;
t += Time.deltaTime;
yield return new WaitForFixedUpdate();
}
}
if (isVertical) verticalSpeed = newSpeed;
else speed = newSpeed;
}
//if a bullet leaves the boundary then make sure it actually left the screen, not enetered it
// if it did, then deactivate
// this process MAY be too intensive for iPhone, but then it does keep bullet number under control
void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Boundary"))
{
if (CamColliders.instance.isOutSideBox(tform.position))
{
Deactivate();
}
}
}
}
//exclusive bullet action variables
[System.Serializable]
public class BulletAction : BPAction
{
[SerializeField]
public BulletActionType type = BulletActionType.Wait;
[SerializeField]
public bool waitForChange = false;
}
[System.Serializable]
public enum BulletActionType
{
Wait,
ChangeDirection,
ChangeSpeed,
StartRepeat,
EndRepeat,
Fire,
VerticalChangeSpeed,
Deactivate
};