diff options
Diffstat (limited to 'Thronefall_v1.0/Decompile/CameraRig.cs')
-rw-r--r-- | Thronefall_v1.0/Decompile/CameraRig.cs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/Thronefall_v1.0/Decompile/CameraRig.cs b/Thronefall_v1.0/Decompile/CameraRig.cs new file mode 100644 index 0000000..7a87897 --- /dev/null +++ b/Thronefall_v1.0/Decompile/CameraRig.cs @@ -0,0 +1,120 @@ +using System.Collections; +using UnityEngine; + +public class CameraRig : MonoBehaviour +{ + [Header("Forced Camera")] + [HideInInspector] + public Transform overrideCameraTarget; + + private Transform cameraTarget; + + private Transform currentTarget; + + private Quaternion startRotation; + + [SerializeField] + private float transitionSpeed = 1f; + + [Header("Camera Boundaries")] + [SerializeField] + private CameraBounds camBounds; + + [SerializeField] + private bool addCamBoundMode; + + [SerializeField] + private KeyCode debugAddCamBound; + + [SerializeField] + private KeyCode enableDisableCamBound; + + [SerializeField] + private bool boundsEnabled = true; + + [SerializeField] + private float outerBoundWidth = 12f; + + private Vector3 targetPosition; + + private bool transitionRunning; + + private void Start() + { + startRotation = base.transform.rotation; + cameraTarget = base.transform.parent; + base.transform.SetParent(null); + } + + private void Update() + { + targetPosition = base.transform.position; + if (overrideCameraTarget != null && currentTarget != overrideCameraTarget) + { + StartCoroutine(TransitionToTarget(overrideCameraTarget)); + } + else if (overrideCameraTarget == null && currentTarget != cameraTarget) + { + StartCoroutine(TransitionToTarget(cameraTarget)); + } + else if (!transitionRunning) + { + if (overrideCameraTarget != null) + { + base.transform.position = overrideCameraTarget.position; + base.transform.rotation = overrideCameraTarget.rotation; + } + else + { + base.transform.position = cameraTarget.position; + base.transform.rotation = startRotation; + } + } + } + + private void HandleBounds() + { + if (!camBounds) + { + base.transform.position = targetPosition; + return; + } + if (!boundsEnabled) + { + base.transform.position = targetPosition; + return; + } + if (camBounds.IsInBounds(new Vector2(targetPosition.x, targetPosition.z))) + { + base.transform.position = targetPosition; + return; + } + Vector2 vector = camBounds.ClosestPointOnBounds(new Vector2(targetPosition.x, targetPosition.z)); + float magnitude = (vector - new Vector2(targetPosition.x, targetPosition.z)).magnitude; + Vector3 vector2 = new Vector3(vector.x, targetPosition.y, vector.y); + float num = Mathf.Clamp01(magnitude / outerBoundWidth); + num = 0f - Mathf.Pow(num, 2f) + 2f * num; + Vector2 normalized = (new Vector2(targetPosition.x, targetPosition.z) - vector).normalized; + Vector3 b = vector2 + new Vector3(normalized.x, 0f, normalized.y) * outerBoundWidth * 0.5f; + base.transform.position = Vector3.Lerp(vector2, b, num); + } + + private IEnumerator TransitionToTarget(Transform newTarget) + { + transitionRunning = true; + Vector3 startPosition = base.transform.position; + Quaternion startRotation = base.transform.rotation; + float transitionTime2 = 0f; + while (targetPosition != newTarget.position || base.transform.rotation != newTarget.rotation) + { + transitionTime2 = Mathf.Clamp(transitionTime2, 0f, 1f); + float t = 3f * Mathf.Pow(transitionTime2, 2f) - 2f * Mathf.Pow(transitionTime2, 3f); + base.transform.position = Vector3.Lerp(startPosition, newTarget.position, t); + base.transform.rotation = Quaternion.Lerp(startRotation, newTarget.rotation, t); + transitionTime2 += Time.deltaTime * transitionSpeed; + yield return null; + } + currentTarget = newTarget; + transitionRunning = false; + } +} |