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.
88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Multiplayer.Samples.BossRoom
|
|
{
|
|
public class PlatformManager : MonoBehaviour
|
|
{
|
|
public static PlatformManager Instance = null;
|
|
private List<Platform> m_Platforms;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
} else {
|
|
Instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
m_Platforms = GetComponentsInChildren<Platform>().ToList();
|
|
// Assign unique IDs to each platform
|
|
for (int i = 0; i < m_Platforms.Count; i++)
|
|
{
|
|
m_Platforms[i].AssignID(i + 1); // IDs start from 1
|
|
}
|
|
|
|
Debug.Log("All platforms have been assigned unique IDs.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds an unoccupied platform and assigns the player to it.
|
|
/// </summary>
|
|
/// <param name="player">The ServerCharacter to assign.</param>
|
|
/// <returns>True if successfully assigned, false otherwise.</returns>
|
|
public bool AssignPlayerToPlatform(ServerCharacter player)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds the platform currently occupied by the given player.
|
|
/// </summary>
|
|
/// <param name="player">The ServerCharacter to search for.</param>
|
|
/// <returns>The platform occupied by the player, or null if not found.</returns>
|
|
public Platform GetPlatformOccupiedByPlayer(ServerCharacter player)
|
|
{
|
|
return m_Platforms.FirstOrDefault(p => p.Occupier == player);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if a specific platform is occupied.
|
|
/// </summary>
|
|
/// <param name="platform">The platform to check.</param>
|
|
/// <returns>True if the platform is occupied, false otherwise.</returns>
|
|
public bool IsPlatformOccupied(Platform platform)
|
|
{
|
|
return platform.IsOccupied;
|
|
}
|
|
|
|
public Vector3 GetPlatformPosition(int platformId)
|
|
{
|
|
var platform = m_Platforms.FirstOrDefault(p => p.PlatformID == platformId);
|
|
return platform ? platform.transform.position : Vector3.zero;
|
|
}
|
|
|
|
public Platform GetPlatformById(int platformId)
|
|
{
|
|
return m_Platforms.FirstOrDefault(p => p.PlatformID == platformId);
|
|
}
|
|
|
|
|
|
}
|
|
}
|