using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// GAME 1303 Tier 8 — A doorway that changes the camera mode. Apep's Trial.
///
/// SETUP
/// - Empty GameObject + Collider (3D BoxCollider, Is Trigger) at the threshold.
/// - Set Mode On Enter, and optionally Mode On Exit for a two-way doorway (walk back
/// through and you return to the previous view).
/// - Reposition Player: drag an empty transform to snap the player onto the 2D plane
/// when entering side-scroll mode. Without this the player can end up off-plane and
/// appear to walk into the background — the single most common bug in this kind of game.
/// </summary>
public class ModeSwitchTrigger : MonoBehaviour
{
public CameraModeSwitcher switcher;
public CameraModeSwitcher.Mode modeOnEnter = CameraModeSwitcher.Mode.SideScroll2D;
public bool restoreOnExit = false;
public CameraModeSwitcher.Mode modeOnExit = CameraModeSwitcher.Mode.FirstPerson3D;
[Header("Snap the player onto the plane")]
public Transform repositionPlayerTo;
public bool snapZOnly = true;
public UnityEvent OnEntered;
public UnityEvent OnExited;
void Reset()
{
var c = GetComponent<Collider>();
if (c != null) c.isTrigger = true;
}
void Start()
{
if (switcher == null) switcher = FindFirstObjectByType<CameraModeSwitcher>();
}
void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player") || switcher == null) return;
Reposition(other.transform);
switcher.SetMode(modeOnEnter);
OnEntered?.Invoke();
}
void OnTriggerExit(Collider other)
{
if (!restoreOnExit || !other.CompareTag("Player") || switcher == null) return;
switcher.SetMode(modeOnExit);
OnExited?.Invoke();
}
void Reposition(Transform player)
{
if (repositionPlayerTo == null) return;
player.position = snapZOnly
? new Vector3(player.position.x, player.position.y, repositionPlayerTo.position.z)
: repositionPlayerTo.position;
}
}