TakesOrbitals.cs (2703B)
1 using System.Collections.Generic; 2 using System.Linq; 3 using UnityEngine; 4 5 namespace Slimecing.Swords.Orbitals 6 { 7 public class TakesOrbitals : MonoBehaviour 8 { 9 [SerializeField] private List<Orbital> orbitals; 10 11 private void OnEnable() 12 { 13 SpawnAllOrbitals(); 14 } 15 16 private void SpawnAllOrbitals() 17 { 18 var thisTransform = transform; 19 foreach (var orbital in orbitals) 20 { 21 if (orbital.instantiateOrbital) 22 { 23 orbital.orbitalObject = Instantiate(orbital.orbitalObject, thisTransform.position, 24 Quaternion.identity); 25 } 26 27 InitializeCurrentOrbital(orbital); 28 } 29 } 30 31 private void OnDisable() 32 { 33 RemoveAllOrbitals(); 34 } 35 36 private void RemoveAllOrbitals() 37 { 38 foreach (var orbital in orbitals) 39 { 40 Destroy(orbital.orbitalObject); 41 } 42 } 43 44 private void InitializeCurrentOrbital(Orbital orbital) 45 { 46 orbital.Initialize(gameObject); 47 SetCollisions(orbital); 48 } 49 50 private void ActuallyAddOrbital(Orbital orbital) 51 { 52 InitializeCurrentOrbital(orbital); 53 orbitals.Add(orbital); 54 } 55 56 private void SetCollisions(Orbital orbital) 57 { 58 foreach (var orbitalCollider in orbitals.Select(o => 59 o.orbitalObject.GetComponent<Collider>()).Where(orbitalCollider => orbitalCollider != null)) 60 { 61 orbital.SetIgnoreCollisions(orbitalCollider); 62 } 63 } 64 65 private void RemoveOrbital(Orbital orbital) 66 { 67 orbital.DisableOrbital(); 68 orbitals.Remove(orbital); 69 } 70 71 public Orbital GetOrbital(GameObject orbital) 72 { 73 Orbital orbitalToReturn = null; 74 foreach (var o in orbitals) 75 { 76 if (o.orbitalObject.Equals(orbital)) 77 { 78 orbitalToReturn = o; 79 } 80 } 81 82 return orbitalToReturn; 83 } 84 85 public void Update() 86 { 87 foreach (var orbital in orbitals) 88 { 89 orbital.UpdateTick(); 90 } 91 } 92 93 public void FixedUpdate() 94 { 95 foreach (var orbital in orbitals) 96 { 97 orbital.Tick(); 98 } 99 100 foreach (var orbital in orbitals.Where(orbital => !orbital.Validate())) 101 { 102 SetCollisions(orbital); 103 } 104 } 105 } 106 }