From 281bb6e87c288f91e92b8147f97866806219e09a Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Wed, 18 Dec 2024 18:45:05 +0500 Subject: [PATCH] Added slowdown --- .../Abilities/SlowDownZonePrefab.prefab | 2 +- Assets/Scripts/Gameplay/Ability.cs | 26 +---------- Assets/Scripts/Gameplay/AbilitySystem.cs | 43 +++++++++++++------ .../Character/ServerCharacterMovement.cs | 25 ++++++++++- Assets/Scripts/Gameplay/SlowZonePrefab.cs | 10 ++--- 5 files changed, 62 insertions(+), 44 deletions(-) diff --git a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab index 86830c5..0f1ed40 100644 --- a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab +++ b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab @@ -97,7 +97,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Name: m_EditorClassIdentifier: - GlobalObjectIdHash: 4101054131 + GlobalObjectIdHash: 725408325 InScenePlacedSourceGlobalObjectIdHash: 0 AlwaysReplicateAsRoot: 0 SynchronizeTransform: 1 diff --git a/Assets/Scripts/Gameplay/Ability.cs b/Assets/Scripts/Gameplay/Ability.cs index f025550..d104a27 100644 --- a/Assets/Scripts/Gameplay/Ability.cs +++ b/Assets/Scripts/Gameplay/Ability.cs @@ -8,31 +8,9 @@ public abstract class Ability : ScriptableObject public KeyCode keybind; // Key to activate the ability public float cooldownTime; // Cooldown duration in seconds - private float lastActivationTime; - - /// - /// Checks if the ability can be activated (e.g., not on cooldown). - /// - public bool CanActivate() - { - return Time.time >= lastActivationTime + cooldownTime; - } - - /// - /// Invokes the ability. - /// - /// The GameObject or player triggering this ability. - public void TryActivate(GameObject owner) + public void ActivateAbility(GameObject owner) { - if (CanActivate()) - { - Activate(owner); - lastActivationTime = Time.time; - } - else - { - Debug.Log($"{abilityName} is on cooldown."); - } + Activate(owner); } protected abstract void Activate(GameObject owner); // Logic for the specific ability diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index 08f12f4..9368df1 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -1,5 +1,5 @@ +using System.Collections; using System.Collections.Generic; -using Unity.Netcode; using UnityEngine; public class AbilitySystem : MonoBehaviour @@ -10,37 +10,39 @@ public class AbilitySystem : MonoBehaviour private Ability activeAbility; // Tracks the currently active ability in "spawn mode" private bool isAbilityActive = false; + private HashSet abilitiesOnCooldown = new HashSet(); + void Update() { - // If ability mode is active, listen for mouse input to activate ability if (isAbilityActive && Input.GetMouseButtonDown(0)) { UseActiveAbility(); - ToggleAbilityMode(activeAbility); } } /// - /// Activates ability by key (called externally). + /// Activates an ability by its keybind. /// - /// The key assigned to an ability. public void ActivateAbilityByKey(string key) { foreach (var ability in abilities) { if (ability.abilityKey == key) { - ToggleAbilityMode(ability); + if (!abilitiesOnCooldown.Contains(ability)) + { + ToggleAbilityMode(ability); + } + else + { + Debug.Log($"{ability.abilityName} is on cooldown."); + } return; } } Debug.LogWarning($"No ability assigned to key {key}."); } - /// - /// Toggles the ability activation mode. - /// - /// The ability to toggle. private void ToggleAbilityMode(Ability ability) { if (isAbilityActive && activeAbility == ability) @@ -57,7 +59,7 @@ public class AbilitySystem : MonoBehaviour { isAbilityActive = true; activeAbility = ability; - Debug.Log($"Ability {ability.name} activated! Click to use."); + Debug.Log($"Ability {ability.abilityName} activated! Click to use."); } private void DeactivateAbilityMode() @@ -71,8 +73,23 @@ public class AbilitySystem : MonoBehaviour { if (activeAbility != null) { - activeAbility.TryActivate(gameObject); - DeactivateAbilityMode(); // Ability used, deactivate mode + activeAbility.ActivateAbility(gameObject); + StartCoroutine(StartCooldown(activeAbility)); + DeactivateAbilityMode(); } } + + /// + /// Starts the cooldown coroutine for the ability. + /// + private IEnumerator StartCooldown(Ability ability) + { + abilitiesOnCooldown.Add(ability); + Debug.Log($"{ability.abilityName} is now on cooldown for {ability.cooldownTime} seconds."); + + yield return new WaitForSeconds(ability.cooldownTime); + + abilitiesOnCooldown.Remove(ability); + Debug.Log($"{ability.abilityName} is off cooldown."); + } } diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacterMovement.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacterMovement.cs index 79adf04..cb98187 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacterMovement.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacterMovement.cs @@ -249,9 +249,32 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character m_Rigidbody.rotation = transform.rotation; } + + private float speedModifier = 1.0f; + + /// + /// Adjusts the character's speed by applying a multiplier. + /// + /// The speed modifier (e.g., 0.5 for half speed, 2.0 for double speed). + public void SetSpeedModifier(float modifier) + { + speedModifier = Mathf.Clamp(modifier, 0.1f, 10.0f); // Prevent extreme values + } + + /// + /// Resets the speed modifier back to normal speed. + /// + public void ResetSpeedModifier() + { + speedModifier = 1.0f; + } + /// /// Retrieves the speed for this character's class. /// + /// + /// Retrieves the adjusted speed for this character's class. + /// private float GetBaseMovementSpeed() { #if UNITY_EDITOR || DEVELOPMENT_BUILD @@ -262,7 +285,7 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character #endif CharacterClass characterClass = GameDataSource.Instance.CharacterDataByType[m_CharLogic.CharacterType]; Assert.IsNotNull(characterClass, $"No CharacterClass data for character type {m_CharLogic.CharacterType}"); - return characterClass.Speed; + return characterClass.Speed * speedModifier; } diff --git a/Assets/Scripts/Gameplay/SlowZonePrefab.cs b/Assets/Scripts/Gameplay/SlowZonePrefab.cs index 7c7c221..4eaa2f6 100644 --- a/Assets/Scripts/Gameplay/SlowZonePrefab.cs +++ b/Assets/Scripts/Gameplay/SlowZonePrefab.cs @@ -38,12 +38,12 @@ public class SlowZonePrefab : NetworkBehaviour if (collider.TryGetComponent(out var player)) { // Check if the player is NOT the crow - if (!player.IsCrow) - { + // if (!player.IsCrow) + // { // Apply slow effect StartCoroutine(SlowPlayer(player)); Debug.Log($"{player.name} is slowed down!"); - } + // } } } } @@ -51,14 +51,14 @@ public class SlowZonePrefab : NetworkBehaviour private IEnumerator SlowPlayer(ServerCharacter player) { // Halve the player's movement speed - player.GetComponent().speed /= 2; + player.Movement.SetSpeedModifier(0.2f); // player.Movement.SetMovementSpeed(originalSpeed * slowMultiplier); // Wait for the slow duration yield return new WaitForSeconds(slowDuration); // Restore the original speed - player.GetComponent().speed /= 2; + player.Movement.ResetSpeedModifier(); Debug.Log($"{player.name}'s speed is restored."); Destroy(gameObject); }