maze_switching (#82)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #82
This commit is contained in:
2025-12-15 09:20:15 +00:00
parent 34a6c367cc
commit c62f169d08
32 changed files with 2226 additions and 1155 deletions

View File

@@ -0,0 +1,318 @@
using System;
using System.Collections;
using Core;
using Input;
using Interactions;
using Minigames.TrashMaze.Core;
using Minigames.TrashMaze.Data;
using Unity.Cinemachine;
using UnityEngine;
namespace Items
{
/// <summary>
/// Saveable data for ControllerSwitchItem state
/// </summary>
[Serializable]
public class ControllerSwitchItemSaveData
{
public bool hasBeenUsed;
}
/// <summary>
/// Camera switching mode for controller switch items
/// </summary>
public enum CameraSwitchMode
{
/// <summary>
/// No camera switching - controller switch only
/// </summary>
None,
/// <summary>
/// Use a direct reference to a Cinemachine camera
/// </summary>
DirectReference,
/// <summary>
/// Use TrashMazeCameraController state manager API
/// </summary>
TrashMazeCameraState
}
/// <summary>
/// An interactable item that switches control from one character controller to another.
/// When clicked:
/// 1. The selected character moves to this item's position
/// 2. Upon arrival, the current controller is disabled
/// 3. Camera blends to the target camera (based on camera mode)
/// 4. Once the blend completes, control switches to the target controller
/// </summary>
public class ControllerSwitchItem : SaveableInteractable
{
[Header("Controller Switch Settings")]
[Tooltip("Name of the controller to switch to (must match GameObject name of the controller)")]
[SerializeField] private string targetControllerName;
[Header("Camera Settings")]
[Tooltip("How to switch the camera when changing controllers")]
[SerializeField] private CameraSwitchMode cameraSwitchMode = CameraSwitchMode.None;
[Tooltip("Direct camera reference (only used if Camera Switch Mode is DirectReference)")]
[SerializeField] private CinemachineCamera targetVirtualCamera;
[Tooltip("Target camera state (only used if Camera Switch Mode is TrashMazeCameraState)")]
[SerializeField] private TrashMazeCameraState targetCameraState;
[Header("Visual Feedback")]
[Tooltip("Visual representation to hide after use (optional)")]
[SerializeField] private GameObject visualRepresentation;
// State
private bool _hasBeenUsed;
private PlayerTouchController _currentPlayerController;
private bool _isSwitching;
public override string SaveId => $"{gameObject.scene.name}/ControllerSwitchItem/{gameObject.name}";
internal override void OnManagedAwake()
{
base.OnManagedAwake();
if (string.IsNullOrEmpty(targetControllerName))
{
Debug.LogError($"[ControllerSwitchItem] {gameObject.name} has no target controller name specified!");
}
}
internal override void OnManagedStart()
{
base.OnManagedStart();
// Apply state after restoration
if (_hasBeenUsed && isOneTime)
{
DisableVisual();
}
}
protected override bool CanBeClicked()
{
// Cannot be clicked if already used (one-time) or if currently switching
if (_isSwitching)
return false;
if (isOneTime && _hasBeenUsed)
return false;
// Check if target controller is registered
if (!InputManager.Instance.IsControllerRegistered(targetControllerName))
{
Debug.LogWarning($"[ControllerSwitchItem] Target controller '{targetControllerName}' is not registered with InputManager.");
return false;
}
return base.CanBeClicked();
}
protected override bool DoInteraction()
{
if (_isSwitching)
return false;
// By the time this is called, the interacting character has already arrived at this item
// We just need to perform the controller/camera switch
Logging.Debug("[ControllerSwitchItem] Starting controller switch sequence");
// Start the async switch sequence (camera blend + controller switch)
StartCoroutine(SwitchControllerSequence());
// Return true immediately - interaction is considered successful
// The coroutine will handle the actual switching asynchronously
return true;
}
private IEnumerator SwitchControllerSequence()
{
_isSwitching = true;
// Step 1: Get current player controller (the one we're switching FROM)
_currentPlayerController = FindFirstObjectByType<PlayerTouchController>();
if (_currentPlayerController == null)
{
Debug.LogError("[ControllerSwitchItem] Could not find PlayerTouchController in scene!");
_isSwitching = false;
yield break;
}
Logging.Debug("[ControllerSwitchItem] Character has arrived, beginning switch");
// Step 2: Disable current player controller
_currentPlayerController.enabled = false;
Logging.Debug("[ControllerSwitchItem] Disabled current player controller");
// Step 3: Blend to target camera based on mode
yield return SwitchCamera();
// Step 4: Switch to target controller
ITouchInputConsumer targetController = InputManager.Instance.GetController(targetControllerName);
if (targetController != null)
{
// Enable the target controller if it's a MonoBehaviour
if (targetController is MonoBehaviour targetMono)
{
targetMono.enabled = true;
Logging.Debug($"[ControllerSwitchItem] Enabled target controller: {targetControllerName}");
}
// Switch input control to the target controller
bool switchSuccess = InputManager.Instance.SwitchToController(targetControllerName);
if (switchSuccess)
{
Logging.Debug($"[ControllerSwitchItem] Successfully switched input to controller: {targetControllerName}");
}
else
{
Debug.LogError($"[ControllerSwitchItem] Failed to switch to controller: {targetControllerName}");
}
}
else
{
Debug.LogError($"[ControllerSwitchItem] Target controller '{targetControllerName}' not found!");
}
// Step 5: Mark as used if one-time use
if (isOneTime)
{
_hasBeenUsed = true;
DisableVisual();
}
_isSwitching = false;
}
private IEnumerator SwitchCamera()
{
switch (cameraSwitchMode)
{
case CameraSwitchMode.None:
// No camera switching
Logging.Debug("[ControllerSwitchItem] No camera switching configured");
break;
case CameraSwitchMode.DirectReference:
if (targetVirtualCamera != null)
{
Logging.Debug($"[ControllerSwitchItem] Blending to camera: {targetVirtualCamera.name}");
// Set the target camera as highest priority
targetVirtualCamera.Priority = 100;
// Wait for camera blend to complete
yield return WaitForCameraBlend();
}
else
{
Debug.LogWarning("[ControllerSwitchItem] DirectReference mode selected but no camera assigned!");
}
break;
case CameraSwitchMode.TrashMazeCameraState:
if (TrashMazeCameraController.Instance != null)
{
Logging.Debug($"[ControllerSwitchItem] Switching to camera state: {targetCameraState}");
// Use the state manager API
if (targetCameraState == TrashMazeCameraState.Gameplay)
{
TrashMazeCameraController.Instance.SwitchToGameplay();
}
else if (targetCameraState == TrashMazeCameraState.Maze)
{
TrashMazeCameraController.Instance.SwitchToMaze();
}
// Wait for camera blend to complete
yield return WaitForCameraBlend();
}
else
{
Debug.LogError("[ControllerSwitchItem] TrashMazeCameraController instance not found in scene!");
}
break;
}
}
private IEnumerator WaitForCameraBlend()
{
CinemachineBrain brain = Camera.main?.GetComponent<CinemachineBrain>();
if (brain != null)
{
// Wait until blend is not active
while (brain.IsBlending)
{
yield return null;
}
Logging.Debug("[ControllerSwitchItem] Camera blend completed");
}
else
{
// If no brain, just wait a brief moment
yield return new WaitForSeconds(0.5f);
}
}
private void DisableVisual()
{
if (visualRepresentation != null)
{
visualRepresentation.SetActive(false);
}
}
#region Save/Load
protected override object GetSerializableState()
{
return new ControllerSwitchItemSaveData
{
hasBeenUsed = _hasBeenUsed
};
}
protected override void ApplySerializableState(string serializedData)
{
try
{
var data = JsonUtility.FromJson<ControllerSwitchItemSaveData>(serializedData);
_hasBeenUsed = data.hasBeenUsed;
Logging.Debug($"[ControllerSwitchItem] Restored state: hasBeenUsed={_hasBeenUsed}");
}
catch (Exception e)
{
Debug.LogError($"[ControllerSwitchItem] Failed to deserialize save data: {e.Message}");
}
}
#endregion
#if UNITY_EDITOR
private void OnValidate()
{
// Visual feedback in editor
if (string.IsNullOrEmpty(targetControllerName))
{
name = "ControllerSwitchItem (UNCONFIGURED)";
}
else
{
name = $"ControllerSwitchItem_To_{targetControllerName}";
}
}
#endif
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 915abd653d714ea3ae11bbf14feafb1e
timeCreated: 1765747971

View File

@@ -56,7 +56,10 @@ public class GlowOutline : ManagedBehaviour
foreach (SpriteRenderer childSprite in childrenSprites)
{
if (itemSprite.sprite != null)
if (!itemSprite)
continue;
if (itemSprite?.sprite != null)
{
childSprite.sprite = itemSprite.sprite;
childSprite.material = outlineMaterial;

View File

@@ -34,7 +34,7 @@ namespace Interactions
public UnityEvent characterArrived;
public UnityEvent<bool> interactionComplete;
private PlayerTouchController playerRef;
private IInteractingCharacter _interactingCharacter;
protected FollowerController FollowerController;
private bool isActive = true;
@@ -69,7 +69,7 @@ namespace Interactions
/// <summary>
/// Dispatch an interaction event to all registered actions and await their completion
/// </summary>
private async Task DispatchEventAsync(InteractionEventType eventType)
private async Task DispatchEventAsync(InteractionEventType eventType, PlayerTouchController playerRef = null)
{
// Collect all tasks from actions that want to respond
List<Task<bool>> tasks = new List<Task<bool>>();
@@ -114,26 +114,52 @@ namespace Interactions
/// </summary>
private async Task StartInteractionFlowAsync()
{
// 2. Find characters
playerRef = FindFirstObjectByType<PlayerTouchController>();
// 2. Find characters - get the ACTIVE controller from InputManager
BasePlayerMovementController playerController = null;
if (InputManager.Instance != null)
{
// Get the controller that currently has input control
var activeController = InputManager.Instance.GetActiveController();
playerController = activeController as BasePlayerMovementController;
}
// Fallback: if InputManager doesn't have an active controller, try to find PlayerTouchController specifically
if (playerController == null)
{
playerController = FindFirstObjectByType<PlayerTouchController>();
Logging.Warning("[Interactable] No active controller from InputManager, falling back to FindFirstObjectByType<PlayerTouchController>");
}
_interactingCharacter = playerController;
FollowerController = FindFirstObjectByType<FollowerController>();
// For legacy event compatibility, try to get PlayerTouchController reference
var playerRef = playerController as PlayerTouchController;
// 3. Virtual hook: Setup
OnInteractionStarted();
// 4. Fire events
interactionStarted?.Invoke(playerRef, FollowerController);
await DispatchEventAsync(InteractionEventType.InteractionStarted);
await DispatchEventAsync(InteractionEventType.InteractionStarted, playerRef);
// 5. Orchestrate character movement
await MoveCharactersAsync();
bool movementSucceeded = await MoveCharactersAsync(playerRef);
// If movement was cancelled, stop the interaction flow
if (!movementSucceeded)
{
Logging.Debug($"[Interactable] Interaction cancelled due to movement failure on {gameObject.name}");
return;
}
// 6. Virtual hook: Arrival reaction
OnInteractingCharacterArrived();
// 7. Fire arrival events
characterArrived?.Invoke();
await DispatchEventAsync(InteractionEventType.InteractingCharacterArrived);
await DispatchEventAsync(InteractionEventType.InteractingCharacterArrived, playerRef);
// 8. Validation (base + child)
var (canProceed, errorMessage) = ValidateInteraction();
@@ -143,7 +169,7 @@ namespace Interactions
{
DebugUIMessage.Show(errorMessage, Color.yellow);
}
FinishInteraction(false);
FinishInteraction(false, playerRef);
return;
}
@@ -151,7 +177,7 @@ namespace Interactions
bool success = DoInteraction();
// 10. Finish up
FinishInteraction(success);
FinishInteraction(success, playerRef);
}
#region Virtual Lifecycle Methods
@@ -260,151 +286,46 @@ namespace Interactions
#region Character Movement Orchestration
/// <summary>
/// Orchestrates character movement based on characterToInteract setting.
/// Delegates movement to the interacting character's controller.
/// Each controller implements its own movement behavior based on this interactable's settings.
/// </summary>
private async Task MoveCharactersAsync()
/// <returns>True if movement succeeded, false if cancelled or failed</returns>
private async Task<bool> MoveCharactersAsync(PlayerTouchController playerRef = null)
{
if (playerRef == null)
if (_interactingCharacter == null)
{
Logging.Debug($"[Interactable] Player character could not be found. Aborting interaction.");
Logging.Debug($"[Interactable] No interacting character found. Aborting interaction.");
interactionInterrupted.Invoke();
await DispatchEventAsync(InteractionEventType.InteractionInterrupted);
return;
await DispatchEventAsync(InteractionEventType.InteractionInterrupted, playerRef);
return false;
}
// If characterToInteract is None, skip movement
if (characterToInteract == CharacterToInteract.None)
{
return; // Continue to arrival
return true; // Continue to arrival
}
// Move player and optionally follower based on characterToInteract setting
if (characterToInteract == CharacterToInteract.Trafalgar)
// Delegate to controller - let it decide how to handle the interaction
bool arrived = await _interactingCharacter.MoveToInteractableAsync(this);
if (!arrived)
{
await MovePlayerAsync();
}
else if (characterToInteract == CharacterToInteract.Pulver || characterToInteract == CharacterToInteract.Both)
{
await MovePlayerAsync(); // Move player to range first
await MoveFollowerAsync(); // Then move follower to interaction point
}
}
/// <summary>
/// Moves the player to the interaction point or custom target.
/// </summary>
private async Task MovePlayerAsync()
{
Vector3 stopPoint = transform.position; // Default to interactable position
bool customTargetFound = false;
// Check for a CharacterMoveToTarget component for Trafalgar or Both
CharacterMoveToTarget[] moveTargets = GetComponentsInChildren<CharacterMoveToTarget>();
foreach (var target in moveTargets)
{
if (target.characterType == CharacterToInteract.Trafalgar || target.characterType == CharacterToInteract.Both)
{
stopPoint = target.GetTargetPosition();
customTargetFound = true;
break;
}
Logging.Debug($"[Interactable] Movement cancelled for {gameObject.name}");
await HandleInteractionCancelledAsync(playerRef);
return false;
}
// If no custom target, use default distance
if (!customTargetFound)
{
Vector3 interactablePos = transform.position;
Vector3 playerPos = playerRef.transform.position;
float stopDistance = characterToInteract == CharacterToInteract.Pulver
? GameManager.Instance.PlayerStopDistance
: GameManager.Instance.PlayerStopDistanceDirectInteraction;
Vector3 toPlayer = (playerPos - interactablePos).normalized;
stopPoint = interactablePos + toPlayer * stopDistance;
}
// Wait for player to arrive
var tcs = new TaskCompletionSource<bool>();
void OnPlayerArrivedLocal()
{
if (playerRef != null)
{
playerRef.OnArrivedAtTarget -= OnPlayerArrivedLocal;
playerRef.OnMoveToCancelled -= OnPlayerMoveCancelledLocal;
}
tcs.TrySetResult(true);
}
void OnPlayerMoveCancelledLocal()
{
if (playerRef != null)
{
playerRef.OnArrivedAtTarget -= OnPlayerArrivedLocal;
playerRef.OnMoveToCancelled -= OnPlayerMoveCancelledLocal;
}
_ = HandleInteractionCancelledAsync();
tcs.TrySetResult(false);
}
playerRef.OnArrivedAtTarget += OnPlayerArrivedLocal;
playerRef.OnMoveToCancelled += OnPlayerMoveCancelledLocal;
playerRef.MoveToAndNotify(stopPoint);
await tcs.Task;
}
/// <summary>
/// Moves the follower to the interaction point or custom target.
/// </summary>
private async Task MoveFollowerAsync()
{
if (FollowerController == null)
return;
// Check for a CharacterMoveToTarget component for Pulver or Both
Vector3 targetPosition = transform.position;
CharacterMoveToTarget[] moveTargets = GetComponentsInChildren<CharacterMoveToTarget>();
foreach (var target in moveTargets)
{
if (target.characterType == CharacterToInteract.Pulver || target.characterType == CharacterToInteract.Both)
{
targetPosition = target.GetTargetPosition();
break;
}
}
// Wait for follower to arrive
var tcs = new TaskCompletionSource<bool>();
void OnFollowerArrivedLocal()
{
if (FollowerController != null)
{
FollowerController.OnPickupArrived -= OnFollowerArrivedLocal;
}
// Tell follower to return to player
if (FollowerController != null && playerRef != null)
{
FollowerController.ReturnToPlayer(playerRef.transform);
}
tcs.TrySetResult(true);
}
FollowerController.OnPickupArrived += OnFollowerArrivedLocal;
FollowerController.GoToPoint(targetPosition);
await tcs.Task;
return true;
}
/// <summary>
/// Handles interaction being cancelled (player stopped moving).
/// </summary>
private async Task HandleInteractionCancelledAsync()
private async Task HandleInteractionCancelledAsync(PlayerTouchController playerRef = null)
{
interactionInterrupted?.Invoke();
await DispatchEventAsync(InteractionEventType.InteractionInterrupted);
await DispatchEventAsync(InteractionEventType.InteractionInterrupted, playerRef);
}
#endregion
@@ -414,14 +335,14 @@ namespace Interactions
/// <summary>
/// Finalizes the interaction after DoInteraction completes.
/// </summary>
private async void FinishInteraction(bool success)
private async void FinishInteraction(bool success, PlayerTouchController playerRef = null)
{
// Virtual hook: Cleanup
OnInteractionFinished(success);
// Fire completion events
interactionComplete?.Invoke(success);
await DispatchEventAsync(InteractionEventType.InteractionComplete);
await DispatchEventAsync(InteractionEventType.InteractionComplete, playerRef);
// Handle one-time / cooldown
if (success)
@@ -437,7 +358,7 @@ namespace Interactions
}
// Reset state
playerRef = null;
_interactingCharacter = null;
FollowerController = null;
}