using UnityEngine;
namespace jeanf.universalplayer
{
///
/// A seat expressed as plain values — everything needs to
/// seat the player, with no dependency on a live GameObject/Transform. Both the classic
/// MonoBehaviour and the ECS-baked seat proxy produce one of these,
/// so a single sit/stand path serves the additive-scene flow and the entity world alike.
///
public readonly struct SeatData
{
/// Stable id for entity-world lookups; the classic path uses the seat's instance id.
public readonly int SeatId;
/// Human-readable name for logs/warnings.
public readonly string Name;
/// Where the hips go (world space).
public readonly Vector3 SitPosition;
/// Which way the player faces while seated (world yaw, degrees).
public readonly float SitFacingYaw;
/// Eye height above the sit position while seated.
public readonly float EyeHeightAboveSeat;
/// When false the player returns to where they sat down from on exit.
public readonly bool HasExit;
public readonly Vector3 ExitPosition;
public readonly float ExitFacingYaw;
/// Optional hand rest (chair back / armrest) reached with IK during the glide.
public readonly bool HasHandSupport;
public readonly Vector3 HandSupportWorldPos;
public readonly Quaternion HandSupportWorldRot;
public SeatData(
int seatId,
string name,
Vector3 sitPosition,
float sitFacingYaw,
float eyeHeightAboveSeat,
bool hasExit,
Vector3 exitPosition,
float exitFacingYaw,
bool hasHandSupport,
Vector3 handSupportWorldPos,
Quaternion handSupportWorldRot)
{
SeatId = seatId;
Name = name;
SitPosition = sitPosition;
SitFacingYaw = sitFacingYaw;
EyeHeightAboveSeat = eyeHeightAboveSeat;
HasExit = hasExit;
ExitPosition = exitPosition;
ExitFacingYaw = exitFacingYaw;
HasHandSupport = hasHandSupport;
HandSupportWorldPos = handSupportWorldPos;
HandSupportWorldRot = handSupportWorldRot;
}
}
///
/// Anything the player can sit on. Implemented by the classic and by the
/// ECS seat proxy, so resolves both from one raycast / XR select.
///
public interface ISeatSource
{
SeatData GetSeatData();
/// True when a player or NPC is using this seat. Player SitOn refuses occupied seats.
bool IsOccupied { get; }
/// Mark this seat occupied (NPC / scenario) or free it. Player sit/stand also claims and releases.
void SetOccupied(bool occupied);
}
///
/// Finds a seat by its authored Seat Id across BOTH worlds — for scenario code (e.g.
/// ) that must target a seat it cannot hold a direct
/// reference to: a seat in another additive scene, or one baked into a SubScene.
/// Live s with a non-zero Seat Id register themselves; the entity
/// world's SeatDataBridge registers a resolver covering every currently-baked seat.
///
public static class SeatRegistry
{
/// Resolver signature: true + data when the id is known to this resolver.
public delegate bool SeatResolver(int seatId, out SeatData data);
private static readonly System.Collections.Generic.Dictionary Sources =
new System.Collections.Generic.Dictionary();
private static readonly System.Collections.Generic.List Resolvers =
new System.Collections.Generic.List();
public static void Register(int seatId, ISeatSource source)
{
if (seatId == 0 || source == null) return;
if (Sources.TryGetValue(seatId, out var existing) && !ReferenceEquals(existing, source))
Debug.LogWarning($"[UniversalPlayer] SeatRegistry: two seats share Seat Id {seatId} — " +
"ids must be unique for scenario targeting to be deterministic. The newest wins.");
Sources[seatId] = source;
}
public static void Unregister(int seatId, ISeatSource source)
{
if (seatId == 0) return;
if (Sources.TryGetValue(seatId, out var existing) && ReferenceEquals(existing, source))
Sources.Remove(seatId);
}
public static void RegisterResolver(SeatResolver resolver)
{
if (resolver != null && !Resolvers.Contains(resolver)) Resolvers.Add(resolver);
}
public static void UnregisterResolver(SeatResolver resolver) => Resolvers.Remove(resolver);
/// Seat values for an authored id — live Seats first, then baked-world resolvers.
public static bool TryGetSeatData(int seatId, out SeatData data)
{
if (seatId != 0)
{
if (Sources.TryGetValue(seatId, out var source))
{
data = source.GetSeatData();
return true;
}
for (var i = 0; i < Resolvers.Count; i++)
if (Resolvers[i](seatId, out data)) return true;
}
data = default;
return false;
}
}
}