slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

AbilityUser.cs (5820B)


      1 using System.Collections.Generic;
      2 using System.Linq;
      3 using Slimecing.Abilities;
      4 using Slimecing.Abilities.AbilityEffects;
      5 using Slimecing.Abilities.UseAbilities;
      6 using UnityEngine;
      7 
      8 namespace Slimecing.Characters
      9 {
     10     //LOOKING LIKE THERE IS TOO MANY FUNCTIONS LOOK INTO SPLITTING UP. Maybe needs interface ICooldownUser, IAbilityEffectUser
     11     public class AbilityUser : MonoBehaviour
     12     {
     13         //[SerializeField] private CharacterMovementController controller;
     14 
     15         //public CharacterMovementController Controller { get { return controller; } }
     16         [SerializeField] private List<AbilityPackage> useableAbilities = new List<AbilityPackage>();
     17 
     18         public List<TimedAbilityEffect> currentTimedAbilityEffects { get; set; } = new List<TimedAbilityEffect>();
     19 
     20         public List<CooldownData> abilitiesOnCooldown { get; set; } = new List<CooldownData>();
     21 
     22         public List<AbilityEffect> currentAbilityEffects { get; set; } = new List<AbilityEffect>();
     23 
     24         public AudioSource Audio { get; private set; }
     25 
     26         private void Awake()
     27         {
     28             Audio = GetComponent<AudioSource>();
     29             //Repeated code needs to be fixed!
     30             if (useableAbilities.Count != 0)
     31             {
     32                 for (int i = useableAbilities.Count - 1; i >= 0; i--)
     33                 {
     34                     if (useableAbilities[i] == null)
     35                     {
     36                         useableAbilities.RemoveAt(i);
     37                     }
     38                     else
     39                     {
     40                         useableAbilities[i].Initialize(this);
     41                     }
     42                 }
     43             }
     44 
     45             foreach (var ability in useableAbilities)
     46             {
     47                 
     48             }
     49         }
     50         private void Update()
     51         {
     52             CheckAbilitiesOnCooldown();
     53             CheckAbilityEffects();
     54             CheckTimedAbilityEffects();
     55         }
     56 
     57         public bool CheckForEffects()
     58         {
     59             return currentAbilityEffects.Count != 0 || currentTimedAbilityEffects.Count != 0;
     60         }
     61         
     62         private void CheckTimedAbilityEffects()
     63         {
     64             for (int i = currentTimedAbilityEffects.Count - 1; i >= 0; i--)
     65             {
     66                 currentTimedAbilityEffects[i].Tick(Time.deltaTime);
     67                 if (currentTimedAbilityEffects[i].IsDone)
     68                 {
     69                     RemoveTimedAbilityEffect(currentTimedAbilityEffects[i]);
     70                 }
     71             }
     72         }
     73         
     74         private void CheckAbilityEffects()
     75         {
     76             for (int i = currentAbilityEffects.Count - 1; i >= 0; i--)
     77             {
     78                 if (currentAbilityEffects[i].DoesUpdate())
     79                 {
     80                     currentAbilityEffects[i].DoUpdate();
     81                 }
     82             }
     83         }
     84         
     85         private void CheckAbilitiesOnCooldown()
     86         {
     87             //COPY PASTED CODE FROM CHECKTIMEDABILITY EFFECTS! SINGLE RESPONSIBLILITY
     88             for (int i = abilitiesOnCooldown.Count - 1; i >= 0; i--)
     89             {
     90                 abilitiesOnCooldown[i].TickCooldown(Time.deltaTime);
     91                 if (!abilitiesOnCooldown[i].IsCoolin)
     92                 {
     93                     abilitiesOnCooldown.RemoveAt(i);
     94                 }
     95             }
     96         }
     97 
     98         //Something feels wrong about this there might be a better way. Put in ability processor (struct)
     99         private void CheckAbility(AbilityPackage abilityPack)
    100         {
    101             Ability ability = abilityPack.ability;
    102             if (ReferenceEquals(ability, null))
    103             {
    104                 //Make a void factory that dishes out void objects/ classes and stuff
    105                 //it is slow to create an instance of an object ~13ms maybe make an IAbility and make a void that
    106                 AddAbility(ScriptableObject.CreateInstance<VoidAbility>());
    107             }
    108             else if (!abilityPack.initialized)
    109             {
    110                 abilityPack.Initialize(this);
    111             }
    112         }
    113 
    114         //CHANGE THESE THREE METHODS TO SCRIPTIBLEOBJECT EVENTS
    115         public void AddTimedAbilityEffect(TimedAbilityEffect abilityEffect)
    116         {
    117             currentTimedAbilityEffects.Add(abilityEffect);
    118             abilityEffect.Activate();
    119         }
    120 
    121         public void RemoveTimedAbilityEffect(TimedAbilityEffect abilityEffect)
    122         {
    123             currentTimedAbilityEffects.Remove(abilityEffect);
    124         }
    125 
    126         public void AddAbilityEffect(AbilityEffect abilityEffect)
    127         {
    128             currentAbilityEffects.Add(abilityEffect);
    129             abilityEffect.Activate();
    130         }
    131         
    132         public void RemoveAbilityEffect(AbilityEffect abilityEffect)
    133         {
    134             currentAbilityEffects.Remove(abilityEffect);
    135         }
    136 
    137         public void AddAbilityOnCooldown(CooldownData cooldownData)
    138         {
    139             abilitiesOnCooldown.Add(cooldownData);
    140         }
    141         
    142         public AbilityEffect GetAbilityEffect(AbilityEffect abilityEffect)
    143         {
    144             return currentAbilityEffects.FirstOrDefault(effect => effect.Equals(abilityEffect));
    145         }
    146         
    147         public TimedAbilityEffect GetTimedAbilityEffect(TimedAbilityEffect timedAbilityEffect)
    148         {
    149             return currentTimedAbilityEffects.FirstOrDefault(effect => effect.Equals(timedAbilityEffect));
    150         }
    151         
    152         public void AddAbility(Ability ability)
    153         {
    154             foreach (var abilities in useableAbilities)
    155             {
    156                 if (!ReferenceEquals(abilities.ability, null)) continue;
    157                 abilities.ability = ability;
    158                 abilities.Initialize(this);
    159                 return;
    160             }
    161             AbilityPackage abilityPack = new AbilityPackage(ability);
    162             useableAbilities.Add(abilityPack);
    163             abilityPack.Initialize(this);
    164         }
    165     }
    166 }