|
|
|
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
|
|
|
using Unity.Netcode;
|
|
|
|
using UnityEngine;
|
|
|
|
using DG.Tweening;
|
|
|
|
using Unity.Netcode.Components;
|
|
|
|
using log4net.Filter;
|
|
|
|
|
|
|
|
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 Animator animator;
|
|
|
|
private NetworkAnimator networkAnimator;
|
|
|
|
public override void OnNetworkSpawn()
|
|
|
|
{
|
|
|
|
if (!IsOwner) return;
|
|
|
|
|
|
|
|
Debug.Log("OnNetworkSpawn");
|
|
|
|
// Example: Start an idle animation when spawned
|
|
|
|
PlayAnimation("Rotating");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PlayAnimation(string animationTrigger)
|
|
|
|
{
|
|
|
|
if (IsOwner)
|
|
|
|
{
|
|
|
|
// Set the trigger on the local Animator
|
|
|
|
animator.SetTrigger(animationTrigger);
|
|
|
|
|
|
|
|
// Synchronize animation across the network
|
|
|
|
networkAnimator.SetTrigger(animationTrigger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PauseAnimation()
|
|
|
|
{
|
|
|
|
if (IsOwner)
|
|
|
|
{
|
|
|
|
// Pause the animation locally
|
|
|
|
animator.speed = 0f;
|
|
|
|
|
|
|
|
// Inform the server to pause animation for all clients
|
|
|
|
PauseAnimationServerRpc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResumeAnimation()
|
|
|
|
{
|
|
|
|
if (IsOwner)
|
|
|
|
{
|
|
|
|
// Resume the animation locally
|
|
|
|
animator.speed = 1f;
|
|
|
|
|
|
|
|
// Inform the server to resume animation for all clients
|
|
|
|
ResumeAnimationServerRpc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[ServerRpc]
|
|
|
|
private void PauseAnimationServerRpc()
|
|
|
|
{
|
|
|
|
// On the server, pause animation for all clients
|
|
|
|
PauseAnimationClientRpc();
|
|
|
|
}
|
|
|
|
|
|
|
|
[ClientRpc]
|
|
|
|
private void PauseAnimationClientRpc()
|
|
|
|
{
|
|
|
|
animator.speed = 0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
[ServerRpc]
|
|
|
|
private void ResumeAnimationServerRpc()
|
|
|
|
{
|
|
|
|
// On the server, resume animation for all clients
|
|
|
|
ResumeAnimationClientRpc();
|
|
|
|
}
|
|
|
|
|
|
|
|
[ClientRpc]
|
|
|
|
private void ResumeAnimationClientRpc()
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!IsServer)
|
|
|
|
{
|
|
|
|
Debug.LogError("Occupy can only be called on the server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PauseAnimation();
|
|
|
|
if (IsOccupied)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
OccupierId.Value = player.OwnerClientId;
|
|
|
|
player.OnArrivalOnPlatform(PlatformID);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
ResumeAnimation();
|
|
|
|
|
|
|
|
if (!IsOccupied || OccupierId.Value != player.OwnerClientId)
|
|
|
|
{
|
|
|
|
Debug.LogWarning($"Platform {PlatformID} is not occupied by Player {player.OwnerClientId}.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
IsOccupied = false;
|
|
|
|
OccupierId.Value = 0;
|
|
|
|
player.OnLeavingPlatform(PlatformID);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
Vacate(player);
|
|
|
|
if (OccupierId.Value == player.OwnerClientId)
|
|
|
|
player.OnLeavingPlatform(PlatformID);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|