178 lines
5.8 KiB
C#
178 lines
5.8 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using SomniumSpace.Bridge;
|
|
using SomniumSpace.Bridge.Player;
|
|
|
|
namespace UFMTESTING1
|
|
{
|
|
public class AdminManager : MonoBehaviour
|
|
{
|
|
[Header("Admin Location Target")]
|
|
[SerializeField] private Transform adminTarget; // Drag your admin panel location GameObject here (position and rotation will be used)
|
|
|
|
[Header("Objects to Activate for Admin")]
|
|
[SerializeField] private List<GameObject> activateForAdmin = new List<GameObject>();
|
|
|
|
[Header("Objects to Deactivate for Admin")]
|
|
[SerializeField] private List<GameObject> deactivateForAdmin = new List<GameObject>();
|
|
|
|
[Header("Admin Usernames (exact match)")]
|
|
[SerializeField] private List<string> adminUsernames = new List<string>();
|
|
|
|
[Header("Timing Settings")]
|
|
[SerializeField] private float readyCheckInterval = 0.5f; // How often to check if player is ready
|
|
[SerializeField] private float maxReadyWait = 10f; // Max seconds to wait for player ready
|
|
|
|
[Header("Enforcement Check (for respawn)")]
|
|
[SerializeField] private float toggleCheckInterval = 1f; // How often to re-apply toggles (seconds)
|
|
|
|
private bool _hasCheckedLocalPlayer;
|
|
private ISomniumPlayer localPlayer;
|
|
private bool isAdmin;
|
|
private float toggleCheckTimer;
|
|
|
|
private void Start()
|
|
{
|
|
// Set all to default (non-admin) state
|
|
foreach (var obj in activateForAdmin)
|
|
{
|
|
if (obj != null) obj.SetActive(false);
|
|
}
|
|
foreach (var obj in deactivateForAdmin)
|
|
{
|
|
if (obj != null) obj.SetActive(true);
|
|
}
|
|
|
|
CheckLocalPlayerImmediately();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
var container = SomniumBridge.PlayersContainer;
|
|
if (container != null)
|
|
{
|
|
container.OnLocalPlayerAdded += OnLocalPlayerAdded;
|
|
}
|
|
CheckLocalPlayerImmediately();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
var container = SomniumBridge.PlayersContainer;
|
|
if (container != null)
|
|
{
|
|
container.OnLocalPlayerAdded -= OnLocalPlayerAdded;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isAdmin)
|
|
{
|
|
toggleCheckTimer += Time.deltaTime;
|
|
if (toggleCheckTimer >= toggleCheckInterval)
|
|
{
|
|
toggleCheckTimer = 0f;
|
|
ApplyAdminToggles();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CheckLocalPlayerImmediately()
|
|
{
|
|
var player = SomniumBridge.PlayersContainer?.LocalPlayer;
|
|
if (player != null && !_hasCheckedLocalPlayer)
|
|
{
|
|
OnLocalPlayerAdded(player);
|
|
}
|
|
}
|
|
|
|
private void OnLocalPlayerAdded(ISomniumPlayer player)
|
|
{
|
|
if (_hasCheckedLocalPlayer) return;
|
|
|
|
localPlayer = player;
|
|
_hasCheckedLocalPlayer = true;
|
|
|
|
string nickname = player.Properties?.NickName;
|
|
if (string.IsNullOrEmpty(nickname))
|
|
{
|
|
Debug.LogWarning("[ADMIN SCRIPT] Local player nickname is empty - cannot check admin.");
|
|
return;
|
|
}
|
|
|
|
isAdmin = adminUsernames.Contains(nickname);
|
|
Debug.Log($"[ADMIN SCRIPT] Player {nickname} joined - admin: {isAdmin}");
|
|
|
|
if (isAdmin)
|
|
{
|
|
StartCoroutine(WaitForPlayerReadyAndAdmin());
|
|
}
|
|
}
|
|
|
|
private IEnumerator WaitForPlayerReadyAndAdmin()
|
|
{
|
|
float waitTime = 0f;
|
|
while (waitTime < maxReadyWait)
|
|
{
|
|
if (localPlayer?.Features?.Motion != null && localPlayer.References?.Body?.Root != null)
|
|
{
|
|
Debug.Log("[ADMIN SCRIPT] Player ready - applying admin settings");
|
|
|
|
// Apply toggles first
|
|
ApplyAdminToggles();
|
|
|
|
if (adminTarget != null)
|
|
{
|
|
// Teleport
|
|
localPlayer.Features.Motion.DoTeleportToPoint(
|
|
adminTarget.position,
|
|
adminTarget.eulerAngles,
|
|
OnTeleportFinished
|
|
);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[ADMIN SCRIPT] Admin target not set - skipping teleport.");
|
|
}
|
|
|
|
yield break;
|
|
}
|
|
|
|
Debug.Log("[ADMIN SCRIPT] Waiting for player ready...");
|
|
yield return new WaitForSeconds(readyCheckInterval);
|
|
waitTime += readyCheckInterval;
|
|
}
|
|
|
|
Debug.LogError("[ADMIN SCRIPT] Timed out waiting for player ready - admin may not apply!");
|
|
}
|
|
|
|
private void ApplyAdminToggles()
|
|
{
|
|
Debug.Log($"[ADMIN SCRIPT] Applying toggles: {activateForAdmin.Count} to activate, {deactivateForAdmin.Count} to deactivate...");
|
|
|
|
foreach (var obj in activateForAdmin)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
obj.SetActive(true);
|
|
Debug.Log($"[ADMIN SCRIPT] Activated {obj.name}");
|
|
}
|
|
}
|
|
foreach (var obj in deactivateForAdmin)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
obj.SetActive(false);
|
|
Debug.Log($"[ADMIN SCRIPT] Deactivated {obj.name}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTeleportFinished()
|
|
{
|
|
Debug.Log("[ADMIN SCRIPT] Admin player teleported to the panel.");
|
|
}
|
|
}
|
|
} |