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 PlatformID = new NetworkVariable(0); public bool IsOccupied { get; private set; } private NetworkVariable occupierId = new NetworkVariable(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server); private Collider platformCollider; private Animator animator; private void Awake() { platformCollider = GetComponent(); if (!platformCollider.isTrigger) platformCollider.isTrigger = true; animator = GetComponent(); } public void AssignID(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) { 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(out var player) && !IsOccupied) Occupy(player); } private void OnTriggerExit(Collider other) { if (IsServer && other.TryGetComponent(out var player)) Vacate(player); } public ulong GetOccupierId() => occupierId.Value; } }