diff --git a/Assets/Scripts/Camera/CutsceneController.cs b/Assets/Scripts/Camera/CutsceneController.cs index 1f55c88..a3cbbe9 100644 --- a/Assets/Scripts/Camera/CutsceneController.cs +++ b/Assets/Scripts/Camera/CutsceneController.cs @@ -3,58 +3,137 @@ /// /// Manages the cutscene sequence by controlling the cutscene camera's movement -/// along a spline for a specified duration, after which it switches back to the -/// player camera. +/// along a spline path for a specified duration, after which it switches back +/// to the player camera and re-enables player input. /// public class CutsceneController : MonoBehaviour { - private Camera playerCamera; // Regular gameplay camera - public Transform splineParent; // The parent GameObject that controls the spline path - public float speed = 1.0f; // Speed at which the cutscene camera follows the spline + /// + /// The main player camera used during regular gameplay. + /// + private Camera _playerCamera; - private Camera cutsceneCamera; // Camera for the cutscene - private SplineContainer splineContainer; // Reference to the spline - private float duration = 8.0f; // Duration of the cutscene - private float elapsedTime = 0f; // Time elapsed since the start of the cutscene + /// + /// The parent GameObject containing the spline that defines the cutscene camera's path. + /// + public Transform splineParent; + /// + /// The speed at which the cutscene camera moves along the spline. + /// + public float speed = 1.0f; + + /// + /// The dedicated camera used during the cutscene sequence. + /// + private Camera _cutsceneCamera; + + /// + /// The SplineContainer component that holds the spline data for the cutscene path. + /// + private SplineContainer _splineContainer; + + /// + /// The total duration of the cutscene in seconds. + /// + private const float Duration = 8.0f; + + /// + /// The time elapsed since the cutscene started. + /// + private float _elapsedTime; + + /// + /// Initializes the cutscene by disabling the player camera, enabling the cutscene camera, + /// and setting up the spline container. Also validates the existence of the spline. + /// private void Start() { - playerCamera = Camera.main; - cutsceneCamera = new GameObject("Cutscene Camera").AddComponent(); - cutsceneCamera.gameObject.SetActive(true); - playerCamera.gameObject.SetActive(false); - - splineContainer = splineParent.GetComponent(); - if (splineContainer != null) return; - Debug.LogError("No SplineContainer found on the spline parent."); - return; + // Assign the main player camera (assumes it's tagged as MainCamera) + _playerCamera = Camera.main; + + // Create a new cutscene camera dynamically and activate it + _cutsceneCamera = new GameObject("Cutscene Camera").AddComponent(); + _cutsceneCamera.gameObject.SetActive(true); + + // Deactivate the player camera to focus on the cutscene + if (_playerCamera != null) + _playerCamera.gameObject.SetActive(false); + + // Attempt to retrieve the SplineContainer from the spline parent + _splineContainer = splineParent.GetComponent(); + + // If no SplineContainer is found, log an error + if (_splineContainer == null) + { + Debug.LogError("No SplineContainer found on the spline parent."); + } } + /// + /// Enables or disables the player's input by toggling the player's movement controller. + /// + /// If true, player input is disabled; if false, player input is enabled. + private static void DisablePlayerInput(bool disable) + { + // Locate the player by tag + var player = GameObject.FindWithTag("Player"); + + // If no player is found, exit the method + if (player == null) return; + + // Access the player's input or movement controller (assumed to be FirstPersonController) + var playerController = player.GetComponent(); + + // If a controller is found, enable/disable it based on the provided flag + if (playerController != null) + { + playerController.enabled = !disable; + } + } + + /// + /// Updates the cutscene camera's position and rotation along the spline path over time, + /// and switches back to the player camera once the cutscene duration has elapsed. + /// private void Update() { - elapsedTime += Time.deltaTime; + // Increment the elapsed time by the time passed since the last frame + _elapsedTime += Time.deltaTime; - if (elapsedTime < duration) + // If the cutscene is still in progress (i.e., within its duration) + if (_elapsedTime < Duration) { - // Normalize the elapsed time to a 0-1 range - float normalizedTime = elapsedTime / duration; + // Normalize the elapsed time into a value between 0 and 1 for progress along the spline + float normalizedTime = _elapsedTime / Duration; - // Get normalized progress along the spline (from 0 to 1) + // Calculate the progress along the spline based on the normalized time float splineProgress = normalizedTime; - // Move the camera along the spline based on progress - Vector3 targetPosition = splineContainer.EvaluatePosition(splineProgress); - Vector3 targetDirection = splineContainer.EvaluateTangent(splineProgress); + // Get the camera's target position and direction (tangent) on the spline + Vector3 targetPosition = _splineContainer.EvaluatePosition(splineProgress); + Vector3 targetDirection = _splineContainer.EvaluateTangent(splineProgress); + + // Update the cutscene camera's position and rotation to follow the spline path + _cutsceneCamera.transform.position = targetPosition; + _cutsceneCamera.transform.rotation = Quaternion.LookRotation(targetDirection); - // Set camera position and rotation along the spline - cutsceneCamera.transform.position = targetPosition; - cutsceneCamera.transform.rotation = Quaternion.LookRotation(targetDirection); + // Disable player input for the duration of the cutscene + DisablePlayerInput(true); } else { - // After cutscene duration, switch back to the player camera - playerCamera.gameObject.SetActive(true); - Destroy(cutsceneCamera.gameObject); + // When the cutscene finishes, reactivate the player camera + if (_playerCamera != null) + _playerCamera.gameObject.SetActive(true); + + // Destroy the cutscene camera to clean up resources + Destroy(_cutsceneCamera.gameObject); + + // Re-enable player input after the cutscene ends + DisablePlayerInput(false); + + // Disable this script, as the cutscene has ended enabled = false; } }