|
|
@ -9,184 +9,97 @@ namespace Unity.Multiplayer.Samples.BossRoom
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
|
|
public class Platform : NetworkBehaviour
|
|
|
|
public class Platform : NetworkBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public int PlatformID { get; private set; }
|
|
|
|
public NetworkVariable<int> PlatformID = new NetworkVariable<int>(0);
|
|
|
|
public bool IsOccupied { get; private set; }
|
|
|
|
public bool IsOccupied { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private NetworkVariable<ulong> OccupierId = new NetworkVariable<ulong>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
|
|
|
|
private NetworkVariable<ulong> occupierId = new NetworkVariable<ulong>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
|
|
|
|
private Collider m_PlatformCollider;
|
|
|
|
private Collider platformCollider;
|
|
|
|
private Animator animator;
|
|
|
|
private Animator animator;
|
|
|
|
private NetworkAnimator networkAnimator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void PauseAnimation()
|
|
|
|
private void Awake()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
platformCollider = GetComponent<Collider>();
|
|
|
|
|
|
|
|
if (!platformCollider.isTrigger)
|
|
|
|
|
|
|
|
platformCollider.isTrigger = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
animator = GetComponent<Animator>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (IsServer) PlatformID.Value = id;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void StartRotation() => transform.DOLocalRotate(Vector3.up, 120).SetSpeedBased(true).SetId(PlatformID).SetLoops(-1, LoopType.Incremental);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void PauseRotation() => DOTween.Pause(PlatformID);
|
|
|
|
|
|
|
|
private void ResumeRotation() => DOTween.Play(PlatformID);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (IsOwner)
|
|
|
|
if (IsOwner)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Pause the animation locally
|
|
|
|
|
|
|
|
animator.speed = 0f;
|
|
|
|
animator.speed = 0f;
|
|
|
|
|
|
|
|
PauseServerRpc();
|
|
|
|
// Inform the server to pause animation for all clients
|
|
|
|
|
|
|
|
PauseAnimationServerRpc();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ResumeAnimation()
|
|
|
|
public void Resume()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (IsOwner)
|
|
|
|
if (IsOwner)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Resume the animation locally
|
|
|
|
|
|
|
|
animator.speed = 1f;
|
|
|
|
animator.speed = 1f;
|
|
|
|
|
|
|
|
ResumeServerRpc();
|
|
|
|
// Inform the server to resume animation for all clients
|
|
|
|
|
|
|
|
ResumeAnimationServerRpc();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ServerRpc]
|
|
|
|
[ServerRpc]
|
|
|
|
private void PauseAnimationServerRpc()
|
|
|
|
private void PauseServerRpc() => PauseClientRpc();
|
|
|
|
{
|
|
|
|
|
|
|
|
// On the server, pause animation for all clients
|
|
|
|
|
|
|
|
PauseAnimationClientRpc();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ClientRpc]
|
|
|
|
[ClientRpc]
|
|
|
|
private void PauseAnimationClientRpc()
|
|
|
|
private void PauseClientRpc() => animator.speed = 0f;
|
|
|
|
{
|
|
|
|
|
|
|
|
animator.speed = 0f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ServerRpc]
|
|
|
|
[ServerRpc]
|
|
|
|
private void ResumeAnimationServerRpc()
|
|
|
|
private void ResumeServerRpc() => ResumeClientRpc();
|
|
|
|
{
|
|
|
|
|
|
|
|
// On the server, resume animation for all clients
|
|
|
|
|
|
|
|
ResumeAnimationClientRpc();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ClientRpc]
|
|
|
|
[ClientRpc]
|
|
|
|
private void ResumeAnimationClientRpc()
|
|
|
|
private void ResumeClientRpc() => animator.speed = 1f;
|
|
|
|
{
|
|
|
|
|
|
|
|
animator.speed = 1f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
animator = GetComponent<Animator>();
|
|
|
|
|
|
|
|
networkAnimator = GetComponent<NetworkAnimator>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void AssignID(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PlatformID = id;
|
|
|
|
|
|
|
|
IsOccupied = false;
|
|
|
|
|
|
|
|
Debug.Log($"Platform {name} assigned ID: {PlatformID}");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AnimationStarter()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
transform.DOLocalRotate(Vector3.up, 120).SetSpeedBased(true).SetId(PlatformID).SetLoops(-1, LoopType.Incremental);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationResumer()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.Log("Play PlatformID: " + PlatformID);
|
|
|
|
|
|
|
|
DOTween.Play(PlatformID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationPauser()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.Log("Pause PlatformID: " + PlatformID);
|
|
|
|
|
|
|
|
DOTween.Pause(PlatformID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Occupy(ServerCharacter player)
|
|
|
|
public void Occupy(ServerCharacter player)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!IsServer)
|
|
|
|
if (!IsServer || IsOccupied) return;
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogError("Occupy can only be called on the server.");
|
|
|
|
Pause();
|
|
|
|
return;
|
|
|
|
bool giveScore = player.PreviousPlatformId != PlatformID.Value;
|
|
|
|
}
|
|
|
|
int score = (player.TargetPlatformId == PlatformID.Value) ? 10 : 20;
|
|
|
|
PauseAnimation();
|
|
|
|
|
|
|
|
if (IsOccupied)
|
|
|
|
if (giveScore) ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, score);
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogWarning($"Platform {PlatformID} is already occupied.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool giveScore = player.PreviousPlatformId != PlatformID;
|
|
|
|
|
|
|
|
// Check if the player who occupied this platform had this platform as their target platform
|
|
|
|
|
|
|
|
if (giveScore)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (player.TargetPlatformId.HasValue && player.TargetPlatformId.Value == PlatformID)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Successful swap
|
|
|
|
|
|
|
|
ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, 10);
|
|
|
|
|
|
|
|
Debug.Log($"Player {player.OwnerClientId} successfully swapped to Platform {PlatformID}. Awarded 10 score.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, 20);
|
|
|
|
|
|
|
|
Debug.Log($"Crow Player {player.OwnerClientId} successfully stole Platform {PlatformID}. Awarded 20 score.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IsOccupied = true;
|
|
|
|
IsOccupied = true;
|
|
|
|
OccupierId.Value = player.OwnerClientId;
|
|
|
|
occupierId.Value = player.OwnerClientId;
|
|
|
|
player.OnArrivalOnPlatform(PlatformID);
|
|
|
|
player.OnArrivalOnPlatform(PlatformID.Value);
|
|
|
|
Debug.Log($"Platform {PlatformID} is now occupied by Player {player.OwnerClientId}.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Vacate(ServerCharacter player)
|
|
|
|
public void Vacate(ServerCharacter player)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!IsServer)
|
|
|
|
if (!IsServer || !IsOccupied || occupierId.Value != player.OwnerClientId) return;
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogError("Vacate can only be called on the server.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ResumeAnimation();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsOccupied || OccupierId.Value != player.OwnerClientId)
|
|
|
|
Resume();
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.LogWarning($"Platform {PlatformID} is not occupied by Player {player.OwnerClientId}.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IsOccupied = false;
|
|
|
|
IsOccupied = false;
|
|
|
|
OccupierId.Value = 0;
|
|
|
|
occupierId.Value = 0;
|
|
|
|
player.OnLeavingPlatform(PlatformID);
|
|
|
|
player.OnLeavingPlatform(PlatformID.Value);
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"Platform {PlatformID} is now vacated.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!IsServer) return;
|
|
|
|
if (IsServer && other.TryGetComponent<ServerCharacter>(out var player) && !IsOccupied)
|
|
|
|
|
|
|
|
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!IsOccupied)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Occupy(player);
|
|
|
|
Occupy(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!IsServer) return;
|
|
|
|
if (IsServer && other.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Vacate(player);
|
|
|
|
Vacate(player);
|
|
|
|
if (OccupierId.Value == player.OwnerClientId)
|
|
|
|
|
|
|
|
player.OnLeavingPlatform(PlatformID);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ulong GetOccupierId() => occupierId.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|