|
|
@ -10,81 +10,175 @@ namespace Unity.Multiplayer.Samples.BossRoom
|
|
|
|
public class PlatformManager : NetworkBehaviour
|
|
|
|
public class PlatformManager : NetworkBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public static PlatformManager Instance;
|
|
|
|
public static PlatformManager Instance;
|
|
|
|
private List<Platform> platforms;
|
|
|
|
public List<Platform> m_Platforms;
|
|
|
|
|
|
|
|
public int numberOfPlayers = 2; // Number of players (n)
|
|
|
|
|
|
|
|
public float planeRadius = 4f; // Radius of the circular plane
|
|
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (Instance != null && Instance != this)
|
|
|
|
if (Instance != null && Instance != this)
|
|
|
|
|
|
|
|
{
|
|
|
|
Destroy(gameObject);
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
Instance = this;
|
|
|
|
Instance = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
platforms = GetComponentsInChildren<Platform>(true).ToList();
|
|
|
|
if (IsServer)
|
|
|
|
if (IsServer) StartCoroutine(ServerSetup());
|
|
|
|
{
|
|
|
|
else StartCoroutine(ClientSetup());
|
|
|
|
m_Platforms = GetComponentsInChildren<Platform>(true).ToList();
|
|
|
|
|
|
|
|
for (int i = 0; i < m_Platforms.Count; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_Platforms[i].AssignID(i + 1); // Initialize platforms with unique IDs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
StartCoroutine(DelayedServerSetup());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
StartCoroutine(DelayedClientSetup()); // Delay to give time to load
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator ServerSetup()
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Coroutine to delay server-side setup.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private IEnumerator DelayedServerSetup()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
yield return new WaitForSeconds(0.5f); // Delay for 0.5 seconds
|
|
|
|
ActivatePlatformsServerRpc();
|
|
|
|
PlatformSelectionServerRpc(); // Call ServerRpc after delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator ClientSetup()
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Coroutine to delay client-side setup.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private IEnumerator DelayedClientSetup()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
platforms = GetComponentsInChildren<Platform>(true).ToList();
|
|
|
|
m_Platforms = GetComponentsInChildren<Platform>(true).ToList(); // Populate client platform list
|
|
|
|
|
|
|
|
Debug.Log($"Client platform list initialized. Platform count: {m_Platforms.Count}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
|
|
private void ActivatePlatformsServerRpc()
|
|
|
|
private void PlatformSelectionServerRpc()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int activeCount = PlayerPrefs.GetInt("NumberOfLobbyPlayers") - 1;
|
|
|
|
int platformsToSpawn = PlayerPrefs.GetInt("NumberOfLobbyPlayers") - 1;
|
|
|
|
if (activeCount <= 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
platforms.ForEach(p => p.gameObject.SetActive(false));
|
|
|
|
m_Platforms.ForEach(platform => platform.gameObject.SetActive(false));
|
|
|
|
for (int i = 0; i < activeCount; i++)
|
|
|
|
Debug.Log("Disabled all platforms on the server.");
|
|
|
|
platforms[i].gameObject.SetActive(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var activeIds = platforms.Where(p => p.gameObject.activeSelf).Select(p => p.PlatformID.Value).ToArray();
|
|
|
|
if (platformsToSpawn > 0)
|
|
|
|
ActivatePlatformsClientRpc(activeIds);
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < platformsToSpawn; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_Platforms[i].gameObject.SetActive(true);
|
|
|
|
|
|
|
|
Debug.Log($"Enabled platform with ID {m_Platforms[i].PlatformID.Value} on server.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Platforms = m_Platforms.Where(platform => platform.gameObject.activeSelf).ToList();
|
|
|
|
|
|
|
|
//float angleIncrement = 360f / platformsToSpawn;
|
|
|
|
|
|
|
|
//for (int i = 0; i < platformsToSpawn; i++)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// m_Platforms[i].transform.position = new Vector3(
|
|
|
|
|
|
|
|
// Mathf.Cos(i * angleIncrement * Mathf.Deg2Rad) * planeRadius,
|
|
|
|
|
|
|
|
// 0f,
|
|
|
|
|
|
|
|
// Mathf.Sin(i * angleIncrement * Mathf.Deg2Rad) * planeRadius
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[] platformIDs = m_Platforms.Select(p => p.PlatformID.Value).ToArray();
|
|
|
|
|
|
|
|
ClientEnablePlatformClientRpc(platformIDs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ClientRpc]
|
|
|
|
[ClientRpc]
|
|
|
|
private void ActivatePlatformsClientRpc(int[] platformIds)
|
|
|
|
private void ClientEnablePlatformClientRpc(int[] platformIDs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
platforms = GetComponentsInChildren<Platform>(true).ToList();
|
|
|
|
m_Platforms = GetComponentsInChildren<Platform>(true).ToList(); // Ensure the platform list is refreshed
|
|
|
|
platforms.ForEach(p => p.gameObject.SetActive(false));
|
|
|
|
Debug.Log($"Client received platform IDs: {string.Join(",", platformIDs)}");
|
|
|
|
|
|
|
|
Debug.Log($"Client platform list count: {m_Platforms.Count}");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (int id in platformIds)
|
|
|
|
foreach (var platform in m_Platforms)
|
|
|
|
platforms.FirstOrDefault(p => p.PlatformID.Value == id)?.gameObject.SetActive(true);
|
|
|
|
{
|
|
|
|
|
|
|
|
platform.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
Debug.Log($"Disabled platform with ID: {platform.PlatformID.Value}");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (int id in platformIDs)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var platformToEnable = m_Platforms.FirstOrDefault(p => p.PlatformID.Value == id);
|
|
|
|
|
|
|
|
if (platformToEnable != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
platformToEnable.gameObject.SetActive(true);
|
|
|
|
|
|
|
|
Debug.Log($"Enabled platform with ID: {id}");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogError($"Platform with ID {id} not found on client.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Platforms = m_Platforms.Where(platform => platform.gameObject.activeSelf).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
public Platform GetNearestAvailablePlatform(Vector3 position)
|
|
|
|
}
|
|
|
|
|
|
|
|
public Platform FindNearestUnoccupiedPlatform(Vector3 position)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return IsServer ? platforms.Where(p => !p.IsOccupied).OrderBy(p => Vector3.Distance(position, p.transform.position)).FirstOrDefault() : null;
|
|
|
|
if (!IsServer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogError("FindNearestUnoccupiedPlatform should only be called on the server.");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool AllPlatformsOccupied() => platforms.All(p => p.IsOccupied);
|
|
|
|
return m_Platforms
|
|
|
|
|
|
|
|
.Where(platform => !platform.IsOccupied)
|
|
|
|
|
|
|
|
.OrderBy(platform => Vector3.Distance(position, platform.transform.position))
|
|
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool AssignPlayerToPlatform(ServerCharacter player)
|
|
|
|
public bool AreAllPlatformsOccupied()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!IsServer) return false;
|
|
|
|
return m_Platforms.All(platform => platform.IsOccupied);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var availablePlatform = platforms.FirstOrDefault(p => !p.IsOccupied);
|
|
|
|
public bool AssignPlayerToPlatform(ServerCharacter player)
|
|
|
|
if (availablePlatform == null) return false;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!IsServer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogError("AssignPlayerToPlatform should only be called on the server.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
availablePlatform.Occupy(player);
|
|
|
|
var platform = m_Platforms.FirstOrDefault(p => !p.IsOccupied);
|
|
|
|
|
|
|
|
if (platform != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
platform.Occupy(player);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Platform GetPlayerPlatform(ServerCharacter player) => platforms.FirstOrDefault(p => p.IsOccupied && p.GetOccupierId() == player.OwnerClientId);
|
|
|
|
Debug.LogWarning($"No unoccupied platforms available for {player.name}.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Platform GetPlatformOccupiedByPlayer(ServerCharacter player)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return m_Platforms.FirstOrDefault(p => p.IsOccupied && p.GetOccupierId() == player.OwnerClientId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector3 GetPlatformPosition(int platformId) => platforms.FirstOrDefault(p => p.PlatformID.Value == platformId)?.transform.position ?? Vector3.zero;
|
|
|
|
public bool IsPlatformOccupied(Platform platform)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return platform.IsOccupied;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Platform GetPlatformById(int platformId) => platforms.FirstOrDefault(p => p.PlatformID.Value == platformId);
|
|
|
|
public Vector3 GetPlatformPosition(int platformId)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var platform = m_Platforms.FirstOrDefault(p => p.PlatformID.Value == platformId);
|
|
|
|
|
|
|
|
return platform ? platform.transform.position : Vector3.zero;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Platform GetPlatformById(int platformId)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return m_Platforms.FirstOrDefault(p => p.PlatformID.Value == platformId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|