You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/PlatformManager.cs

347 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode;
using UnityEngine;
namespace Unity.Multiplayer.Samples.BossRoom
{
public class PlatformManager : NetworkBehaviour
{
public static PlatformManager Instance;
4 weeks ago
public List<Platform> m_Platforms;
public int numberOfPlayers = 2; // Number of players (n)
private void Awake()
{
if (Instance != null && Instance != this)
4 weeks ago
{
Destroy(gameObject);
4 weeks ago
}
else
4 weeks ago
{
Instance = this;
4 weeks ago
}
}
private void Start()
{
4 weeks ago
if (IsServer)
{
InitializePlatforms();
4 weeks ago
StartCoroutine(DelayedServerSetup());
}
1 month ago
}
4 weeks ago
/// <summary>
/// Initializes platform list and assigns unique IDs.
4 weeks ago
/// </summary>
private void InitializePlatforms()
{
m_Platforms = GetComponentsInChildren<Platform>(true).ToList();
for (int i = 0; i < m_Platforms.Count; i++)
{
m_Platforms[i].AssignID(i + 1); // Assign unique IDs to platforms
}
}
4 weeks ago
/// <summary>
/// Delayed server setup to allow other components to initialize.
4 weeks ago
/// </summary>
private IEnumerator DelayedServerSetup()
{
yield return new WaitForSeconds(0.5f);
PlatformSelection();
}
/// <summary>
/// Server-side logic to select and enable platforms based on player count.
/// </summary>
private void PlatformSelection()
{
4 weeks ago
int platformsToSpawn = PlayerPrefs.GetInt("NumberOfLobbyPlayers",1) - 1;
4 weeks ago
// Disable all platforms initially
foreach (var platform in m_Platforms)
4 weeks ago
{
platform.gameObject.SetActive(false);
}
// Enable required number of platforms
for (int i = 0; i < platformsToSpawn; i++)
{
Debug.Log(m_Platforms[i].gameObject.name + " is activated");
m_Platforms[i].gameObject.SetActive(true);
4 weeks ago
}
m_Platforms = m_Platforms.Where(platform => platform.gameObject.activeSelf).ToList();
4 weeks ago
int[] platformIDs = m_Platforms.Select(p => p.PlatformID.Value).ToArray();
// Notify clients to enable corresponding platforms
4 weeks ago
ClientEnablePlatformClientRpc(platformIDs);
}
/// <summary>
/// ClientRpc to enable platforms on the client side.
/// </summary>
[ClientRpc]
4 weeks ago
private void ClientEnablePlatformClientRpc(int[] platformIDs)
{
m_Platforms = GetComponentsInChildren<Platform>(true).ToList();
4 weeks ago
// Disable all platforms initially
4 weeks ago
foreach (var platform in m_Platforms)
{
platform.gameObject.SetActive(false);
}
// Enable specified platforms
4 weeks ago
foreach (int id in platformIDs)
{
var platformToEnable = m_Platforms.FirstOrDefault(p => p.PlatformID.Value == id);
if (platformToEnable != null)
{
platformToEnable.gameObject.SetActive(true);
}
}
m_Platforms = m_Platforms.Where(platform => platform.gameObject.activeSelf).ToList();
}
/// <summary>
/// Finds the nearest unoccupied platform to a given position.
/// Server-only method.
/// </summary>
4 weeks ago
public Platform FindNearestUnoccupiedPlatform(Vector3 position)
{
4 weeks ago
if (!IsServer)
{
Debug.LogError("FindNearestUnoccupiedPlatform should only be called on the server.");
return null;
}
return m_Platforms
.Where(platform => !platform.IsOccupied)
.OrderBy(platform => Vector3.Distance(position, platform.transform.position))
.FirstOrDefault();
}
/// <summary>
/// Checks if all platforms are occupied.
/// </summary>
4 weeks ago
public bool AreAllPlatformsOccupied()
{
return m_Platforms.All(platform => platform.IsOccupied);
}
/// <summary>
/// Gets the platform occupied by a specific player.
/// </summary>
4 weeks ago
public Platform GetPlatformOccupiedByPlayer(ServerCharacter player)
{
return m_Platforms.FirstOrDefault(p => p.IsOccupied && p.GetOccupierId() == player.OwnerClientId);
}
/// <summary>
/// Checks if a platform is occupied.
/// </summary>
4 weeks ago
public bool IsPlatformOccupied(Platform platform)
{
return platform.IsOccupied;
}
/// <summary>
/// Gets the position of a platform by its ID.
/// </summary>
4 weeks ago
public Vector3 GetPlatformPosition(int platformId)
{
var platform = m_Platforms.FirstOrDefault(p => p.PlatformID.Value == platformId);
return platform ? platform.transform.position : Vector3.zero;
}
/// <summary>
/// Gets a platform by its ID.
/// </summary>
4 weeks ago
public Platform GetPlatformById(int platformId)
{
return m_Platforms.FirstOrDefault(p => p.PlatformID.Value == platformId);
}
}
}
//using System.Collections;
//using System.Collections.Generic;
//using System.Linq;
//using Unity.BossRoom.Gameplay.GameplayObjects.Character;
//using Unity.Netcode;
//using UnityEngine;
//namespace Unity.Multiplayer.Samples.BossRoom
//{
// public class PlatformManager : NetworkBehaviour
// {
// public static PlatformManager Instance;
// 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()
// {
// if (Instance != null && Instance != this)
// {
// Destroy(gameObject);
// }
// else
// {
// Instance = this;
// }
// }
// private void Start()
// {
// if (IsServer)
// {
// 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
// }
// }
// /// <summary>
// /// Coroutine to delay server-side setup.
// /// </summary>
// private IEnumerator DelayedServerSetup()
// {
// yield return new WaitForSeconds(0.5f); // Delay for 0.5 seconds
// PlatformSelection(); // Call ServerRpc after delay
// }
// /// <summary>
// /// Coroutine to delay client-side setup.
// /// </summary>
// private IEnumerator DelayedClientSetup()
// {
// yield return new WaitForSeconds(0.5f);
// m_Platforms = GetComponentsInChildren<Platform>(true).ToList(); // Populate client platform list
// Debug.Log($"Client platform list initialized. Platform count: {m_Platforms.Count}");
// }
// private void PlatformSelection()
// {
// int platformsToSpawn = PlayerPrefs.GetInt("NumberOfLobbyPlayers") - 1;
// m_Platforms.ForEach(platform => platform.gameObject.SetActive(false));
// Debug.Log("Disabled all platforms on the server.");
// if (platformsToSpawn > 0)
// {
// 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();
// int[] platformIDs = m_Platforms.Select(p => p.PlatformID.Value).ToArray();
// ClientEnablePlatformClientRpc(platformIDs);
// }
// [ClientRpc]
// private void ClientEnablePlatformClientRpc(int[] platformIDs)
// {
// m_Platforms = GetComponentsInChildren<Platform>(true).ToList(); // Ensure the platform list is refreshed
// Debug.Log($"Client received platform IDs: {string.Join(",", platformIDs)}");
// Debug.Log($"Client platform list count: {m_Platforms.Count}");
// foreach (var platform in m_Platforms)
// {
// 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 FindNearestUnoccupiedPlatform(Vector3 position)
// {
// if (!IsServer)
// {
// Debug.LogError("FindNearestUnoccupiedPlatform should only be called on the server.");
// return null;
// }
// return m_Platforms
// .Where(platform => !platform.IsOccupied)
// .OrderBy(platform => Vector3.Distance(position, platform.transform.position))
// .FirstOrDefault();
// }
// public bool AreAllPlatformsOccupied()
// {
// return m_Platforms.All(platform => platform.IsOccupied);
// }
// /*public bool AssignPlayerToPlatform(ServerCharacter player)
// {
// if (!IsServer)
// {
// Debug.LogError("AssignPlayerToPlatform should only be called on the server.");
// return false;
// }
// var platform = m_Platforms.FirstOrDefault(p => !p.IsOccupied);
// if (platform != null)
// {
// platform.Occupy(player);
// return true;
// }
// 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 bool IsPlatformOccupied(Platform platform)
// {
// return platform.IsOccupied;
// }
// 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);
// }
// }
//}