using System.Collections.Generic; using BlockSpace.Voxels; using Photon.VR.Player; using UnityEngine; namespace BlockSpace.Gamemodes { [DisallowMultipleComponent] [RequireComponent(typeof(Collider))] public sealed class GamemodeZone : MonoBehaviour { [SerializeField] public int zoneID; [SerializeField] public GamemodeType type; private readonly HashSet playersInside = new HashSet(); private Collider zoneCollider; public IReadOnlyCollection PlayersInside => playersInside; private void Awake() { zoneCollider = GetComponent(); if (zoneCollider != null) { zoneCollider.isTrigger = true; } if (GamemodeZoneManager.Instance == null) { GameObject obj = new GameObject("GamemodeZoneManager"); obj.AddComponent(); Object.DontDestroyOnLoad(obj); } } private void OnEnable() { EnsureZoneManager(); GamemodeZoneManager.Instance?.RegisterZone(this); } private void OnDisable() { GamemodeZoneManager.Instance?.UnregisterZone(this); } private void Reset() { Collider component = GetComponent(); if (component != null) { component.isTrigger = true; } } public bool ContainsGroup(VoxelGroup group) { if (group == null || group.size.x <= 0 || group.size.y <= 0 || group.size.z <= 0) { return false; } if (zoneCollider == null) { zoneCollider = GetComponent(); } if (zoneCollider != null) { return zoneCollider.bounds.Intersects(group.GetWorldBounds()); } return false; } public bool ContainsWorldPosition(Vector3 worldPosition) { if (zoneCollider == null) { zoneCollider = GetComponent(); } if (zoneCollider != null) { return zoneCollider.bounds.Contains(worldPosition); } return false; } internal void RefreshPlayersInside(PhotonVRPlayer[] players) { playersInside.Clear(); if (players == null) { return; } foreach (PhotonVRPlayer photonVRPlayer in players) { if (!(photonVRPlayer == null) && !(photonVRPlayer.photonView == null) && photonVRPlayer.photonView.Owner != null && ContainsWorldPosition(GetPlayerZonePosition(photonVRPlayer))) { playersInside.Add(photonVRPlayer.photonView.Owner.ActorNumber); } } } internal static Vector3 GetPlayerZonePosition(PhotonVRPlayer player) { if (player == null) { return Vector3.zero; } if (player.Head != null) { return player.Head.position; } if (!(player.Body != null)) { return player.transform.position; } return player.Body.position; } private static void EnsureZoneManager() { if (!(GamemodeZoneManager.Instance != null)) { GameObject obj = new GameObject("GamemodeZoneManager"); obj.AddComponent(); Object.DontDestroyOnLoad(obj); } } } }