InputOrbitalLogic.cs (3912B)
1 using Slimecing.Triggers; 2 using UnityEngine; 3 using UnityEngine.InputSystem; 4 5 namespace Slimecing.Swords.Orbitals.OrbitalLogicScripts 6 { 7 [CreateAssetMenu(fileName = "InputOrbitalLogic", menuName = "Swords/Orbitals/OrbitalLogic/InputOrbitalLogic")] 8 public class InputOrbitalLogic : OrbitalLogic, IOrbitalTickEveryFrame, IControllableOrbital 9 { 10 [SerializeField] private Trigger orbitalInputTrigger; 11 12 private Trigger runtimeOrbitalInputTrigger { get; set; } 13 14 public override OrbitalLogic GetOrbital() 15 { 16 return Instantiate(this); 17 } 18 19 public override void Initialize(Orbital orbital) 20 { 21 runtimeOrbitalInputTrigger = orbitalInputTrigger.GetTrigger(); 22 GetInput(orbital, Vector2.up); 23 runtimeOrbitalInputTrigger.EnableTrigger(orbital.ownerControlObject); 24 } 25 26 public override void Tick(Orbital orbital) 27 { 28 Vector3 center = orbital.ownerObject.transform.position; 29 30 float step = Time.fixedDeltaTime * orbital.rotationSpeed; 31 Vector2 circlePos = orbital.orbitPath.EvaluateEllipse(orbital.orbitalProgress, orbital.radiusX, orbital.radiusY); 32 Vector3 targetPos = new Vector3(circlePos.x, orbital.verticalOffset, circlePos.y); 33 Vector3 smoothPos = Vector3.RotateTowards(orbital.orbitalObject.transform.position - center, 34 targetPos,step, 0f); 35 Vector3 orbitPos = new Vector3(smoothPos.normalized.x * orbital.radiusX, 36 orbital.verticalOffset, smoothPos.normalized.z * orbital.radiusY); 37 if (orbitPos != orbital.orbitalObject.transform.position) 38 { 39 orbital.orbitalObject.transform.position = orbitPos + center; 40 } 41 42 SetLook(orbital.ownerObject, orbital.orbitalObject); 43 } 44 45 private static void SetLook(GameObject owner, GameObject orbital) 46 { 47 Vector3 position = orbital.transform.position; 48 Vector3 ownerPos = owner.transform.position; 49 orbital.transform.LookAt(2 * position - new Vector3(ownerPos.x, position.y, ownerPos.z)); 50 } 51 52 private void GetInput(Orbital orbital, Vector2 inputDir) 53 { 54 if (inputDir.Equals(Vector2.zero)) return; 55 if (!(Mathf.Abs(inputDir.x) > 0.05f) && !(Mathf.Abs(inputDir.y) > 0.05f)) return; 56 Vector3 center = orbital.ownerObject.transform.position; 57 float angle = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg; 58 orbital.orbitalProgress = angle / 360f; 59 } 60 61 public void TickUpdate(Orbital orbital) 62 { 63 var action = runtimeOrbitalInputTrigger.ReadCurrentValue<InputAction>(); 64 if (action == null) return; 65 if (!action.triggered) return; 66 if (action.activeControl != null && action.activeControl?.device.name == "Mouse") 67 { 68 if (UnityEngine.Camera.main != null) 69 { 70 Plane playerPlane = new Plane(Vector3.up, orbital.ownerObject.transform.position); 71 Vector2 readInput = action.ReadValue<Vector2>(); 72 Ray ray = UnityEngine.Camera.main.ScreenPointToRay(readInput); 73 if (!playerPlane.Raycast(ray, out var distance)) return; 74 Vector3 targetPoint = ray.GetPoint(distance) - orbital.ownerObject.transform.position; 75 GetInput(orbital, new Vector2(targetPoint.x, targetPoint.z)); 76 return; 77 } 78 } 79 GetInput(orbital, action.ReadValue<Vector2>()); 80 } 81 82 public void ChangeController(GameObject controller) 83 { 84 if (runtimeOrbitalInputTrigger == null) return; 85 runtimeOrbitalInputTrigger.EnableTrigger(controller); 86 } 87 } 88 }