diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs index a90dbbb..3761f58 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs @@ -142,8 +142,37 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character NetworkAvatarGuidState m_State; public ulong? PendingSwapRequest { get; set; } + public int? TargetPlatformId { get; private set; } = null; + public void SetTargetPlatform(int platformId) + { + TargetPlatformId = platformId; + } + + public void ClearTargetPlatform() + { + TargetPlatformId = null; + } + private void OnTriggerEnter(Collider other) + { + if (TargetPlatformId.HasValue && other.TryGetComponent(out var platform)) + { + if (platform.PlatformID == TargetPlatformId.Value) + { + if (!platform.IsOccupied) + { + // platform.Occupy(this); + Debug.Log($"{name} successfully occupied Platform {platform.PlatformID}."); + } + else + { + Debug.Log($"{name} couldn't occupy Platform {platform.PlatformID}. Becoming a crow!"); + } + ClearTargetPlatform(); + } + } + } void Awake() { @@ -194,14 +223,10 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(initiatingPlayerId, out var initiatingPlayerObj) && initiatingPlayerObj.TryGetComponent(out ServerCharacter initiatingPlayer)) { - Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position; - Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position; + // Call InitiateSwap directly + InitiateSwap(initiatingPlayer, this); - // Execute the swap - initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition); - this.ServerSendCharacterInputRpc(initiatingPlayerPosition); - - Debug.Log($"Swap confirmed: {initiatingPlayer.name} swapped with {this.name}."); + Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping."); } } else @@ -209,10 +234,27 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character Debug.Log($"Swap request denied by {this.name}."); } - // Clear pending request PendingSwapRequest = null; } + public void InitiateSwap(ServerCharacter initiatingPlayer, ServerCharacter targetPlayer) + { + int initiatingPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(initiatingPlayer).PlatformID; + int targetPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(targetPlayer).PlatformID; + + initiatingPlayer.SetTargetPlatform(targetPlatformId); + targetPlayer.SetTargetPlatform(initiatingPlatformId); + + Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position; + Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position; + + // Execute the swap + initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition); + this.ServerSendCharacterInputRpc(initiatingPlayerPosition); + + Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatformId}, {targetPlayer.name} -> Platform {initiatingPlatformId}."); + } + public override void OnNetworkSpawn() { diff --git a/Assets/Scripts/Platform.cs b/Assets/Scripts/Gameplay/Platform.cs similarity index 100% rename from Assets/Scripts/Platform.cs rename to Assets/Scripts/Gameplay/Platform.cs diff --git a/Assets/Scripts/Platform.cs.meta b/Assets/Scripts/Gameplay/Platform.cs.meta similarity index 100% rename from Assets/Scripts/Platform.cs.meta rename to Assets/Scripts/Gameplay/Platform.cs.meta diff --git a/Assets/Scripts/PlatformManager.cs b/Assets/Scripts/Gameplay/PlatformManager.cs similarity index 75% rename from Assets/Scripts/PlatformManager.cs rename to Assets/Scripts/Gameplay/PlatformManager.cs index 0e7d1a5..4f62d0a 100644 --- a/Assets/Scripts/PlatformManager.cs +++ b/Assets/Scripts/Gameplay/PlatformManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Unity.BossRoom.Gameplay.GameplayObjects.Character; @@ -7,8 +8,19 @@ namespace Unity.Multiplayer.Samples.BossRoom { public class PlatformManager : MonoBehaviour { + public static PlatformManager Instance = null; private List m_Platforms; + private void Awake() + { + if (Instance != null && Instance != this) + { + Destroy(gameObject); + } else { + Instance = this; + } + } + private void Start() { m_Platforms = GetComponentsInChildren().ToList(); @@ -58,5 +70,18 @@ namespace Unity.Multiplayer.Samples.BossRoom { return platform.IsOccupied; } + + public Vector3 GetPlatformPosition(int platformId) + { + var platform = m_Platforms.FirstOrDefault(p => p.PlatformID == platformId); + return platform ? platform.transform.position : Vector3.zero; + } + + public Platform GetPlatformById(int platformId) + { + return m_Platforms.FirstOrDefault(p => p.PlatformID == platformId); + } + + } } diff --git a/Assets/Scripts/PlatformManager.cs.meta b/Assets/Scripts/Gameplay/PlatformManager.cs.meta similarity index 100% rename from Assets/Scripts/PlatformManager.cs.meta rename to Assets/Scripts/Gameplay/PlatformManager.cs.meta diff --git a/Assets/Scripts/Gameplay/SwapConfirmationPanel.cs b/Assets/Scripts/Gameplay/SwapConfirmationPanel.cs index deecc40..955693c 100644 --- a/Assets/Scripts/Gameplay/SwapConfirmationPanel.cs +++ b/Assets/Scripts/Gameplay/SwapConfirmationPanel.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using Unity.BossRoom.Gameplay.GameplayObjects.Character; +using Unity.Multiplayer.Samples.BossRoom; using Unity.Netcode; using UnityEngine; using UnityEngine.UI; @@ -38,4 +39,6 @@ public class SwapConfirmationPanel : MonoBehaviour } confirmationPanel.SetActive(false); } + + } diff --git a/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs b/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs index 91e2e95..9877091 100644 --- a/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs +++ b/Assets/Scripts/Gameplay/UserInput/ClientInputSender.cs @@ -529,7 +529,7 @@ namespace Unity.BossRoom.Gameplay.UserInput if (Input.GetMouseButtonDown(0)) // Left-click to request swap { var ray = m_MainCamera.ScreenPointToRay(UnityEngine.Input.mousePosition); - int hits = Physics.RaycastNonAlloc(ray, k_CachedHit, k_MouseInputRaycastDistance, m_PlatformLayerMask); + int hits = Physics.RaycastNonAlloc(ray, k_CachedHit, k_MouseInputRaycastDistance); if (hits > 0) { for (int i = 0; i < hits; i++)