From a15f6d6b341a44b9f544cbe49aee449220ccc561 Mon Sep 17 00:00:00 2001 From: Ali Sharoz Date: Fri, 21 Feb 2025 07:48:51 +0500 Subject: [PATCH] CoolDown timer fixed and WarningPanel UI --- Assets/Scenes/BossRoom.unity | 24 ++++++-- Assets/Scripts/Gameplay/AbilitySystem.cs | 4 ++ Assets/Scripts/Gameplay/AbilityUI.cs | 62 +++++++++++++------- Assets/Scripts/Gameplay/ExecutionerBox.cs | 2 +- Assets/Scripts/Gameplay/WarningPanel.cs | 44 ++++++++++++++ Assets/Scripts/Gameplay/WarningPanel.cs.meta | 11 ++++ 6 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 Assets/Scripts/Gameplay/WarningPanel.cs create mode 100644 Assets/Scripts/Gameplay/WarningPanel.cs.meta diff --git a/Assets/Scenes/BossRoom.unity b/Assets/Scenes/BossRoom.unity index e3f22d9..dc1a6ec 100644 --- a/Assets/Scenes/BossRoom.unity +++ b/Assets/Scenes/BossRoom.unity @@ -4382,13 +4382,14 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 731395403} + - component: {fileID: 731395404} m_Layer: 5 m_Name: WarningPanel m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!224 &731395403 RectTransform: m_ObjectHideFlags: 0 @@ -4409,6 +4410,19 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &731395404 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 731395402} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 725a3a3076d96f54f8ffcb254140c646, type: 3} + m_Name: + m_EditorClassIdentifier: + panel: {fileID: 1586538565} --- !u!1 &732850150 GameObject: m_ObjectHideFlags: 0 @@ -13889,11 +13903,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1676734515771252668, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.y - value: 0.28987604 + value: 0.28987613 objectReference: {fileID: 0} - target: {fileID: 1676734515771252668, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.z - value: -0.1815203 + value: -0.1815204 objectReference: {fileID: 0} - target: {fileID: 1676734516302391364, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_UpdateMethod @@ -17092,12 +17106,12 @@ GameObject: - component: {fileID: 1586538568} - component: {fileID: 1586538567} m_Layer: 5 - m_Name: BG + m_Name: PanelBG m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!224 &1586538566 RectTransform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index b8c9f19..918e4b1 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -414,6 +414,10 @@ public class AbilitySystem : NetworkBehaviour abilitiesOnCooldown.Remove(ability); Debug.Log($"{ability.abilityName} is off cooldown."); + if (abilityUI) + { + abilityUI.CancelCooldownUI(); + } } private void UpdateIndicatorPosition() diff --git a/Assets/Scripts/Gameplay/AbilityUI.cs b/Assets/Scripts/Gameplay/AbilityUI.cs index 63bdd64..a11880f 100644 --- a/Assets/Scripts/Gameplay/AbilityUI.cs +++ b/Assets/Scripts/Gameplay/AbilityUI.cs @@ -1,46 +1,66 @@ using System.Collections; -using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Unity.Multiplayer.Samples.BossRoom { - public class AbilityUI : MonoBehaviour { public string key; public Image CoolDownImg; public GameObject CoolDownImgParent; - public IEnumerator StopWatchRoutine; + + private Coroutine stopWatchRoutine; + private bool isCooldownActive = false; + public void StopWatchFiller(float coolDownTime) { - if (StopWatchRoutine == null) - { - StopWatchRoutine = StopWatchFillerRoutine(coolDownTime); - StartCoroutine(StopWatchRoutine); - } - else + if (stopWatchRoutine != null) { - StopCoroutine(StopWatchRoutine); - StopWatchRoutine = StopWatchFillerRoutine(coolDownTime); - StartCoroutine(StopWatchRoutine); + StopCoroutine(stopWatchRoutine); } + + stopWatchRoutine = StartCoroutine(StopWatchFillerRoutine(coolDownTime)); } - public IEnumerator StopWatchFillerRoutine(float coolDownTime) + + private IEnumerator StopWatchFillerRoutine(float coolDownTime) { - float factor = coolDownTime; + isCooldownActive = true; CoolDownImgParent.SetActive(true); - // CoolDownImg.gameObject.SetActive(true); CoolDownImg.fillAmount = 1; - while (coolDownTime > 0) + + float elapsedTime = 0; + while (elapsedTime < coolDownTime) + { + // Check if cooldown is still active before updating UI + if (!isCooldownActive) + { + CoolDownImgParent.SetActive(false); + yield break; + } + + elapsedTime += Time.deltaTime; + CoolDownImg.fillAmount = 1 - (elapsedTime / coolDownTime); + yield return null; + } + + CoolDownImgParent.SetActive(false); + isCooldownActive = false; + stopWatchRoutine = null; + } + + /// + /// Cancels cooldown UI immediately if the ability is available before the countdown ends. + /// + public void CancelCooldownUI() + { + if (stopWatchRoutine != null) { - coolDownTime -= Time.deltaTime; - CoolDownImg.fillAmount = coolDownTime / factor; - yield return new WaitForSeconds(Time.deltaTime); + StopCoroutine(stopWatchRoutine); + stopWatchRoutine = null; } CoolDownImgParent.SetActive(false); - // CoolDownImg.gameObject.SetActive(false); - StopWatchRoutine = null; + isCooldownActive = false; } } } diff --git a/Assets/Scripts/Gameplay/ExecutionerBox.cs b/Assets/Scripts/Gameplay/ExecutionerBox.cs index c170dda..c0ce622 100644 --- a/Assets/Scripts/Gameplay/ExecutionerBox.cs +++ b/Assets/Scripts/Gameplay/ExecutionerBox.cs @@ -48,7 +48,7 @@ public class ExecutionerBox : NetworkBehaviour { // Show warning UI on all clients // UIManager.Instance.ShowExecutionerWarning(_warningTime); - + WarningPanel.Instance.ShowPanelForTime(_warningTime); // Wait for the warning time before moving yield return new WaitForSeconds(_warningTime); diff --git a/Assets/Scripts/Gameplay/WarningPanel.cs b/Assets/Scripts/Gameplay/WarningPanel.cs new file mode 100644 index 0000000..a458f9d --- /dev/null +++ b/Assets/Scripts/Gameplay/WarningPanel.cs @@ -0,0 +1,44 @@ +using Unity.Netcode; +using UnityEngine; +using System.Collections; + + +public class WarningPanel : NetworkBehaviour +{ + public static WarningPanel Instance { get; private set; } + + [SerializeField] private GameObject panel; + + private void Awake() + { + if (Instance == null) + { + Instance = this; + } + else + { + Destroy(gameObject); + } + } + + public void ShowPanelForTime(float seconds) + { + if (IsServer) // Only the server should trigger this + { + ShowPanelClientRpc(seconds); + } + } + + [ClientRpc] + private void ShowPanelClientRpc(float seconds) + { + panel.SetActive(true); + StartCoroutine(HidePanelAfterTime(seconds)); + } + + private IEnumerator HidePanelAfterTime(float seconds) + { + yield return new WaitForSeconds(seconds); + panel.SetActive(false); + } +} diff --git a/Assets/Scripts/Gameplay/WarningPanel.cs.meta b/Assets/Scripts/Gameplay/WarningPanel.cs.meta new file mode 100644 index 0000000..28dc03a --- /dev/null +++ b/Assets/Scripts/Gameplay/WarningPanel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 725a3a3076d96f54f8ffcb254140c646 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: