Ability.cs (2833B)
1 using System; 2 using Slimecing.Characters; 3 using Slimecing.Triggers; 4 using UnityEngine; 5 6 namespace Slimecing.Abilities 7 { 8 [Serializable] 9 public abstract class Ability : ScriptableObject 10 { 11 [SerializeField] protected string abilityName = "REPLACE ABILITY NAME"; 12 [SerializeField] protected float abilityCooldown = 1f; 13 [SerializeField] protected AudioClip abilitySound; 14 [SerializeField] private Trigger abilityTrigger; 15 16 public Trigger runtimeAbilityTrigger { get; set; } 17 18 public virtual void CheckActivation(AbilityUser aUser, TriggerState state) 19 { 20 if (runtimeAbilityTrigger == null) return; 21 if (state != TriggerState.Performed) return; 22 runtimeAbilityTrigger.currentTriggerState = TriggerState.Canceled; 23 StartAbility(aUser); 24 } 25 26 public abstract void Use(AbilityUser aUser); 27 28 public virtual void Initialize(AbilityUser aUser) 29 { 30 runtimeAbilityTrigger = abilityTrigger.GetTrigger(); 31 runtimeAbilityTrigger.EnableTrigger(aUser.gameObject); 32 if (abilitySound == null) abilitySound = AudioClip.Create("void", 1, 1, 1000, false); 33 34 runtimeAbilityTrigger.TriggerStateChange += ctx => CheckActivation(aUser, ctx); 35 } 36 37 public void StartAbility(AbilityUser aUser) 38 { 39 switch (CanUse(aUser)) 40 { 41 case CooldownState.FirstTimeUse: 42 Use(aUser); 43 PutOnCooldown(aUser); 44 break; 45 case CooldownState.HasAnotherUse: 46 Use(aUser); 47 break; 48 case CooldownState.OnCooldown: 49 break; 50 } 51 } 52 53 protected virtual void PutOnCooldown(AbilityUser aUser) 54 { 55 aUser.AddAbilityOnCooldown(new CooldownData(this, abilityCooldown)); 56 } 57 protected CooldownState CanUse(AbilityUser aUser) 58 { 59 Ability coolinAbility = null; 60 CooldownData coolinAbilityObject = null; 61 foreach (var cooldownData in aUser.abilitiesOnCooldown) 62 { 63 if (cooldownData.GetAbility != this) continue; 64 coolinAbility = cooldownData.GetAbility; 65 coolinAbilityObject = cooldownData; 66 break; 67 } 68 69 if (ReferenceEquals(coolinAbility, null)) return CooldownState.FirstTimeUse; 70 if (coolinAbilityObject.UseAmount <= 0) return CooldownState.OnCooldown; 71 coolinAbilityObject.SubUseAmount(); 72 return CooldownState.HasAnotherUse; 73 } 74 75 private void EndAbility() 76 { 77 runtimeAbilityTrigger.currentTriggerState = TriggerState.Canceled; 78 } 79 } 80 }