Files
AppleHillsProduction/Assets/Scripts/Minigames/TrashMaze/Objects/TeleportationHelper.cs

159 lines
6.1 KiB
C#
Raw Normal View History

2025-12-19 15:26:13 +01:00
using System.Collections;
using Core;
using Minigames.TrashMaze.Core;
using Minigames.TrashMaze.Data;
using Unity.Cinemachine;
using UnityEngine;
namespace Minigames.TrashMaze.Objects
{
/// <summary>
/// Static helper class for trash maze teleportation logic.
/// Provides reusable methods for:
/// - Starting camera blend to a target state
/// - Teleporting a character midway through blend
/// - Repositioning camera to teleported character
/// - Setting up camera tracking after blend completes
/// </summary>
public static class TeleportationHelper
{
/// <summary>
/// Get the camera for the target camera state
/// </summary>
private static CinemachineCamera GetTargetCamera(TrashMazeCameraState cameraState)
{
if (TrashMazeCameraController.Instance == null)
return null;
return cameraState switch
{
TrashMazeCameraState.Gameplay => TrashMazeCameraController.Instance.GetGameplayCamera(),
TrashMazeCameraState.Maze => TrashMazeCameraController.Instance.GetMazeCamera(),
TrashMazeCameraState.PulverGameplay => TrashMazeCameraController.Instance.GetPulverGameplayCamera(),
_ => null
};
}
/// <summary>
/// Start the camera blend to the target state
/// </summary>
public static void StartCameraBlend(TrashMazeCameraState targetState)
{
if (TrashMazeCameraController.Instance != null)
{
Logging.Debug($"[TeleportationHelper] Starting camera blend to {targetState}");
switch (targetState)
{
case TrashMazeCameraState.Gameplay:
TrashMazeCameraController.Instance.SwitchToGameplay();
break;
case TrashMazeCameraState.Maze:
TrashMazeCameraController.Instance.SwitchToMaze();
break;
case TrashMazeCameraState.PulverGameplay:
TrashMazeCameraController.Instance.SwitchToPulverGameplay();
break;
}
}
else
{
Debug.LogError($"[TeleportationHelper] TrashMazeCameraController instance not found!");
}
}
/// <summary>
/// Coroutine that waits for halfway through camera blend, then teleports the character
/// and immediately sets it as the tracking target for the camera
/// </summary>
public static IEnumerator TeleportMidBlendAndSetTracking(GameObject character, Transform teleportTarget, TrashMazeCameraState targetCameraState)
{
CinemachineBrain brain = Camera.main?.GetComponent<CinemachineBrain>();
if (brain != null && brain.IsBlending)
{
// Get blend duration from brain
float blendDuration = brain.ActiveBlend != null ? brain.ActiveBlend.Duration : 1f;
float halfBlendTime = blendDuration / 2f;
Logging.Debug($"[TeleportationHelper] Waiting {halfBlendTime:F2}s (half of {blendDuration:F2}s blend) before teleport");
// Wait for halfway through the blend
yield return new WaitForSeconds(halfBlendTime);
}
else
{
// Fallback: wait a short moment if no blend is detected
yield return new WaitForSeconds(0.25f);
}
// Teleport character
if (character != null && teleportTarget != null)
{
character.transform.position = teleportTarget.position;
character.transform.rotation = teleportTarget.rotation;
Logging.Debug($"[TeleportationHelper] Teleported {character.name} to {teleportTarget.position}");
// Immediately set as tracking target - let the blend finish naturally
SetCameraTrackingTarget(character, targetCameraState);
}
else
{
if (character == null)
Debug.LogError($"[TeleportationHelper] Character GameObject is null!");
if (teleportTarget == null)
Debug.LogError($"[TeleportationHelper] Teleport target not assigned!");
}
}
/// <summary>
/// Wait for camera blend to complete
/// </summary>
public static IEnumerator WaitForCameraBlend()
{
CinemachineBrain brain = Camera.main?.GetComponent<CinemachineBrain>();
if (brain != null)
{
// Wait until blend is complete
while (brain.IsBlending)
{
yield return null;
}
Logging.Debug($"[TeleportationHelper] Camera blend completed");
}
else
{
// Fallback: wait a brief moment
yield return new WaitForSeconds(0.5f);
}
}
/// <summary>
/// Set a character as the tracking target for the target camera
/// </summary>
public static void SetCameraTrackingTarget(GameObject character, TrashMazeCameraState targetCameraState)
{
if (character == null)
{
Debug.LogError($"[TeleportationHelper] Cannot set tracking target - character is null");
return;
}
var targetCamera = GetTargetCamera(targetCameraState);
if (targetCamera != null)
{
targetCamera.Follow = character.transform;
targetCamera.LookAt = character.transform;
Logging.Debug($"[TeleportationHelper] Set {character.name} as tracking target for {targetCameraState} camera");
}
else
{
Debug.LogError($"[TeleportationHelper] Target camera for state {targetCameraState} not found!");
}
}
}
}