105 lines
2.6 KiB
C#
105 lines
2.6 KiB
C#
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.SDKBase;
|
|
using VRC.Udon;
|
|
|
|
public class TeamMachineCollider : UdonSharpBehaviour
|
|
{
|
|
[SerializeField] private VRCPlayerApi[] playersInside = new VRCPlayerApi[0];
|
|
|
|
private void Start()
|
|
{
|
|
if (playersInside == null)
|
|
{
|
|
playersInside = new VRCPlayerApi[0];
|
|
}
|
|
}
|
|
|
|
public override void OnPlayerTriggerEnter(VRCPlayerApi player)
|
|
{
|
|
if (Utilities.IsValid(player))
|
|
{
|
|
AddPlayer(player);
|
|
}
|
|
}
|
|
|
|
public override void OnPlayerTriggerExit(VRCPlayerApi player)
|
|
{
|
|
if (Utilities.IsValid(player))
|
|
{
|
|
RemovePlayer(player);
|
|
}
|
|
}
|
|
|
|
public override void OnPlayerLeft(VRCPlayerApi player)
|
|
{
|
|
if (Utilities.IsValid(player))
|
|
{
|
|
RemovePlayer(player);
|
|
}
|
|
}
|
|
|
|
public VRCPlayerApi[] GetPlayersInside()
|
|
{
|
|
if (playersInside == null) return new VRCPlayerApi[0];
|
|
return playersInside;
|
|
}
|
|
|
|
private void AddPlayer(VRCPlayerApi playerToAdd)
|
|
{
|
|
if (playersInside == null) playersInside = new VRCPlayerApi[0];
|
|
|
|
// Check if already exists
|
|
for (int i = 0; i < playersInside.Length; i++)
|
|
{
|
|
if (playersInside[i] == playerToAdd) return;
|
|
}
|
|
|
|
// Create new array with +1 size
|
|
VRCPlayerApi[] newArray = new VRCPlayerApi[playersInside.Length + 1];
|
|
for (int i = 0; i < playersInside.Length; i++)
|
|
{
|
|
newArray[i] = playersInside[i];
|
|
}
|
|
newArray[playersInside.Length] = playerToAdd;
|
|
playersInside = newArray;
|
|
}
|
|
|
|
private void RemovePlayer(VRCPlayerApi playerToRemove)
|
|
{
|
|
if (playersInside == null)
|
|
{
|
|
playersInside = new VRCPlayerApi[0];
|
|
return;
|
|
}
|
|
|
|
// Count valid players that are NOT the one we are removing
|
|
int newCount = 0;
|
|
for (int i = 0; i < playersInside.Length; i++)
|
|
{
|
|
VRCPlayerApi p = playersInside[i];
|
|
// Keep player if it is valid AND it is not the player we are removing
|
|
if (Utilities.IsValid(p) && p != playerToRemove)
|
|
{
|
|
newCount++;
|
|
}
|
|
}
|
|
|
|
// Create new array
|
|
VRCPlayerApi[] newArray = new VRCPlayerApi[newCount];
|
|
int idx = 0;
|
|
for (int i = 0; i < playersInside.Length; i++)
|
|
{
|
|
VRCPlayerApi p = playersInside[i];
|
|
if (Utilities.IsValid(p) && p != playerToRemove)
|
|
{
|
|
newArray[idx] = p;
|
|
idx++;
|
|
}
|
|
}
|
|
|
|
playersInside = newArray;
|
|
}
|
|
}
|