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.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/Platform.cs

259 lines
7.3 KiB
C#

using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode;
using UnityEngine;
using DG.Tweening;
namespace Unity.Multiplayer.Samples.BossRoom
{
[RequireComponent(typeof(Collider))]
public class Platform : NetworkBehaviour
{
public NetworkVariable<int> PlatformID = new NetworkVariable<int>(0);
public bool IsOccupied { get; private set; }
private NetworkVariable<ulong> occupierId = new NetworkVariable<ulong>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
private Collider platformCollider;
private Animator animator;
private void Awake()
{
platformCollider = GetComponent<Collider>();
platformCollider.isTrigger = true;
animator = GetComponent<Animator>();
}
private void Start()
{
if (IsServer)
{
Invoke(nameof(EnableCollider), 2); // Delay collider enabling for server
}
}
4 weeks ago
public void AssignID(int id)
{
if (IsServer)
{
PlatformID.Value = id;
}
}
private void EnableCollider()
{
platformCollider.enabled = true;
}
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 (IsServer)
{
animator.speed = 0f;
PauseClientRpc();
}
}
public void Resume()
{
if (IsServer)
{
animator.speed = 1f;
ResumeClientRpc();
}
}
[ClientRpc]
private void PauseClientRpc()
{
animator.speed = 0f;
}
[ClientRpc]
private void ResumeClientRpc()
{
animator.speed = 1f;
}
public void Occupy(ServerCharacter player)
{
if (!IsServer || IsOccupied)
{
return;
}
Pause();
bool giveScore = player.PreviousPlatformId != PlatformID.Value;
int score = (player.TargetPlatformId == PlatformID.Value) ? 10 : 20;
if (giveScore)
{
ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, score);
}
IsOccupied = true;
occupierId.Value = player.OwnerClientId;
player.OnArrivalOnPlatform(PlatformID.Value);
}
public void Vacate(ServerCharacter player)
{
if (!IsServer || !IsOccupied || occupierId.Value != player.OwnerClientId)
{
return;
}
Resume();
IsOccupied = false;
occupierId.Value = 0;
player.OnLeavingPlatform(PlatformID.Value);
}
private void OnTriggerEnter(Collider other)
{
if (IsServer && other.TryGetComponent<ServerCharacter>(out var player) && !IsOccupied)
{
Occupy(player);
}
}
private void OnTriggerExit(Collider other)
{
if (IsServer && other.TryGetComponent<ServerCharacter>(out var player))
{
Vacate(player);
}
}
public ulong GetOccupierId()
{
return occupierId.Value;
}
}
}
//using Unity.BossRoom.Gameplay.GameplayObjects.Character;
//using Unity.Netcode;
//using UnityEngine;
//using DG.Tweening;
//using Unity.Netcode.Components;
//namespace Unity.Multiplayer.Samples.BossRoom
//{
// [RequireComponent(typeof(Collider))]
// public class Platform : NetworkBehaviour
// {
// public NetworkVariable<int> PlatformID = new NetworkVariable<int>(0);
// public bool IsOccupied { get; private set; }
// private NetworkVariable<ulong> occupierId = new NetworkVariable<ulong>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
// private Collider platformCollider;
// private Animator animator;
// private void Awake()
// {
// platformCollider = GetComponent<Collider>();
// if (!platformCollider.isTrigger)
// platformCollider.isTrigger = true;
// animator = GetComponent<Animator>();
// }
// private void Start()
// {
// Invoke(nameof(ColliderEnabler),2);
// }
// public void AssignID(int id)
// {
// if (IsServer) PlatformID.Value = id;
// }
// void ColliderEnabler()
// {
// platformCollider.enabled = true;
// }
// 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)
// {
// animator.speed = 0f;
// PauseServerRpc();
// }
// }
// public void Resume()
// {
// if (IsOwner)
// {
// animator.speed = 1f;
// ResumeServerRpc();
// }
// }
// [ServerRpc]
// private void PauseServerRpc() => PauseClientRpc();
// [ClientRpc]
// private void PauseClientRpc() => animator.speed = 0f;
// [ServerRpc]
// private void ResumeServerRpc() => ResumeClientRpc();
// [ClientRpc]
// private void ResumeClientRpc() => animator.speed = 1f;
// public void Occupy(ServerCharacter player)
// {
// if (!IsServer || IsOccupied) return;
// Pause();
// bool giveScore = player.PreviousPlatformId != PlatformID.Value;
// int score = (player.TargetPlatformId == PlatformID.Value) ? 10 : 20;
// if (giveScore) ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, score);
// IsOccupied = true;
// occupierId.Value = player.OwnerClientId;
// player.OnArrivalOnPlatform(PlatformID.Value);
// }
// public void Vacate(ServerCharacter player)
// {
// if (!IsServer || !IsOccupied || occupierId.Value != player.OwnerClientId) return;
// Resume();
// IsOccupied = false;
// occupierId.Value = 0;
// player.OnLeavingPlatform(PlatformID.Value);
// }
// private void OnTriggerEnter(Collider other)
// {
// if (IsServer && other.TryGetComponent<ServerCharacter>(out var player) && !IsOccupied)
// Occupy(player);
// }
// private void OnTriggerExit(Collider other)
// {
// if (IsServer && other.TryGetComponent<ServerCharacter>(out var player))
// Vacate(player);
// }
// public ulong GetOccupierId() => occupierId.Value;
// }
//}