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.
74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
6 days ago
|
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace Unity.Multiplayer.Samples.BossRoom
|
||
|
{
|
||
|
[RequireComponent(typeof(Collider))]
|
||
|
public class Platform : MonoBehaviour
|
||
|
{
|
||
|
public bool IsOccupied => Occupier != null;
|
||
|
public ServerCharacter Occupier { get; private set; }
|
||
|
|
||
|
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.
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Marks this platform as occupied by a specific player.
|
||
|
/// </summary>
|
||
|
/// <param name="player">The ServerCharacter occupying the platform.</param>
|
||
|
public void Occupy(ServerCharacter player)
|
||
|
{
|
||
|
if (IsOccupied)
|
||
|
{
|
||
|
Debug.LogWarning($"Platform {name} is already occupied by {Occupier.name}.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Occupier = player;
|
||
|
Debug.Log($"{player.name} has occupied Platform {name}.");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Clears the occupation of this platform.
|
||
|
/// </summary>
|
||
|
public void Vacate()
|
||
|
{
|
||
|
if (!IsOccupied)
|
||
|
{
|
||
|
Debug.LogWarning($"Platform {name} is not currently occupied.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Debug.Log($"{Occupier.name} has vacated Platform {name}.");
|
||
|
Occupier = null;
|
||
|
}
|
||
|
|
||
|
private void OnTriggerEnter(Collider other)
|
||
|
{
|
||
|
if (!IsOccupied && other.TryGetComponent<ServerCharacter>(out var player))
|
||
|
{
|
||
|
Occupy(player);
|
||
|
Debug.Log($"{player.name} has entered Platform {name}.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnTriggerExit(Collider other)
|
||
|
{
|
||
|
if (IsOccupied && other.TryGetComponent<ServerCharacter>(out var player) && player == Occupier)
|
||
|
{
|
||
|
Vacate();
|
||
|
Debug.Log($"{player.name} has exited Platform {name}.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|