From 0bd0070054b940fb65bab1ca4b968563b73d01a2 Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Fri, 13 Dec 2024 16:35:24 +0500 Subject: [PATCH] Added Crow functionality. Not server authenticated yet --- Assets/Scenes/BossRoom.unity | 49 +++++- Assets/Scenes/BossRoom/DungeonEntrance.unity | 10 +- Assets/Scenes/Startup.unity | 64 ++++++++ Assets/Scripts/Gameplay/CrowManager.cs | 96 +++++++++++ Assets/Scripts/Gameplay/CrowManager.cs.meta | 11 ++ .../Character/ServerCharacter.cs | 152 ++++++++++++------ Assets/Scripts/Gameplay/Platform.cs | 4 +- Assets/Scripts/Gameplay/PlatformManager.cs | 14 ++ 8 files changed, 347 insertions(+), 53 deletions(-) create mode 100644 Assets/Scripts/Gameplay/CrowManager.cs create mode 100644 Assets/Scripts/Gameplay/CrowManager.cs.meta diff --git a/Assets/Scenes/BossRoom.unity b/Assets/Scenes/BossRoom.unity index a1f1e18..20ec6df 100644 --- a/Assets/Scenes/BossRoom.unity +++ b/Assets/Scenes/BossRoom.unity @@ -1075,6 +1075,50 @@ GameObject: m_CorrespondingSourceObject: {fileID: 5277713031684110771, guid: 523332e67f2466e4f818161850bfb207, type: 3} m_PrefabInstance: {fileID: 222782633} m_PrefabAsset: {fileID: 0} +--- !u!1 &396364066 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 396364068} + - component: {fileID: 396364067} + m_Layer: 0 + m_Name: CrowManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &396364067 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 396364066} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 501460ae984b4e44d991e4b43e36afee, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &396364068 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 396364066} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &451210155 GameObject: m_ObjectHideFlags: 0 @@ -8575,11 +8619,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.y - value: 0.30972984 + value: 0.3097298 objectReference: {fileID: 0} - target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.z - value: -0.1450697 + value: -0.14506969 objectReference: {fileID: 0} - target: {fileID: 1676734516724634599, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalPosition.z @@ -10602,3 +10646,4 @@ SceneRoots: - {fileID: 1716415238} - {fileID: 1690610726} - {fileID: 1486746210} + - {fileID: 396364068} diff --git a/Assets/Scenes/BossRoom/DungeonEntrance.unity b/Assets/Scenes/BossRoom/DungeonEntrance.unity index bc73239..6a5291d 100644 --- a/Assets/Scenes/BossRoom/DungeonEntrance.unity +++ b/Assets/Scenes/BossRoom/DungeonEntrance.unity @@ -143,7 +143,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &240007729 Transform: m_ObjectHideFlags: 0 @@ -474,7 +474,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &849256359 Transform: m_ObjectHideFlags: 0 @@ -732,7 +732,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &997499840 Transform: m_ObjectHideFlags: 0 @@ -1118,7 +1118,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &1463337823 Transform: m_ObjectHideFlags: 0 @@ -1258,7 +1258,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &1620155725 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Startup.unity b/Assets/Scenes/Startup.unity index b774798..9d63d3b 100644 --- a/Assets/Scenes/Startup.unity +++ b/Assets/Scenes/Startup.unity @@ -664,6 +664,70 @@ PrefabInstance: propertyPath: m_Name value: NetworkSimulator objectReference: {fileID: 0} + - target: {fileID: 5634176411869099529, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: m_PresetReference + value: 2982271614721982465 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464] + value: Unity.Multiplayer.Tools.NetworkSimulator.Runtime Unity.Multiplayer.Tools.NetworkSimulator.Runtime.NetworkSimulatorPreset + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465] + value: Unity.Multiplayer.Tools.NetworkSimulator.Runtime Unity.Multiplayer.Tools.NetworkSimulator.Runtime.NetworkSimulatorPreset + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: None + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: None + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982464].k__BackingField + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} + propertyPath: managedReferences[2982271614721982465].k__BackingField + value: 0 + objectReference: {fileID: 0} - target: {fileID: 6220772510846579177, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} propertyPath: m_RootOrder value: 14 diff --git a/Assets/Scripts/Gameplay/CrowManager.cs b/Assets/Scripts/Gameplay/CrowManager.cs new file mode 100644 index 0000000..5855b89 --- /dev/null +++ b/Assets/Scripts/Gameplay/CrowManager.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.Linq; +using Unity.BossRoom.Gameplay.GameplayObjects.Character; +using UnityEngine; + +public class CrowManager : MonoBehaviour +{ + public static CrowManager Instance { get; private set; } + + private List players = new List(); + + private ServerCharacter currentCrow; + + private void Awake() + { + if (Instance != null && Instance != this) + { + Destroy(this); + return; + } + Instance = this; + } + + private void Start() + { + // players = FindObjectsOfType().ToList(); + } + + + private void Update() + { + DetermineCrow(); + } + + private void DetermineCrow() + { + int unoccupiedPlayers = 0; + ServerCharacter potentialCrow = null; + + foreach (var player in players) + { + if (!player.IsOnAPlatform) + { + unoccupiedPlayers++; + potentialCrow = player; + } + } + + // If exactly one player is not on a platform, assign as crow + if (unoccupiedPlayers == 1) + { + AssignCrow(potentialCrow); + } + else if (unoccupiedPlayers == 0 && currentCrow != null) + { + // All players are on platforms, clear crow + ClearCrow(); + } + } + + private void AssignCrow(ServerCharacter player) + { + if (currentCrow == player) return; // Already the crow + + if (currentCrow != null) + { + // Clear old crow + currentCrow.SetAsCrow(false); + } + + currentCrow = player; + currentCrow.SetAsCrow(true); + Debug.Log($"{currentCrow.name} is now the Crow."); + } + + private void ClearCrow() + { + if (currentCrow != null) + { + currentCrow.SetAsCrow(false); + Debug.Log($"{currentCrow.name} is no longer the Crow."); + currentCrow = null; + } + } + + public void AddCharacterToList(ServerCharacter character) + { + players.Add(character); + } + + public void RemoveCharacterFromList(ServerCharacter character) + { + players.Remove(character); + } + +} diff --git a/Assets/Scripts/Gameplay/CrowManager.cs.meta b/Assets/Scripts/Gameplay/CrowManager.cs.meta new file mode 100644 index 0000000..5de5b70 --- /dev/null +++ b/Assets/Scripts/Gameplay/CrowManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 501460ae984b4e44d991e4b43e36afee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs index 3761f58..fb85def 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs @@ -143,36 +143,10 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character public ulong? PendingSwapRequest { get; set; } public int? TargetPlatformId { get; private set; } = null; + public bool IsOnAPlatform { get; private set; } = false; + public bool IsCrow { get; private set; } = false; - 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() { @@ -199,7 +173,95 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character } } + //Hazim + public void SetAsCrow(bool status) + { + IsCrow = status; + } + + public void SetTargetPlatform(int platformId) + { + TargetPlatformId = platformId; + } + + public void ClearTargetPlatform() + { + TargetPlatformId = null; + } + + public void OnArrivalOnPlatform() + { + ClearTargetPlatform(); + SetOnPlatform(true); + } + + public void OnLeavingPlatform() + { + SetOnPlatform(false); + } + + public void SetOnPlatform(bool status) + { + IsOnAPlatform = status; + } + + // private void OnTriggerEnter(Collider other) + // { + // if (TargetPlatformId == null) return; + // + // if (other.TryGetComponent(out Platform platform) && platform.PlatformID == TargetPlatformId) + // { + // if (platform.IsOccupied && platform.Occupier != this) + // { + // Debug.Log($"Platform {platform.PlatformID} was occupied. Now looking for others"); + // // Platform is occupied, handle intervention + // // HandleOccupiedPlatform(platform); + // } + // else + // { + // // Occupy the platform + // Debug.Log($"Platform {platform.PlatformID} is being occupied"); + // platform.Occupy(this); + // ClearTargetPlatform(); + // } + // } + // } + + private void HandleOccupiedPlatform(Platform currentPlatform) + { + var nearestPlatform = PlatformManager.Instance.FindNearestUnoccupiedPlatform(transform.position); + if (nearestPlatform != null) + { + Debug.Log($"Platform {currentPlatform.PlatformID} is occupied. Moving to nearest Platform {nearestPlatform.PlatformID}."); + MoveToPlatform(nearestPlatform); + } + else + { + Debug.Log($"No unoccupied platforms available. Becoming the Crow."); + BecomeCrow(); + } + } + + private void MoveToPlatform(Platform platform) + { + SetTargetPlatform(platform.PlatformID); + var pltpos = PlatformManager.Instance.GetPlatformPosition(platform.PlatformID); + pltpos.y = 0; + Debug.Log($"Platform position: {pltpos}."); + ServerSendCharacterInputRpc(pltpos); + } + + private void BecomeCrow() + { + ClearTargetPlatform(); + Debug.Log($"{name} is now the Crow."); + // Add additional logic for Crow role if needed + } + + + + [Rpc(SendTo.Server, RequireOwnership = false)] public void NotifySwapRequestRpc(ulong initiatingPlayerId) { @@ -223,9 +285,7 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(initiatingPlayerId, out var initiatingPlayerObj) && initiatingPlayerObj.TryGetComponent(out ServerCharacter initiatingPlayer)) { - // Call InitiateSwap directly InitiateSwap(initiatingPlayer, this); - Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping."); } } @@ -237,30 +297,32 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character 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); + var initiatingPlatform = PlatformManager.Instance.GetPlatformOccupiedByPlayer(initiatingPlayer); + var targetPlatform = PlatformManager.Instance.GetPlatformOccupiedByPlayer(targetPlayer); - Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position; - Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position; - - // Execute the swap - initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition); - this.ServerSendCharacterInputRpc(initiatingPlayerPosition); + if (initiatingPlatform == null || targetPlatform == null) + { + Debug.LogError("One or both players are not on a platform!"); + return; + } + // Use the MoveToPlatform function for movement + initiatingPlayer.MoveToPlatform(targetPlatform); + targetPlayer.MoveToPlatform(initiatingPlatform); - Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatformId}, {targetPlayer.name} -> Platform {initiatingPlatformId}."); + Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatform.PlatformID}, {targetPlayer.name} -> Platform {initiatingPlatform.PlatformID}."); } - + + //Hazim public override void OnNetworkSpawn() { if (!IsServer) { enabled = false; } else { + CrowManager.Instance.AddCharacterToList(this); NetLifeState.LifeState.OnValueChanged += OnLifeStateChanged; m_DamageReceiver.DamageReceived += ReceiveHP; m_DamageReceiver.CollisionEntered += CollisionEntered; @@ -282,7 +344,7 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character public override void OnNetworkDespawn() { NetLifeState.LifeState.OnValueChanged -= OnLifeStateChanged; - + CrowManager.Instance.RemoveCharacterFromList(this); if (m_DamageReceiver) { m_DamageReceiver.DamageReceived -= ReceiveHP; diff --git a/Assets/Scripts/Gameplay/Platform.cs b/Assets/Scripts/Gameplay/Platform.cs index 195e6bf..003a3c7 100644 --- a/Assets/Scripts/Gameplay/Platform.cs +++ b/Assets/Scripts/Gameplay/Platform.cs @@ -69,7 +69,8 @@ namespace Unity.Multiplayer.Samples.BossRoom if (!IsOccupied && other.TryGetComponent(out var player)) { Occupy(player); - Debug.Log($"{player.name} has entered Platform {name}."); + player.OnArrivalOnPlatform(); + // Debug.Log($"{player.name} has entered Platform {name}."); } } @@ -78,6 +79,7 @@ namespace Unity.Multiplayer.Samples.BossRoom if (IsOccupied && other.TryGetComponent(out var player) && player == Occupier) { Vacate(); + player.OnLeavingPlatform(); Debug.Log($"{player.name} has exited Platform {name}."); } } diff --git a/Assets/Scripts/Gameplay/PlatformManager.cs b/Assets/Scripts/Gameplay/PlatformManager.cs index 4f62d0a..6245231 100644 --- a/Assets/Scripts/Gameplay/PlatformManager.cs +++ b/Assets/Scripts/Gameplay/PlatformManager.cs @@ -33,6 +33,20 @@ namespace Unity.Multiplayer.Samples.BossRoom Debug.Log("All platforms have been assigned unique IDs."); } + public Platform FindNearestUnoccupiedPlatform(Vector3 position) + { + return m_Platforms + .Where(platform => !platform.IsOccupied) + .OrderBy(platform => Vector3.Distance(position, platform.transform.position)) + .FirstOrDefault(); + } + + public bool AreAllPlatformsOccupied() + { + return m_Platforms.All(platform => platform.IsOccupied); + } + + /// /// Finds an unoccupied platform and assigns the player to it. ///