slimecing

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

TimerOrbitalLogic.cs (1979B)


      1 using UnityEngine;
      2 using Random = UnityEngine.Random;
      3 
      4 namespace Slimecing.Swords.Orbitals.OrbitalLogicScripts
      5 {
      6     [CreateAssetMenu(fileName = "TimerOrbitalLogic", menuName = "Swords/Orbitals/OrbitalLogic/TimerOrbitalLogic")]
      7     public class TimerOrbitalLogic : OrbitalLogic, IOrbitalTickEveryFrame
      8     {
      9         [SerializeField] private bool randomProgress;
     10         [SerializeField] private float startOrbitalProgress;
     11         
     12         public override void Initialize(Orbital orbital)
     13         {
     14             orbital.orbitalProgress = randomProgress ? Random.Range(0.0f, 1.0f) : startOrbitalProgress;
     15             SetOrbitalPos(orbital);
     16         }
     17         public override void Tick(Orbital orbital) { }
     18 
     19         private void SetOrbitalPos(Orbital orbital)
     20         {
     21             Vector2 orbitPos = orbital.orbitPath.EvaluateEllipse(orbital.orbitalProgress, orbital.radiusX, orbital.radiusY);
     22             Vector3 pos = new Vector3(orbitPos.x, orbital.verticalOffset, orbitPos.y);
     23             orbital.orbitalObject.transform.position = pos + orbital.ownerObject.transform.position;
     24         }
     25 
     26         private static void SetLook(GameObject owner, GameObject orbital)
     27         {
     28             Vector3 position = orbital.transform.position;
     29             Vector3 ownerPos = owner.transform.position;
     30             orbital.transform.LookAt(2 * position - new Vector3(ownerPos.x, position.y, ownerPos.z));
     31         }
     32 
     33         private void Rotate(Orbital orbital)
     34         {
     35             float orbitSpeed = 1f / orbital.rotationSpeed;
     36             orbital.orbitalProgress += Time.deltaTime * orbitSpeed;
     37             orbital.orbitalProgress %= 1f;
     38             SetOrbitalPos(orbital);
     39         }
     40 
     41         public void TickUpdate(Orbital orbital)
     42         {
     43             if (Mathf.Abs(orbital.rotationSpeed) < 0.1)
     44             {
     45                 orbital.rotationSpeed = 0.1f;
     46             }
     47             Rotate(orbital);
     48             SetLook(orbital.ownerObject, orbital.orbitalObject);
     49         }
     50     }
     51 }