You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/AbilitySystem.cs

96 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AbilitySystem : MonoBehaviour
{
[Header("Assigned Abilities")]
public List<Ability> abilities = new List<Ability>();
private Ability activeAbility; // Tracks the currently active ability in "spawn mode"
private bool isAbilityActive = false;
private HashSet<Ability> abilitiesOnCooldown = new HashSet<Ability>();
void Update()
{
if (isAbilityActive && Input.GetMouseButtonDown(0))
{
UseActiveAbility();
}
}
/// <summary>
/// Activates an ability by its keybind.
/// </summary>
public void ActivateAbilityByKey(string key)
{
foreach (var ability in abilities)
{
if (ability.abilityKey == key)
{
if (!abilitiesOnCooldown.Contains(ability))
{
ToggleAbilityMode(ability);
}
else
{
Debug.Log($"{ability.abilityName} is on cooldown.");
}
return;
}
}
Debug.LogWarning($"No ability assigned to key {key}.");
}
private void ToggleAbilityMode(Ability ability)
{
if (isAbilityActive && activeAbility == ability)
{
DeactivateAbilityMode();
}
else
{
ActivateAbilityMode(ability);
}
}
private void ActivateAbilityMode(Ability ability)
{
isAbilityActive = true;
activeAbility = ability;
Debug.Log($"Ability {ability.abilityName} activated! Click to use.");
}
private void DeactivateAbilityMode()
{
isAbilityActive = false;
activeAbility = null;
Debug.Log("Ability mode deactivated.");
}
private void UseActiveAbility()
{
if (activeAbility != null)
{
activeAbility.ActivateAbility(gameObject);
StartCoroutine(StartCooldown(activeAbility));
DeactivateAbilityMode();
}
}
/// <summary>
/// Starts the cooldown coroutine for the ability.
/// </summary>
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.");
}
}