171 lines
4.6 KiB
C#
171 lines
4.6 KiB
C#
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.SDKBase;
|
|
using VRC.Udon;
|
|
using System;
|
|
|
|
public class PlayerManager : UdonSharpBehaviour
|
|
{
|
|
[UdonSynced] public int[] playerIds = new int[0];
|
|
[UdonSynced] private int[] playerScores = new int[0];
|
|
[UdonSynced] private string[] playerTeams = new string[0];
|
|
[UdonSynced] public int[] currentPlayerIds = new int[0];
|
|
|
|
public override void OnPlayerJoined(VRCPlayerApi player)
|
|
{
|
|
if (!Networking.IsOwner(gameObject)) return;
|
|
|
|
int id = player.playerId;
|
|
int index = GetPlayerIndex(id);
|
|
|
|
if (index == -1)
|
|
{
|
|
int[] newIds = new int[playerIds.Length + 1];
|
|
int[] newScores = new int[playerScores.Length + 1];
|
|
string[] newTeams = new string[playerTeams.Length + 1];
|
|
|
|
Array.Copy(playerIds, newIds, playerIds.Length);
|
|
Array.Copy(playerScores, newScores, playerScores.Length);
|
|
Array.Copy(playerTeams, newTeams, playerTeams.Length);
|
|
|
|
newIds[playerIds.Length] = id;
|
|
newScores[playerScores.Length] = 0;
|
|
newTeams[playerTeams.Length] = "unassigned";
|
|
|
|
playerIds = newIds;
|
|
playerScores = newScores;
|
|
playerTeams = newTeams;
|
|
}
|
|
|
|
// Add to current players
|
|
int[] newCurrentIds = new int[currentPlayerIds.Length + 1];
|
|
Array.Copy(currentPlayerIds, newCurrentIds, currentPlayerIds.Length);
|
|
newCurrentIds[currentPlayerIds.Length] = id;
|
|
currentPlayerIds = newCurrentIds;
|
|
|
|
RequestSerialization();
|
|
}
|
|
|
|
public override void OnPlayerLeft(VRCPlayerApi player)
|
|
{
|
|
if (!Networking.IsOwner(gameObject)) return;
|
|
|
|
// Remove from current players
|
|
int[] newCurrentIds = new int[currentPlayerIds.Length - 1];
|
|
int j = 0;
|
|
for (int i = 0; i < currentPlayerIds.Length; i++)
|
|
{
|
|
if (currentPlayerIds[i] != player.playerId)
|
|
{
|
|
newCurrentIds[j] = currentPlayerIds[i];
|
|
j++;
|
|
}
|
|
}
|
|
currentPlayerIds = newCurrentIds;
|
|
|
|
RequestSerialization();
|
|
}
|
|
|
|
public void SetScore(int playerId, int score)
|
|
{
|
|
if (!Networking.IsOwner(gameObject)) return;
|
|
|
|
for (int i = 0; i < playerIds.Length; i++)
|
|
{
|
|
if (playerIds[i] == playerId)
|
|
{
|
|
playerScores[i] = score;
|
|
break;
|
|
}
|
|
}
|
|
RequestSerialization();
|
|
}
|
|
|
|
public int GetScore(int playerId)
|
|
{
|
|
for (int i = 0; i < playerIds.Length; i++)
|
|
{
|
|
if (playerIds[i] == playerId)
|
|
{
|
|
return playerScores[i];
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public void AddScore(int playerId, int increment)
|
|
{
|
|
if (!Networking.IsOwner(gameObject)) return;
|
|
|
|
for (int i = 0; i < playerIds.Length; i++)
|
|
{
|
|
if (playerIds[i] == playerId)
|
|
{
|
|
playerScores[i] += increment;
|
|
break;
|
|
}
|
|
}
|
|
RequestSerialization();
|
|
}
|
|
|
|
public VRCPlayerApi[] GetCurrentPlayers()
|
|
{
|
|
VRCPlayerApi[] players = new VRCPlayerApi[currentPlayerIds.Length];
|
|
for (int i = 0; i < currentPlayerIds.Length; i++)
|
|
{
|
|
players[i] = VRCPlayerApi.GetPlayerById(currentPlayerIds[i]);
|
|
}
|
|
return players;
|
|
}
|
|
|
|
// Team Management Methods
|
|
public void AssignTeam(int playerId, string team)
|
|
{
|
|
if (!Networking.IsOwner(gameObject)) return;
|
|
|
|
if (team != "red" && team != "blue" && team != "unassigned") return;
|
|
|
|
int index = GetPlayerIndex(playerId);
|
|
if (index >= 0)
|
|
{
|
|
playerTeams[index] = team;
|
|
RequestSerialization();
|
|
}
|
|
}
|
|
|
|
public string GetTeam(int playerId)
|
|
{
|
|
int index = GetPlayerIndex(playerId);
|
|
return index >= 0 ? playerTeams[index] : "unassigned";
|
|
}
|
|
|
|
public int[] GetTeamMembers(string team)
|
|
{
|
|
int count = 0;
|
|
for (int i = 0; i < playerTeams.Length; i++)
|
|
{
|
|
if (playerTeams[i] == team) count++;
|
|
}
|
|
|
|
int[] members = new int[count];
|
|
int j = 0;
|
|
for (int i = 0; i < playerTeams.Length; i++)
|
|
{
|
|
if (playerTeams[i] == team)
|
|
{
|
|
members[j++] = playerIds[i];
|
|
}
|
|
}
|
|
return members;
|
|
}
|
|
|
|
// Helper Methods
|
|
private int GetPlayerIndex(int playerId)
|
|
{
|
|
for (int i = 0; i < playerIds.Length; i++)
|
|
{
|
|
if (playerIds[i] == playerId) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
} |