Add detective board
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
using UnityEngine;
|
||||
using Unheimlich.Data.CameraSettings;
|
||||
using Unheimlich.Gameplay.CameraControl;
|
||||
using Unheimlich.Gameplay.Player;
|
||||
using PlayerInput = Unheimlich.Managers.PlayerInputManager;
|
||||
|
||||
namespace Unheimlich.Gameplay.DetectiveBoard
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls player interaction with the detective board in world space.
|
||||
/// Uses CameraPanController for wide-angle camera movement across the board.
|
||||
/// Activates when player is in range and looking at the board.
|
||||
/// </summary>
|
||||
public class DetectiveBoardController : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private CameraPanController _cameraPanController;
|
||||
[SerializeField] private Transform _boardTransform;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private CameraPanSettings _boardCameraPanSettings;
|
||||
[SerializeField] private float _activationRange = 2f;
|
||||
[SerializeField] private LayerMask _boardLayer;
|
||||
|
||||
private Camera _playerCamera;
|
||||
private bool _isBoardActive;
|
||||
private CameraPanSettings _originalCameraPanSettings;
|
||||
private CameraHeadBob _cameraHeadBob;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_playerCamera = Camera.main;
|
||||
|
||||
if (_cameraPanController == null)
|
||||
{
|
||||
_cameraPanController = FindFirstObjectByType<CameraPanController>();
|
||||
}
|
||||
|
||||
_cameraHeadBob = FindFirstObjectByType<CameraHeadBob>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isBoardActive)
|
||||
{
|
||||
HandleBoardInteraction();
|
||||
}
|
||||
else
|
||||
{
|
||||
CheckForBoardActivation();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if player is looking at the board and in range to activate.
|
||||
/// </summary>
|
||||
private void CheckForBoardActivation()
|
||||
{
|
||||
if (_playerCamera == null || _boardTransform == null)
|
||||
return;
|
||||
|
||||
float distanceToBoard = Vector3.Distance(_playerCamera.transform.position, _boardTransform.position);
|
||||
if (distanceToBoard > _activationRange)
|
||||
return;
|
||||
|
||||
Ray ray = new Ray(_playerCamera.transform.position, _playerCamera.transform.forward);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, _activationRange, _boardLayer))
|
||||
{
|
||||
if (hit.transform.IsChildOf(_boardTransform) || hit.transform == _boardTransform)
|
||||
{
|
||||
var interactAction = PlayerInput.GetInteractAction();
|
||||
if (interactAction != null && interactAction.WasPressedThisFrame())
|
||||
{
|
||||
ActivateBoard();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activates the detective board, switching to pan mode with wide-angle settings.
|
||||
/// </summary>
|
||||
public void ActivateBoard()
|
||||
{
|
||||
if (_cameraPanController == null || _boardCameraPanSettings == null)
|
||||
{
|
||||
Debug.LogError("[DetectiveBoardController] Missing required references!");
|
||||
return;
|
||||
}
|
||||
|
||||
_isBoardActive = true;
|
||||
|
||||
_originalCameraPanSettings = _cameraPanController.Settings;
|
||||
_cameraPanController.Settings = _boardCameraPanSettings;
|
||||
|
||||
_cameraPanController.SaveOriginalState();
|
||||
_cameraPanController.AlignToTarget(_boardTransform, () =>
|
||||
{
|
||||
_cameraPanController.StartPan();
|
||||
});
|
||||
|
||||
PlayerInput.SetContext(PlayerInput.InputContext.Listening);
|
||||
|
||||
ApplyHeadBobSettings(_boardCameraPanSettings);
|
||||
|
||||
Debug.Log("[DetectiveBoardController] Detective board activated");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles board interaction while active (panning and exit).
|
||||
/// </summary>
|
||||
private void HandleBoardInteraction()
|
||||
{
|
||||
if (_cameraPanController != null)
|
||||
{
|
||||
_cameraPanController.UpdatePan();
|
||||
}
|
||||
|
||||
var exitAction = PlayerInput.GetExitAction();
|
||||
if (exitAction != null && exitAction.WasPressedThisFrame())
|
||||
{
|
||||
DeactivateBoard();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deactivates the board and returns to normal gameplay.
|
||||
/// </summary>
|
||||
public void DeactivateBoard()
|
||||
{
|
||||
if (!_isBoardActive)
|
||||
return;
|
||||
|
||||
_isBoardActive = false;
|
||||
|
||||
if (_cameraPanController != null)
|
||||
{
|
||||
_cameraPanController.StopPan();
|
||||
_cameraPanController.ReturnToOriginalPosition(() =>
|
||||
{
|
||||
if (_originalCameraPanSettings != null)
|
||||
{
|
||||
_cameraPanController.Settings = _originalCameraPanSettings;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
PlayerInput.SetContext(PlayerInput.InputContext.Normal);
|
||||
|
||||
RestoreHeadBob();
|
||||
|
||||
Debug.Log("[DetectiveBoardController] Detective board deactivated");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies head bob settings based on CameraPanSettings configuration.
|
||||
/// </summary>
|
||||
private void ApplyHeadBobSettings(CameraPanSettings settings)
|
||||
{
|
||||
if (_cameraHeadBob == null || settings == null)
|
||||
return;
|
||||
|
||||
_cameraHeadBob.SetMode(settings.HeadBobMode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores head bob to normal behavior.
|
||||
/// </summary>
|
||||
private void RestoreHeadBob()
|
||||
{
|
||||
if (_cameraHeadBob == null)
|
||||
return;
|
||||
|
||||
_cameraHeadBob.ClearOverride();
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (_boardTransform == null)
|
||||
return;
|
||||
|
||||
Gizmos.color = _isBoardActive ? Color.green : Color.yellow;
|
||||
Gizmos.DrawWireSphere(_boardTransform.position, _activationRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user