133 lines
2.8 KiB
C#
133 lines
2.8 KiB
C#
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<int> playersInside = new HashSet<int>();
|
|
|
|
private Collider zoneCollider;
|
|
|
|
public IReadOnlyCollection<int> PlayersInside => playersInside;
|
|
|
|
private void Awake()
|
|
{
|
|
zoneCollider = GetComponent<Collider>();
|
|
if (zoneCollider != null)
|
|
{
|
|
zoneCollider.isTrigger = true;
|
|
}
|
|
if (GamemodeZoneManager.Instance == null)
|
|
{
|
|
GameObject obj = new GameObject("GamemodeZoneManager");
|
|
obj.AddComponent<GamemodeZoneManager>();
|
|
Object.DontDestroyOnLoad(obj);
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
EnsureZoneManager();
|
|
GamemodeZoneManager.Instance?.RegisterZone(this);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
GamemodeZoneManager.Instance?.UnregisterZone(this);
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
Collider component = GetComponent<Collider>();
|
|
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<Collider>();
|
|
}
|
|
if (zoneCollider != null)
|
|
{
|
|
return zoneCollider.bounds.Intersects(group.GetWorldBounds());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool ContainsWorldPosition(Vector3 worldPosition)
|
|
{
|
|
if (zoneCollider == null)
|
|
{
|
|
zoneCollider = GetComponent<Collider>();
|
|
}
|
|
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<GamemodeZoneManager>();
|
|
Object.DontDestroyOnLoad(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|