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.
79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
1 month ago
|
using System.Collections.Generic;
|
||
|
using Unity.Netcode;
|
||
|
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;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
// If ability mode is active, listen for mouse input to activate ability
|
||
|
if (isAbilityActive && Input.GetMouseButtonDown(0))
|
||
|
{
|
||
|
UseActiveAbility();
|
||
|
ToggleAbilityMode(activeAbility);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Activates ability by key (called externally).
|
||
|
/// </summary>
|
||
|
/// <param name="key">The key assigned to an ability.</param>
|
||
|
public void ActivateAbilityByKey(string key)
|
||
|
{
|
||
|
foreach (var ability in abilities)
|
||
|
{
|
||
|
if (ability.abilityKey == key)
|
||
|
{
|
||
|
ToggleAbilityMode(ability);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
Debug.LogWarning($"No ability assigned to key {key}.");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Toggles the ability activation mode.
|
||
|
/// </summary>
|
||
|
/// <param name="ability">The ability to toggle.</param>
|
||
|
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.name} activated! Click to use.");
|
||
|
}
|
||
|
|
||
|
private void DeactivateAbilityMode()
|
||
|
{
|
||
|
isAbilityActive = false;
|
||
|
activeAbility = null;
|
||
|
Debug.Log("Ability mode deactivated.");
|
||
|
}
|
||
|
|
||
|
private void UseActiveAbility()
|
||
|
{
|
||
|
if (activeAbility != null)
|
||
|
{
|
||
|
activeAbility.TryActivate(gameObject);
|
||
|
DeactivateAbilityMode(); // Ability used, deactivate mode
|
||
|
}
|
||
|
}
|
||
|
}
|