using jeanf.EventSystem;
using jeanf.validationTools;
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
using System;
using UnityEditor;
#endif
using jeanf.propertyDrawer;
namespace jeanf.universalplayer
{
public class SendTeleportTarget : MonoBehaviour, IDebugBehaviour
{
public bool isDebug
{
get => _isDebug;
set => _isDebug = value;
}
[SerializeField] private bool _isDebug = false;
[Header("Broadcasting on:")]
[Validation("Teleport channel is required — Teleport() raises on it unguarded (a null reference throws) and no teleport is ever sent.")]
[SerializeField] private TeleportEventChannelSO _teleportChannel;
[Header("Teleportation parameters:")]
public bool isTeleportPlayer = false;
[Tooltip("Tick when a script assigns ObjectToTeleport at runtime: the field is hidden and its 'not assigned' warning (field + inspector banner) is skipped.")]
[DrawIf("isTeleportPlayer", false, ComparisonType.Equals, DisablingType.DontDraw)]
[SerializeField] public bool isTargetSetByScript = false;
[DrawIf("isTeleportPlayer", false, ComparisonType.Equals, DisablingType.DontDraw)]
[DrawIf("isTargetSetByScript", false, ComparisonType.Equals, DisablingType.DontDraw)]
[Validation("Object To Teleport is required while Is Teleport Player is off — the teleport event carries nothing to move without it. Tick 'Is Target Set By Script' if a script assigns it at runtime.", RequiredIf = nameof(RequiresObjectToTeleport))]
[SerializeField] public Transform objectToTeleport;
[SerializeField] public bool isUsingFilter = true;
[DrawIf("isUsingFilter", true, ComparisonType.Equals, DisablingType.DontDraw)]
[Validation("Filter is required while Is Using Filter is on — listeners match on it, so an empty filter is accepted by no one (and the debug log dereferences it).", RequiredIf = nameof(isUsingFilter))]
public FilterSO _filter;
[SerializeField] private bool sendEventOnEnable = false;
///
/// Gate for the Object To Teleport validation (field drawer AND scanner banner,
/// which share this rule): only required while this target moves an object (not
/// the player) that no script assigns at runtime.
///
private bool RequiresObjectToTeleport => !isTeleportPlayer && !isTargetSetByScript;
public Transform ObjectToTeleport
{
get => objectToTeleport;
set
{
if(_isDebug) Debug.Log($"ObjectToTeleport name : {(value != null ? value.gameObject.name : "")}");
objectToTeleport = value;
}
}
private void OnEnable()
{
if (sendEventOnEnable) Teleport();
}
public void Teleport()
{
var teleportInformation = new TeleportInformation(objectToTeleport, this.transform, isTeleportPlayer, _filter, isUsingFilter);
if(_isDebug) Debug.Log($"[teleportInformation ({gameObject.name})] " +
$"objectToTeleport : {teleportInformation.objectToTeleport}, " +
$"targetDestination : {teleportInformation.targetDestination}, " +
$"isTeleportPlayer : {teleportInformation.objectIsPlayer}, " +
$"_filter : {teleportInformation.filter.filters[0]}, " +
$"isUsingFilter : {teleportInformation.isUsingFilter}");
_teleportChannel.RaiseEvent(teleportInformation);
WarnIfNothingHandledIt();
}
public void Teleport(bool shouldFade)
{
var teleportInformation = new TeleportInformation(objectToTeleport, this.transform, isTeleportPlayer, _filter, isUsingFilter, shouldFade);
if(_isDebug) Debug.Log($"[teleportInformation ({gameObject.name})] " +
$"objectToTeleport : {teleportInformation.objectToTeleport}, " +
$"targetDestination : {teleportInformation.targetDestination}, " +
$"isTeleportPlayer : {teleportInformation.objectIsPlayer}, " +
$"_filter : {teleportInformation.filter.filters[0]}, " +
$"isUsingFilter : {teleportInformation.isUsingFilter}, " +
$"shouldFade : {teleportInformation.shouldFade}");
_teleportChannel.RaiseEvent(teleportInformation);
WarnIfNothingHandledIt();
}
///
/// TeleportOnEvent listeners run synchronously inside RaiseEvent, so if none of
/// them accepted this teleport by now, it went nowhere — say so instead of
/// letting the button do nothing silently.
///
private void WarnIfNothingHandledIt()
{
if (TeleportOnEvent.LastHandledFrame == Time.frameCount) return;
Debug.LogWarning($"[UniversalPlayer.XR] Teleport requested via '{name}' but NO TeleportOnEvent accepted it — nothing moved. Checklist: " +
"(1) a TeleportOnEvent exists in the scene (usually on the Player variant); " +
"(2) its 'Receiving on channel' asset is the SAME TeleportEventChannel this target broadcasts on; " +
"(3) its OnEventRaised UnityEvent is wired to TeleportOnEvent.Teleport; " +
$"(4) filters: this target {(isUsingFilter ? $"uses filter '{(_filter != null ? _filter.name : "")}' which must be in the listener's filter list" : "uses no filter")}. " +
"(Ignore if a custom listener handles teleports in this project.)", this);
}
#if UNITY_EDITOR
[DrawGizmo(GizmoType.Pickable)]
private void OnDrawGizmos()
{
if (!this.transform) return;
var thisTransform = transform.position;
Gizmos.color = new Color(.2f, .6f, .2f, .5f);
Gizmos.DrawSphere(thisTransform, .25f);
Gizmos.color = new Color(.2f, .6f, .2f, 1f);
Gizmos.DrawWireSphere(thisTransform, .5f);
// Blue Arrow for teleport direction
var a = transform.position + transform.rotation * new Vector3(0, 0, .5f); // tip of the arrow
var b = transform.position + transform.rotation * new Vector3(-.05f, 0, .45f); // bottom left of the arrow
var c = transform.position + transform.rotation * new Vector3(.05f, 0, .45f); // bottom right of the arrow
Gizmos.color = new Color(0, 0, 1, 1f); // using blue color (same as the axis)
Gizmos.DrawLine(thisTransform, a);
Gizmos.DrawLine(c, b);
Gizmos.DrawLine(c, a);
Gizmos.DrawLine(b, a);
}
#endif
}
}