Skip to content

Commit

Permalink
feat: xlm docs in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoneerC committed Sep 26, 2024
1 parent 8512fc0 commit 8ed692b
Show file tree
Hide file tree
Showing 13 changed files with 612 additions and 171 deletions.
101 changes: 101 additions & 0 deletions Assets/Scripts/Camera/CameraShake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections;
using UnityEngine;

/// <summary>
/// Handles camera shake effects to simulate explosions or impacts.
/// The script shakes the camera for a specified duration after a delay
/// and plays an explosion sound effect.
/// </summary>
public class CameraShake : MonoBehaviour
{
/// <summary>
/// The main camera to apply the shake effect to.
/// </summary>
public Camera mainCamera;

/// <summary>
/// The audio source used to play sound effects.
/// </summary>
public AudioSource audioSource;

/// <summary>
/// The audio clip to play during the shake effect.
/// </summary>
public AudioClip explosionSound;

/// <summary>
/// Cached transform of the main camera for optimization.
/// </summary>
private Transform _cameraTransform;

/// <summary>
/// Original position of the camera to reset after shaking.
/// </summary>
private Vector3 _originalPosition;

/// <summary>
/// Initializes the camera shake effect by setting up the camera transform
/// and original position. Starts the shake with a delay.
/// </summary>
private void Awake()
{
// Ensure the main camera is assigned
if (mainCamera != null)
{
_cameraTransform = mainCamera.transform; // Cache the camera's transform
_originalPosition = _cameraTransform.localPosition; // Store the original position
StartDelayedShake(11f, 300f, 1f); // Start the shake effect with parameters
}
else
{
Debug.LogError("Main Camera is not assigned."); // Log an error if camera is not set
}
}

/// <summary>
/// Starts the shake effect after a specified delay.
/// </summary>
/// <param name="delay">Time in seconds to wait before starting the shake.</param>
/// <param name="duration">Total duration of the shake effect in seconds.</param>
/// <param name="magnitude">The intensity of the shake effect.</param>
private void StartDelayedShake(float delay, float duration, float magnitude)
{
StartCoroutine(DelayedShakeCoroutine(delay, duration, magnitude)); // Start the coroutine for the shake effect
}

/// <summary>
/// Coroutine to handle the shaking of the camera over time.
/// </summary>
/// <param name="delay">Time in seconds to wait before starting the shake.</param>
/// <param name="duration">Total duration of the shake effect in seconds.</param>
/// <param name="magnitude">The initial intensity of the shake effect.</param>
/// <returns>Yield instruction to control the coroutine's timing.</returns>
private IEnumerator DelayedShakeCoroutine(float delay, float duration, float magnitude)
{
yield return new WaitForSeconds(delay); // Wait for the specified delay

float elapsed = 0.0f; // Track elapsed time

audioSource.PlayOneShot(explosionSound); // Play the explosion sound

// Perform the shake effect over the specified duration
while (elapsed < duration)
{
// Calculate random offsets for shaking
var x = Random.Range(-1f, 1f) * magnitude;
var y = Random.Range(-1f, 1f) * magnitude;

// Update the camera's local position to create the shake effect
_cameraTransform.localPosition = new Vector3(x, y, _originalPosition.z);

elapsed += Time.deltaTime; // Increment elapsed time

// Gradually decrease the magnitude of shaking
magnitude = Mathf.Lerp(magnitude, 0, elapsed / duration);
yield return null; // Wait for the next frame
}

// Reset the camera's position to its original state
_cameraTransform.localPosition = _originalPosition;
}
}
96 changes: 96 additions & 0 deletions Assets/Scripts/Camera/CutsceneController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using UnityEngine;

/// <summary>
/// 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.
/// </summary>
public class CutsceneController : MonoBehaviour
{
/// <summary>
/// The camera used for the cutscene.
/// </summary>
public Camera cutsceneCamera;

/// <summary>
/// The camera used for regular gameplay.
/// </summary>
public Camera playerCamera;

/// <summary>
/// The parent GameObject that controls the spline path for the cutscene.
/// </summary>
public Transform splineParent;

/// <summary>
/// The speed at which the cutscene camera follows the spline.
/// </summary>
public float speed = 1.0f;

/// <summary>
/// Duration of the cutscene in seconds.
/// </summary>
private float duration = 6.0f;

/// <summary>
/// Time elapsed since the start of the cutscene.
/// </summary>
private float elapsedTime = 0f;

/// <summary>
/// Starting position of the cutscene camera.
/// </summary>
private Vector3 startPosition;

/// <summary>
/// Target position for the cutscene camera to move towards.
/// </summary>
private Vector3 targetPosition;

/// <summary>
/// Initializes the cutscene by setting the appropriate cameras and calculating
/// the target position along the spline.
/// </summary>
void Start()
{
// Disable the player camera and enable the cutscene camera to start the cutscene
playerCamera.gameObject.SetActive(false);
cutsceneCamera.gameObject.SetActive(true);

// Store the initial position of the cutscene camera
startPosition = cutsceneCamera.transform.position;

// Set the target position to the position of the spline parent
targetPosition = splineParent.position; // Modify this to follow a specific point on your spline if needed
}

/// <summary>
/// Updates the cutscene camera's position over time, and switches back to the
/// player camera once the cutscene duration is reached.
/// </summary>
void Update()
{
// Accumulate the time elapsed since the start of the cutscene
elapsedTime += Time.deltaTime;

// Move the cutscene camera along the spline until the duration is reached
if (elapsedTime < duration)
{
// Calculate the interpolation factor (t) based on elapsed time
float t = elapsedTime / duration;

// Move the camera towards the target position using linear interpolation
cutsceneCamera.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
}
else
{
// Once the cutscene duration has elapsed, destroy the cutscene camera
// and re-enable the player camera
Destroy(cutsceneCamera.gameObject);
playerCamera.gameObject.SetActive(true);

// Disable this script to prevent further updates
enabled = false;
}
}
}
55 changes: 0 additions & 55 deletions Assets/Scripts/CameraShake.cs

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 8ed692b

Please sign in to comment.