-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructureController.cs
More file actions
786 lines (753 loc) · 34.1 KB
/
StructureController.cs
File metadata and controls
786 lines (753 loc) · 34.1 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class StructureController : MonoBehaviour
{
public GameObject ProcessorPrefab, BulkheadPrefab, GimbalPrefab, ThrusterPrefab, BoosterPrefab, CannonPrefab, SensorPrefab, PrinterPrefab, AsteroidPrefab;
public bool isAi = false;
const float debug_duration = .01f;
public Dictionary<string, ComponentController> components;
// public Dictionary<string, ClassController> classes;
protected Vector2 center_of_mass;
protected int child_count;
protected Transform rotator;
public Vector2 translation;
public float average_rotation;
public float rotation;
protected UnityEngine.Color debug_color;
RaycastHit hit;
public GameObject Explosion;
public bool Launched = false;
public float explosion_timer = 0, delete_timer = 0;
public float random_initial_speed = 0, random_rotation = 0;
public int Hitpoints;
public void Hit(int damage) {
Hitpoints -= damage;
if (Hitpoints < 0) Explode();
}
public void Unrotate()
{
if (rotator.localEulerAngles.z > 180 ) {
rotator?.Rotate(new Vector3(0, 0, (360 - rotator.localEulerAngles.z) / 10f));
// rotator?.Rotate(new Vector3(0, 0, 1));
}
else {
rotator?.Rotate(new Vector3(0, 0, -rotator.localEulerAngles.z / 10f));
}
print(rotator.localEulerAngles.z);
// rotator?.Rotate(new Vector3(0, 0, -rotator.localEulerAngles.z / 10f));
}
public void Start()
{
GameObject prefab;
rotator = transform.Find("Rotator");
if (isAi)
{
int random_component = (int)UnityEngine.Random.Range(0, 20);
switch (random_component)
{
case 0:
prefab = Instantiate(ProcessorPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Processor" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 1:
prefab = Instantiate(BulkheadPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Bulkhead" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 2:
prefab = Instantiate(GimbalPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Gimbal" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 3:
prefab = Instantiate(ThrusterPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Thruster" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 4:
prefab = Instantiate(BoosterPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Booster" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 5:
prefab = Instantiate(SensorPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Sensor" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 6:
prefab = Instantiate(CannonPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Cannon" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
case 7:
prefab = Instantiate(PrinterPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Printer" + this.name.Replace("Structure", "");
prefab.GetComponent<ComponentController>().Launch();
break;
default:
prefab = Instantiate(AsteroidPrefab, new Vector3(0, 0, 0), Quaternion.identity, rotator) as GameObject;
prefab.transform.localPosition = new Vector3(0, 0, 0);
prefab.transform.localRotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));
prefab.name = "Asteroid" + this.name.Replace("Structure", "");
prefab.GetComponent<AsteroidController>().seed = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
break;
}
}
// if (classes == null) {
// classes = new Dictionary<string, ClassController>();
// classes.Add("◎", new ClassController("◎")); //Booster
// classes.Add("◉", new ClassController("◉")); //Thruster
// classes.Add("◍", new ClassController("◍")); //Cannon
// classes.Add("▥", new ClassController("▥")); //Bulkhead
// classes.Add("▩", new ClassController("▩")); //Processor
// classes.Add("▣", new ClassController("▣")); //Gimbal
// classes.Add("◌", new ClassController("◌")); //Sensor
// classes.Add("▦", new ClassController("▦")); //Printer
// }
components = new Dictionary<string, ComponentController>();
foreach (var controller in GetComponentsInChildren<ComponentController>())
{
// print (controller.name + " -> " + this.name);
components.Add(controller.name, controller);
controller.structure = this;
// switch (controller) {
// case GimbalController gimbal:
// // foreach (var gimbal_controller in gimbal.transform.GetChild(0).GetComponentsInChildren<ComponentController>()) {
// // components.Add(gimbal_controller.name, gimbal_controller);
// // }
// // break;
// }
}
child_count = GetComponentsInChildren<ComponentController>().Length;
// Design();
if (this.transform.position.x == 0 && this.transform.position.y == 0) {
// float x = UnityEngine.Random.Range(-8f, 8f) * 30f, y = UnityEngine.Random.Range(-8f, 8f) * 30f;
// this.transform.position = new Vector2(x, y);
// GameObject.Find("Debris").transform.position = new Vector2(x, y);
}
}
public void Explode() {
explosion_timer++;
}
public void Move(string component, Vector2 direction)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.Translate(direction * .5f);
}
public void SetPosition(string component, Vector2 position)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.position = position;
}
public void SetSize(string component, Vector2 size)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].GetComponent<SpriteRenderer>().size = size;
}
public void UpdateCollider(string component)
{
if (components == null) return;
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
if (components[component].GetComponent<BoxCollider>() == null) return;
if (components[component].GetComponent<PrinterController>() != null) return;
foreach (var c in components.Values)
{
if (c.gameObject.GetComponent<BoxCollider>() != null) c.GetComponent<BoxCollider>().size = new Vector3(c.GetComponent<BoxCollider>().size.x, c.GetComponent<BoxCollider>().size.y, 1f);
}
components[component].GetComponent<BoxCollider>().size = new Vector3(components[component].GetComponent<BoxCollider>().size.x, components[component].GetComponent<BoxCollider>().size.y, 10f);
}
public void Upsize(string component, Vector2 direction)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.Translate(direction/2);
components[component].GetComponent<SpriteRenderer>().size += new Vector2(Mathf.Abs(direction.x), Mathf.Abs(direction.y));
components[component].GetComponent<BoxCollider>().size = components[component].GetComponent<SpriteRenderer>().size;
}
public void Downsize(string component, Vector2 direction)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.Translate(-direction/2);
components[component].GetComponent<SpriteRenderer>().size -= new Vector2(Mathf.Abs(direction.x), Mathf.Abs(direction.y));
components[component].GetComponent<BoxCollider>().size = components[component].GetComponent<SpriteRenderer>().size;
}
public void Rotate90(string component)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.Rotate(new Vector3(0,0,-90));
}
public void RotateM90(string component)
{
if (components == null) return;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return;
components[component].transform.Rotate(new Vector3(0,0,90));
}
public float GetRotation(string component) {
if (components == null) return 0;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return 0;
// print (components[component].transform.eulerAngles);
return components[component].transform.eulerAngles.y;
}
public float GetLocalRotation(string component) {
if (components == null) return 0;
// if (component[1] == ' ')
component = component.Substring(2);
if (!components.ContainsKey(component)) return 0;
// print (components[component].transform.localEulerAngles);
return components[component].GetLocalRotation();
}
public void Remove(string component)
{
if (components == null) return;
component = component.Substring(2);
if (component.Contains("_")) component = component.Split('_')[1];
Destroy(components[component].transform.gameObject);
}
public Vector2 GetSize(string component)
{
if (components == null) return Vector2.zero;
component = component.Substring(2);
if (!components.ContainsKey(component)) return Vector2.zero;
if (components[component].transform.GetComponent<SpriteRenderer>() == null) return new Vector2(14,28); // TODO calculate size of asteroid, Dict -> Array .Lengths
return components[component].transform.GetComponent<SpriteRenderer>().size;
}
public Vector3 GetLocalPosition(string component)
{
if (components == null) return Vector2.zero;
component = component.Substring(2);
if (!components.ContainsKey(component)) return Vector2.zero;
return components[component].transform.localPosition;
}
public Vector3 GetPosition(string component)
{
if (components == null) return Vector2.zero;
component = component.Substring(2);
if (!components.ContainsKey(component)) Start();
if (!components.ContainsKey(component)) return Vector2.zero;
return components[component].transform.position;
}
public Vector2 GetMinimumSize(string component)
{
if (components == null) return Vector2.zero;
component = component.Substring(2);
if (!components.ContainsKey(component)) return Vector2.zero;
return components[component].transform.GetComponent<ComponentController>().GetMinimumSize();
}
public void DisableColliders()
{
if (components == null) return;
foreach (var component in components.Values)
{
if (component.gameObject.GetComponent<BoxCollider>() != null) component.gameObject.GetComponent<BoxCollider>().enabled = false;
}
}
public void EnableColliders()
{
if (components == null) return;
foreach (var component in components.Values)
{
if (component.gameObject.GetComponent<BoxCollider>() != null) component.gameObject.GetComponent<BoxCollider>().enabled = true;
}
}
public ProcessorController GetProcessorController(string component)
{
if (components == null) return null;
component = component.Substring(2);
if (!components.ContainsKey(component)) return null;
switch (components[component]) {
case ProcessorController processor:
return processor;
default:
return null;
}
}
public BulkheadController GetBulkheadController()
{
if (components == null) return null;
foreach (var component in components.Values) {
switch (component) {
case BulkheadController bulkhead:
if (bulkhead.heap.Count() < bulkhead.capacity)
return bulkhead;
break;
}
}
return null;
}
public string[] GetProcessorControllers()
{
if (components == null) return null;
List<string> processors = new List<string>();
foreach (var component in components.Values) {
switch (component) {
case ProcessorController processor:
processors.Add(processor.name);
break;
}
}
return processors.ToArray();
}
public string[] GetControllers()
{
if (components == null) return null;
List<string> controllers = new List<string>();
foreach (var component in components.Values) {
if (component.launched) controllers.Add(component.GetIcon() + " " + component.name);
}
return controllers.ToArray();
}
public Dictionary<string, ComponentController> GetComponentControllers()
{
return components;
}
public float PopDesign()
{
if (components == null) return 0;
foreach (var component in components.Values) {
if (component.launched)
{
component.Design();
return component.GetCost();
}
}
return 0;
}
public void Design()
{
if (components == null) return;
foreach (var component in components.Values) {
component.Design();
}
translation = new Vector2(0, 0);
rotation = 0;
}
public void Launch()
{
if (components == null) return;
foreach (var component in components.Values) {
component.Launch();
}
}
// public void SetInstructions(string component, string instructions)
// {
// if (!components.ContainsKey(component)) return;
// switch (components[component]) {
// case ProcessorController processor:
// processor.SetInstructions(instructions);
// break;
// default: //Want all components to be scriptable? Adjust here.
// break;
// }
// }
public void DeleteLine(string component)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.DeleteLine();
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
public void SetOperand(string component, string op)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.SetOperand(op);
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
public TerrainType Pop()
{
foreach (var component in components.Values) {
switch (component) {
case BulkheadController bulkhead:
if (bulkhead.Count() > 0) return bulkhead.Pop();
break;
}
}
return TerrainType.Empty;
}
public void AddOperand(string component, string op)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.AddOperand(op);
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
public void ModifyConstantOperand(string component, float delta)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.ModifyConstantOperand(delta);
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
public void AddLine(string component, int direction)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.AddLine(direction);
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
public void Scroll(string component, int direction)
{
if (!components.ContainsKey(component)) return;
switch (components[component]) {
case ProcessorController processor:
processor.Scroll(direction);
break;
default: //Want all components to be scriptable? Adjust here.
break;
}
}
float gimbal_test = 0f;
float gimbal_step = 10f;
float thrust_rotation = 0f;
float active_component_count = 0f;
void FixedUpdate()
{
delete_timer -= Time.deltaTime;
if (child_count != GetComponentsInChildren<ComponentController>().Length) Start();
if (components == null) return;
active_component_count = 0;
foreach (var controller in components.Values) {
if (controller != null && controller.enabled) {
active_component_count++;
}
}
// if (!isAi)
// {
// Debug.DrawRay(rotator.position, transform.TransformDirection(Vector3.up) * 5, Color.red);
// RaycastHit hit;
// if (Physics.Raycast(rotator.position, rotator.transform.up, out hit, 5 + 5 * 2f * Time.deltaTime * (this.gameObject.layer + 1) / 2 ))
// {
// if (hit.collider.gameObject.name.Contains("Printer")) {
// print ("dock?");
// GameObject.Find("InputUseWeapon").transform.GetChild(0).GetComponent<Text>().text = "Dock";
// // bulkhead.Action(hit.collider.gameObject.transform.parent.GetComponent<AsteroidController>().Hit(hit.collider.gameObject.name));
// // bulkhead.Action(1000);
// // Destroy(this.gameObject);
// // GameObject.Find("World").GetComponent<PrefabCache>().PlayExplosion(hit.collider.gameObject.transform.position + new Vector3(0, 10, 10), hit.collider.gameObject.GetComponent<SpriteRenderer>().size.magnitude * 5f, "Torpedo");
// }
// else {
// print (hit.collider.gameObject.name);
// GameObject.Find("InputUseWeapon").transform.GetChild(0).GetComponent<Text>().text = "Fire";
// }
// }
// }
if (true) {//!isAi) {
center_of_mass = Vector2.zero;
foreach (var controller in components.Values) {
if (controller != null && controller.enabled) {
center_of_mass += new Vector2(controller.GetTransform().localPosition.x, controller.GetTransform().localPosition.y);
controller.Ping();
switch (controller) {
case ProcessorController processor:
processor.Action(components);
break;
// case CannonController cannon:
// cannon.Cooldown();
// break;
}
}
}
center_of_mass /= active_component_count;
translation = translation * .95f;
rotation = rotation * .95f;
float angle = 0;
foreach (var controller in components.Values)
{
if (controller != null && controller.enabled) switch (controller) {
case ThrusterController thruster:
angle = Vector2.SignedAngle(
thruster.GetThrustVector(),
thruster.GetPosition() - center_of_mass
) + this.rotator.localEulerAngles.z;
translation += thruster.GetThrustVector() / (active_component_count * 10f);// * Mathf.Cos(angle * Mathf.Deg2Rad);
thrust_rotation = thruster.GetThrustVector().magnitude / active_component_count * Mathf.Sin(angle * Mathf.Deg2Rad); //* -thruster.GetThrustVector().magnitude * 2; //(thruster.GetPosition().x - center_of_mass.x)
rotation -= thrust_rotation / 2f;
break;
case BoosterController booster:
angle = Vector2.SignedAngle(
booster.GetThrustVector(),
booster.GetPosition() - center_of_mass
) + this.rotator.localEulerAngles.z;
translation += booster.GetThrustVector() / (active_component_count * 10f);// * Mathf.Cos(angle * Mathf.Deg2Rad);
thrust_rotation = booster.GetThrustVector().magnitude / active_component_count * Mathf.Sin(angle * Mathf.Deg2Rad); //(booster.GetPosition().x - center_of_mass.x)
rotation -= thrust_rotation;
break;
}
}
rotator?.Rotate(new Vector3(0, 0, rotation));
// average_rotation += rotation / Time.deltaTime;
}
else {
switch (this.name) {
case "battleship":
break;
case "carrier":
break;
default:
break;
}
//set ai speed later UnityEngine.Random.Range(-8f, 8f)
// this.transform.Translate(new Vector2(0, random_initial_speed));
// random_rotation += UnityEngine.Random.Range(-.025f, .025f);
// this.transform.Rotate(new Vector3(0, 0, random_rotation));
// random_rotation = random_rotation / 2f;
}
if (active_component_count > 0) {
transform.Translate(translation);
// if (isAi) rotator.Rotate(new Vector3(0,0,-average_rotation * Time.deltaTime));
}
if (explosion_timer > 0) {
explosion_timer += Time.deltaTime;
// if (GetComponent<ParticleSystem>()) GetComponent<ParticleSystem>().Stop();
if (GetComponent<ParticleSystem>()) {
var exhaust_emission = GetComponent<ParticleSystem>().emission;
exhaust_emission.rate = Mathf.Clamp(1 - explosion_timer / GetComponent<SpriteRenderer>().size.magnitude * 1.5f, 0, 1);
}
GetComponent<SpriteRenderer>().color = new Color(1,1,1,Mathf.Clamp(1 - (explosion_timer/(GetComponent<SpriteRenderer>().size.magnitude*2f)), 0, 1));
if (UnityEngine.Random.Range(0f, GetComponent<SpriteRenderer>().size.magnitude + 1) > explosion_timer)
{
random_initial_speed += Time.deltaTime / (10 * explosion_timer);
random_rotation += UnityEngine.Random.Range(-.25f, .25f);
// print (transform.position);
// print (transform.localPosition);
GameObject.Find("World").GetComponent<PrefabCache>().PlayExplosion(
transform.position + new Vector3(
UnityEngine.Random.Range(-GetComponent<SpriteRenderer>().size.x, GetComponent<SpriteRenderer>().size.x),
UnityEngine.Random.Range(-GetComponent<SpriteRenderer>().size.x, GetComponent<SpriteRenderer>().size.x),
UnityEngine.Random.Range(-GetComponent<SpriteRenderer>().size.y * 2, GetComponent<SpriteRenderer>().size.y * 4)),
UnityEngine.Random.Range(GetComponent<SpriteRenderer>().size.magnitude, GetComponent<SpriteRenderer>().size.magnitude * 2),
"Small Ship");
}
if (explosion_timer + 1 > GetComponent<SpriteRenderer>().size.magnitude + 2) {
if (this.gameObject.transform.GetComponentsInChildren<CameraController>().Length == 1) {
this.gameObject.transform.GetComponentsInChildren<CameraController>()[0].CycleView();
this.gameObject.transform.GetComponentsInChildren<CameraController>()[0].CycleView();
}
Destroy(this.gameObject);
} else if (explosion_timer + 1 > GetComponent<SpriteRenderer>().size.magnitude + 1) {
if (GetComponent<ParticleSystem>())
GetComponent<ParticleSystem>().Stop();
Destroy(GetComponent<BoxCollider>());
}
if (delete_timer < 0) {
Destroy(this.gameObject);
}
}
}
// + new Vector3(
// UnityEngine.Random.Range(-GetComponent<SpriteRenderer>().size.x, GetComponent<SpriteRenderer>().size.x),
// UnityEngine.Random.Range(-GetComponent<SpriteRenderer>().size.y, GetComponent<SpriteRenderer>().size.y)
// )
public bool IsComponent(string component)
{
component = component.Substring(2);
return components.ContainsKey(component);
}
public string[] GetInteractiveComponents() {
List<string> objects = new List<string>();
foreach (var controller in components.Values)
{
switch (controller) {
case ProcessorController processor:
objects.Add(processor.name);
break;
case ThrusterController thruster:
objects.Add(thruster.name);
break;
case GimbalController gimbal:
objects.Add(gimbal.name);
break;
case BoosterController booster:
objects.Add(booster.name);
break;
case CannonController cannon:
objects.Add(cannon.name);
break;
case SensorController sensor:
objects.Add(sensor.name);
break;
case BulkheadController bulkhead:
objects.Add(bulkhead.name);
break;
case PrinterController printer:
objects.Add(printer.name);
break;
case AsteroidController printer:
objects.Add(printer.name);
break;
}
}
foreach (var controller in components.Values)
{
switch (controller) {
case ProcessorController processor:
objects.Add(processor.name);
break;
case ThrusterController thruster:
objects.Add(thruster.name);
break;
case GimbalController gimbal:
objects.Add(gimbal.name);
break;
case BoosterController booster:
objects.Add(booster.name);
break;
case CannonController cannon:
objects.Add(cannon.name);
break;
case SensorController sensor:
objects.Add(sensor.name);
break;
case BulkheadController bulkhead:
objects.Add(bulkhead.name);
break;
case PrinterController printer:
objects.Add(printer.name);
break;
case AsteroidController printer:
objects.Add(printer.name);
break;
}
}
return objects.ToArray();
}
public string[] GetOtherComponents(string selected)
{
if (!components.ContainsKey(selected)) return null;
string[] others = new string[components.Count - 1];
int i = 0;
foreach (var key in components.Keys) {
if (key != selected) others[i++] = key;
}
return others;
}
public string GetComponentToString(string component)
{
if (components == null) return "Nil";
component = component.Substring(2);
if (!components.ContainsKey(component)) return "Nil";
components[component].Focus();
return components[component].ToString();
}
public string ToString(string selected)
{
string output = "\n " + this.name;
foreach (var controller_name in components.Keys)
{
if (selected == controller_name) output += "<b>\n> " + controller_name + "</b>";
else output += "\n> " + controller_name;
}
return output;
}
public override string ToString()
{
// print (translation);
// print ($"♘position:[{Neaten(this.transform.localPosition.x)},{Neaten(this.transform.localPosition.y)}");
string output = $"♘position:[{Neaten(this.transform.localPosition.x)},{Neaten(this.transform.localPosition.y)},{Neaten(this.rotator.localEulerAngles.z)}]♘translation:[{Neaten(this.translation.x)},{Neaten(this.translation.y)}]";//,{Neaten(average_rotation)}]";
output += "♘classes:";
// if (classes != null) {
// foreach (ClassController c in classes.Values) {
// output += c.ToString();
// }
// }
output += "♘components:";
foreach (var component in components.Values)
{
if (component != null && component.gameObject != null) {
output += component.ComponentToString();
}
}
output += "♘entities:";
foreach (var entity in GameObject.Find("World").GetComponentsInChildren<ProjectileController>())
{
if (entity.gameObject.activeSelf) {
output += $"♘{entity.name}:[{Neaten(entity.transform.localPosition.x)},{Neaten(entity.transform.localPosition.y)},{Neaten(entity.speed)},{Neaten(entity.transform.localEulerAngles.z)}]";
}
}
return output;
}
public static string Neaten(float input) {
return input.ToString("0.00").TrimEnd('0').TrimEnd('.');
}
}
// public static class ExtensionMethods {
// public static string Neaten(float input) {
// return input.ToString("0.00").TrimEnd('0').TrimEnd('.');
// }
// public static float Remap (this float value, float from1, float to1, float from2, float to2) {
// return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
// }
// // Or IsNanOrInfinity
// public static bool HasValue(this float value)
// {
// return !float.IsNaN(value) && !float.IsInfinity(value);
// }
// public static string Repeat(this string s, int n)
// {
// return String.Concat(Enumerable.Repeat(s, n));
// }
// }