345 lines
12 KiB
C#
345 lines
12 KiB
C#
using UnityEngine;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
[Header("Cinemachine 3 Setup")]
|
|
public CinemachineCamera focusCameraA;
|
|
public CinemachineCamera focusCameraB;
|
|
public CinemachineCamera explorationCamera;
|
|
|
|
public int activePriority = 20;
|
|
public int inactivePriority = 10;
|
|
|
|
[Header("Raycast Setup")]
|
|
public Camera mainUnityCamera;
|
|
public LayerMask interactableLayer;
|
|
|
|
[Header("UI Setup")]
|
|
public Button talkButton;
|
|
public Button doorButton; // New button for the doors
|
|
private GuardManager currentGuardManager;
|
|
private DoorManager currentDoorManager; // New reference for the door
|
|
|
|
[Header("Hover Highlight Settings")]
|
|
public Color highlightColour = Color.yellow;
|
|
private Renderer currentlyHoveredRenderer;
|
|
private Color originalColour;
|
|
|
|
[Header("Exploration Nudge Settings")]
|
|
public float nudgeSpeed = 15f;
|
|
public float maxNudgeAngle = 8f;
|
|
public float edgeThickness = 50f;
|
|
|
|
[Header("Focus Nudge Settings")]
|
|
public float focusNudgeSpeed = 5f;
|
|
public float maxFocusOffset = 2f;
|
|
|
|
// Exploration rotation tracker (Horizontal only now)
|
|
private Quaternion startingRotation;
|
|
private float currentYaw = 0f;
|
|
|
|
// Ping-Pong Focus Trackers
|
|
private bool useCameraA = true;
|
|
private Transform dynamicFocusTargetA;
|
|
private Transform dynamicFocusTargetB;
|
|
private Transform activeDynamicTarget;
|
|
private Transform currentInteractableTransform;
|
|
|
|
// Focus offset (Horizontal only now)
|
|
private float currentFocusOffsetX = 0f;
|
|
|
|
void Start()
|
|
{
|
|
if (explorationCamera != null)
|
|
{
|
|
startingRotation = explorationCamera.transform.rotation;
|
|
}
|
|
|
|
dynamicFocusTargetA = new GameObject("DynamicFocusTarget_A").transform;
|
|
dynamicFocusTargetB = new GameObject("DynamicFocusTarget_B").transform;
|
|
|
|
if (talkButton != null)
|
|
{
|
|
talkButton.gameObject.SetActive(false);
|
|
talkButton.onClick.AddListener(OnTalkButtonClicked);
|
|
}
|
|
|
|
if (doorButton != null)
|
|
{
|
|
doorButton.gameObject.SetActive(false);
|
|
doorButton.onClick.AddListener(OnDoorButtonClicked);
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Mouse.current == null) return;
|
|
|
|
HandleHover();
|
|
HandleEdgeNudge();
|
|
|
|
if (Mouse.current.leftButton.wasPressedThisFrame)
|
|
{
|
|
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
// TRIPWIRE 1: ADD THIS LINE HERE
|
|
Debug.Log("⚠️ Click cancelled! The EventSystem thinks your mouse is hovering over a UI element.");
|
|
return;
|
|
}
|
|
HandleInteractionClick();
|
|
}
|
|
|
|
if (Mouse.current.rightButton.wasPressedThisFrame)
|
|
{
|
|
ReturnToMainCamera();
|
|
}
|
|
|
|
if (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame)
|
|
{
|
|
ResetCameraPositions();
|
|
}
|
|
}
|
|
|
|
private void HandleEdgeNudge()
|
|
{
|
|
Vector2 mousePos = Mouse.current.position.ReadValue();
|
|
float yawInput = 0f;
|
|
|
|
// Check horizontal edges only
|
|
if (mousePos.x < edgeThickness) yawInput = -1f;
|
|
else if (mousePos.x > Screen.width - edgeThickness) yawInput = 1f;
|
|
|
|
bool isFocused = focusCameraA.Priority >= activePriority || focusCameraB.Priority >= activePriority;
|
|
|
|
if (yawInput != 0f)
|
|
{
|
|
if (isFocused && currentInteractableTransform != null && activeDynamicTarget != null)
|
|
{
|
|
currentFocusOffsetX += yawInput * focusNudgeSpeed * Time.deltaTime;
|
|
currentFocusOffsetX = Mathf.Clamp(currentFocusOffsetX, -maxFocusOffset, maxFocusOffset);
|
|
|
|
// Move the invisible point horizontally relative to the camera
|
|
activeDynamicTarget.position = currentInteractableTransform.position
|
|
+ (mainUnityCamera.transform.right * currentFocusOffsetX);
|
|
}
|
|
else if (!isFocused && explorationCamera != null)
|
|
{
|
|
currentYaw += yawInput * nudgeSpeed * Time.deltaTime;
|
|
currentYaw = Mathf.Clamp(currentYaw, -maxNudgeAngle, maxNudgeAngle);
|
|
|
|
Quaternion yawRotation = Quaternion.AngleAxis(currentYaw, Vector3.up);
|
|
explorationCamera.transform.rotation = startingRotation * yawRotation;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleHover()
|
|
{
|
|
Vector2 mousePos = Mouse.current.position.ReadValue();
|
|
Ray ray = mainUnityCamera.ScreenPointToRay(mousePos);
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, interactableLayer))
|
|
{
|
|
Renderer hitRenderer = hit.transform.GetComponentInChildren<Renderer>();
|
|
|
|
if (hitRenderer != null && hitRenderer != currentlyHoveredRenderer)
|
|
{
|
|
RemoveHighlight();
|
|
currentlyHoveredRenderer = hitRenderer;
|
|
|
|
if (currentlyHoveredRenderer is SpriteRenderer sprite)
|
|
{
|
|
originalColour = sprite.color;
|
|
sprite.color = highlightColour;
|
|
}
|
|
else
|
|
{
|
|
originalColour = currentlyHoveredRenderer.material.color;
|
|
currentlyHoveredRenderer.material.color = highlightColour;
|
|
}
|
|
|
|
|
|
if (currentlyHoveredRenderer.gameObject.GetComponentInParent<DoorManager>() != null)
|
|
{
|
|
currentlyHoveredRenderer.gameObject.GetComponentInParent<DoorManager>().isfocussed = 1;
|
|
}
|
|
|
|
if (currentlyHoveredRenderer.gameObject.GetComponentInParent<GuardManager>() != null)
|
|
{
|
|
currentlyHoveredRenderer.gameObject.GetComponentInParent<GuardManager>().isfocussed = 1;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
RemoveHighlight();
|
|
}
|
|
}
|
|
|
|
private void RemoveHighlight()
|
|
{
|
|
if (currentlyHoveredRenderer != null)
|
|
{
|
|
if (currentlyHoveredRenderer is SpriteRenderer sprite)
|
|
{
|
|
sprite.color = originalColour;
|
|
}
|
|
else
|
|
{
|
|
currentlyHoveredRenderer.material.color = originalColour;
|
|
}
|
|
|
|
if (
|
|
currentlyHoveredRenderer.gameObject.GetComponentInParent<DoorManager>() != null &&
|
|
currentlyHoveredRenderer.gameObject.GetComponentInParent<DoorManager>() != currentDoorManager
|
|
)
|
|
{
|
|
currentlyHoveredRenderer.gameObject.GetComponentInParent<DoorManager>().isfocussed = 0;
|
|
}
|
|
|
|
if (
|
|
currentlyHoveredRenderer.gameObject.GetComponent<GuardManager>() != null &&
|
|
currentlyHoveredRenderer.gameObject.GetComponent<GuardManager>() != currentGuardManager
|
|
)
|
|
{
|
|
currentlyHoveredRenderer.gameObject.GetComponent<GuardManager>().isfocussed = 0;
|
|
}
|
|
|
|
currentlyHoveredRenderer = null;
|
|
}
|
|
}
|
|
|
|
private void HandleInteractionClick()
|
|
{
|
|
Vector2 mousePos = Mouse.current.position.ReadValue();
|
|
Ray ray = mainUnityCamera.ScreenPointToRay(mousePos);
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, interactableLayer))
|
|
{
|
|
// TRIPWIRE 2: ADD THIS LINE HERE
|
|
Debug.Log($"🎯 Raycast successfully hit: {hit.transform.name}");
|
|
|
|
currentInteractableTransform = hit.transform;
|
|
currentFocusOffsetX = 0f;
|
|
|
|
CinemachineCamera nextCam = useCameraA ? focusCameraA : focusCameraB;
|
|
CinemachineCamera oldCam = useCameraA ? focusCameraB : focusCameraA;
|
|
Transform nextTarget = useCameraA ? dynamicFocusTargetA : dynamicFocusTargetB;
|
|
|
|
// TRIPWIRE 3: ADD THIS SAFETY CHECK HERE
|
|
if (nextCam == null)
|
|
{
|
|
Debug.LogError("🚨 ERROR: One of your Focus Cameras is missing in the GameManager Inspector!");
|
|
return;
|
|
}
|
|
|
|
nextTarget.position = currentInteractableTransform.position;
|
|
nextCam.Follow = currentInteractableTransform;
|
|
nextCam.LookAt = nextTarget;
|
|
|
|
nextCam.Priority = activePriority;
|
|
oldCam.Priority = inactivePriority;
|
|
|
|
activeDynamicTarget = nextTarget;
|
|
useCameraA = !useCameraA;
|
|
|
|
// Logic to determine which UI button to show
|
|
GuardManager guard = hit.transform.GetComponent<GuardManager>();
|
|
DoorManager door = hit.transform.GetComponent<DoorManager>();
|
|
|
|
// Hide both first to be safe, then reveal the correct one
|
|
if (talkButton != null) talkButton.gameObject.SetActive(false);
|
|
if (doorButton != null) doorButton.gameObject.SetActive(false);
|
|
|
|
UnsetAll();
|
|
currentDoorManager = null;
|
|
currentGuardManager = null;
|
|
|
|
if (guard != null)
|
|
{
|
|
currentGuardManager = guard;
|
|
guard.isfocussed = 2;
|
|
if (talkButton != null) talkButton.gameObject.SetActive(true);
|
|
}
|
|
else if (door != null)
|
|
{
|
|
currentDoorManager = door;
|
|
door.isfocussed = 2;
|
|
if (doorButton != null) doorButton.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// TRIPWIRE 4: ADD THIS LINE HERE
|
|
Debug.Log("💨 Raycast hit nothing on the Interactable layer. Returning to main camera.");
|
|
ReturnToMainCamera();
|
|
}
|
|
}
|
|
|
|
private void OnTalkButtonClicked()
|
|
{
|
|
if (currentGuardManager != null)
|
|
{
|
|
DialogManager.Instance.StartDialog(currentGuardManager);
|
|
talkButton.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnDoorButtonClicked()
|
|
{
|
|
if (currentDoorManager != null)
|
|
{
|
|
// Add your door sequence logic here!
|
|
Debug.Log("Door sequence started!");
|
|
doorButton.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void ReturnToMainCamera()
|
|
{
|
|
focusCameraA.Priority = inactivePriority;
|
|
focusCameraB.Priority = inactivePriority;
|
|
|
|
focusCameraA.Follow = null; focusCameraA.LookAt = null;
|
|
focusCameraB.Follow = null; focusCameraB.LookAt = null;
|
|
|
|
currentInteractableTransform = null;
|
|
activeDynamicTarget = null;
|
|
currentGuardManager = null;
|
|
currentDoorManager = null;
|
|
|
|
UnsetAll();
|
|
if (talkButton != null) talkButton.gameObject.SetActive(false);
|
|
if (doorButton != null) doorButton.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void ResetCameraPositions()
|
|
{
|
|
ReturnToMainCamera();
|
|
|
|
currentFocusOffsetX = 0f;
|
|
currentYaw = 0f;
|
|
|
|
if (explorationCamera != null)
|
|
{
|
|
explorationCamera.transform.rotation = startingRotation;
|
|
}
|
|
}
|
|
|
|
private void UnsetAll()
|
|
{
|
|
foreach (var doorManager in LevelLoader.Instance.Doors)
|
|
{
|
|
doorManager.isfocussed = 0;
|
|
}
|
|
foreach (var doorManager in LevelLoader.Instance.Guards)
|
|
{
|
|
doorManager.isfocussed = 0;
|
|
}
|
|
|
|
}
|
|
} |