slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

Orbital.cs (5915B)


      1 using System;
      2 using System.Collections.Generic;
      3 using Slimecing.Dependency;
      4 using Slimecing.Swords.DropBehaviour;
      5 using UnityEngine;
      6 
      7 namespace Slimecing.Swords.Orbitals
      8 {
      9     [Serializable]
     10     public class Orbital
     11     {
     12         
     13         [SerializeField] private GameObject controlOwner;
     14         [SerializeField] private GameObject orbital;
     15         [SerializeField] private OrbitalLogic orbitalLogic;
     16         [SerializeField] private float orbitalSpeed;
     17         [SerializeField] private float yOffset;
     18         [SerializeField] private float xAxis;
     19         [SerializeField] private float yAxis;
     20         [SerializeField] private bool sticky;
     21         [SerializeField] private bool instantiateOnSpawn = true;
     22 
     23         private List<Collider> _ignoredCollisions;
     24 
     25         private GameObject _cachedControlOwner;
     26         private GameObject _cachedOrbital;
     27         private OrbitalLogic _cachedOrbitalLogic;
     28         public Collider orbitalCollider { get; private set; }
     29         public Collider ownerCollider { get; private set; }
     30         public float orbitalProgress { get; set; }
     31         public Ellipse orbitPath { get; set; }
     32         public GameObject ownerObject { get; set; }
     33         
     34         public GameObject orbitalObject
     35         {
     36             get => orbital;
     37             set => orbital = value;
     38         }
     39 
     40         public OrbitalLogic currentOrbitalLogic
     41         {
     42             get => orbitalLogic;
     43             set => orbitalLogic = value;
     44         }
     45         
     46         public float rotationSpeed
     47         {
     48             get => orbitalSpeed;
     49             set => orbitalSpeed = value;
     50         }
     51 
     52         public float verticalOffset
     53         {
     54             get => yOffset;
     55             set => yOffset = value;
     56         }
     57 
     58         public float radiusX
     59         {
     60             get => xAxis;
     61             set => xAxis = value;
     62         }
     63 
     64         public float radiusY
     65         {
     66             get => yAxis;
     67             set => yAxis = value;
     68         }
     69 
     70         public GameObject ownerControlObject
     71         {
     72             get => controlOwner;
     73             set => controlOwner = value;
     74         }
     75         
     76         public bool orbitalSticky
     77         {
     78             get => sticky;
     79             set => sticky = value;
     80         }
     81         
     82         public bool instantiateOrbital
     83         {
     84             get => instantiateOnSpawn;
     85             set => instantiateOnSpawn = value;
     86         }
     87 
     88         public Orbital()
     89         {
     90             orbitPath = new Ellipse(radiusX, radiusY);
     91         }
     92 
     93         public Orbital(GameObject ownerObject, GameObject controlOwner, GameObject orbital, OrbitalLogic orbitalLogic, 
     94             float orbitalSpeed, float yOffset, float xAxis, float yAxis, bool sticky)
     95         {
     96             this.ownerObject = ownerObject;
     97             this.controlOwner = controlOwner;
     98             this.orbital = orbital;
     99             this.orbitalLogic = orbitalLogic;
    100             this.orbitalSpeed = orbitalSpeed;
    101             this.yOffset = yOffset;
    102             this.xAxis = xAxis;
    103             this.yAxis = yAxis;
    104             this.sticky = sticky;
    105             orbitPath = new Ellipse(radiusX, radiusY);
    106         }
    107 
    108         public void Initialize(GameObject realOwner)
    109         {
    110             orbitPath = new Ellipse(radiusX, radiusY);
    111             ownerObject = realOwner;
    112             InitializeOrbitalObject();
    113             InitializeOrbitalLogic();
    114         }
    115 
    116         private void InitializeOrbitalObject()
    117         {
    118             _cachedOrbital = orbitalObject;
    119             _cachedControlOwner = ownerControlObject;
    120             if (currentOrbitalLogic is IControllableOrbital logic) logic.ChangeController(_cachedControlOwner);
    121             SetCollider();
    122         }
    123 
    124         private void InitializeOrbitalLogic()
    125         {
    126             currentOrbitalLogic = currentOrbitalLogic.GetOrbital();
    127             _cachedOrbitalLogic = currentOrbitalLogic;
    128             currentOrbitalLogic.Initialize(this);
    129         }
    130 
    131         private void SetCollider()
    132         {
    133             orbitalCollider = orbital.gameObject.GetComponent<Collider>();
    134             ownerCollider = ownerObject.gameObject.GetComponent<Collider>();
    135             if (orbitalCollider  == null || ownerCollider == null) return;
    136             SetIgnoreCollisions(ownerCollider);
    137         }
    138 
    139         public void SetIgnoreCollisions(Collider toIgnore)
    140         {
    141             if (_ignoredCollisions == null)
    142             {
    143                 _ignoredCollisions = new List<Collider>();
    144             }
    145             Physics.IgnoreCollision(orbitalCollider, toIgnore);
    146             _ignoredCollisions.Add(toIgnore);
    147         }
    148 
    149         public void UpdateTick()
    150         {
    151             if (currentOrbitalLogic is IOrbitalTickEveryFrame inputOrbitalLogic)
    152             {
    153                 inputOrbitalLogic.TickUpdate(this);
    154             }
    155         }
    156         public void Tick()
    157         {
    158             currentOrbitalLogic.Tick(this);
    159         }
    160 
    161         public bool Validate()
    162         {
    163             bool isValid = true;
    164             if (_cachedOrbital != orbitalObject || _cachedControlOwner != ownerObject)
    165             {
    166                 DisableOrbital();
    167                 InitializeOrbitalObject();
    168                 isValid = false;
    169             }
    170 
    171             if (_cachedOrbitalLogic != currentOrbitalLogic)
    172             {
    173                 InitializeOrbitalLogic();
    174                 isValid = false;
    175             }
    176 
    177             return isValid;
    178         }
    179 
    180         public void DisableOrbital()
    181         {
    182             if (_cachedOrbital == null) return;
    183             foreach (var col in _ignoredCollisions)
    184             {
    185                 Physics.IgnoreCollision(orbitalCollider, col,false);
    186             }
    187             
    188             if (HasComponent<DropLogic>(_cachedOrbital))
    189             {
    190                 _cachedOrbital.GetComponent<DropLogic>().Drop();
    191             }
    192         }
    193         
    194         public static bool HasComponent<T>(GameObject obj) where T:Component
    195         {
    196             return obj.GetComponent<T>() != null;
    197         }
    198     }
    199 }