Added platform entering logic
parent
908cac3c95
commit
9ea479bf32
@ -0,0 +1,73 @@
|
||||
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}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbadb65558c0b1a48b58b765317ed874
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,55 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.Multiplayer.Samples.BossRoom
|
||||
{
|
||||
public class PlatformManager : MonoBehaviour
|
||||
{
|
||||
private List<Platform> m_Platforms;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_Platforms = GetComponentsInChildren<Platform>().ToList();
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d9e826abe81bf7a4b88597fe93add614
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue