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.
110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Multiplayer.Samples.BossRoom
|
|
{
|
|
[RequireComponent(typeof(Collider))]
|
|
public class Platform : NetworkBehaviour
|
|
{
|
|
public int PlatformID { get; private set; }
|
|
public bool IsOccupied{ get; private set; }
|
|
|
|
private NetworkVariable<ulong> OccupierId = new NetworkVariable<ulong>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
|
|
private Collider m_PlatformCollider;
|
|
|
|
private void Awake()
|
|
{
|
|
m_PlatformCollider = GetComponent<Collider>();
|
|
if (!m_PlatformCollider.isTrigger)
|
|
{
|
|
Debug.LogWarning($"Platform {name} collider must be set as a trigger.");
|
|
m_PlatformCollider.isTrigger = true; // Ensure it's a trigger.
|
|
}
|
|
}
|
|
|
|
public void AssignID(int id)
|
|
{
|
|
PlatformID = id;
|
|
IsOccupied = false;
|
|
Debug.Log($"Platform {name} assigned ID: {PlatformID}");
|
|
}
|
|
|
|
public void Occupy(ServerCharacter player)
|
|
{
|
|
if (!IsServer)
|
|
{
|
|
Debug.LogError("Occupy can only be called on the server.");
|
|
return;
|
|
}
|
|
|
|
if (IsOccupied)
|
|
{
|
|
Debug.LogWarning($"Platform {PlatformID} is already occupied.");
|
|
return;
|
|
}
|
|
|
|
IsOccupied = true;
|
|
OccupierId.Value = player.OwnerClientId;
|
|
player.SetOnPlatform(true);
|
|
|
|
Debug.Log($"Platform {PlatformID} is now occupied by Player {player.OwnerClientId}.");
|
|
}
|
|
|
|
public void Vacate(ServerCharacter player)
|
|
{
|
|
if (!IsServer)
|
|
{
|
|
Debug.LogError("Vacate can only be called on the server.");
|
|
return;
|
|
}
|
|
|
|
if (!IsOccupied || OccupierId.Value != player.OwnerClientId)
|
|
{
|
|
Debug.LogWarning($"Platform {PlatformID} is not occupied by Player {player.OwnerClientId}.");
|
|
return;
|
|
}
|
|
|
|
IsOccupied = false;
|
|
OccupierId.Value = 0;
|
|
player.SetOnPlatform(false);
|
|
|
|
Debug.Log($"Platform {PlatformID} is now vacated.");
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (!IsServer) return;
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player))
|
|
{
|
|
if (!IsOccupied)
|
|
{
|
|
Occupy(player);
|
|
player.OnArrivalOnPlatform();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"Player {player.OwnerClientId} attempted to occupy an already occupied Platform {PlatformID}.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public ulong GetOccupierId()
|
|
{
|
|
return OccupierId.Value;
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (!IsServer) return;
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player) && OccupierId.Value == player.OwnerClientId)
|
|
{
|
|
Vacate(player);
|
|
player.OnLeavingPlatform();
|
|
}
|
|
}
|
|
}
|
|
}
|