Encounter / Assets / Scripts / BattleLogic / EntityModList.cs
EntityModList.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class EntityModList
{
    private Dictionary<Modifier, int> mods;

    private bool ampedCheck = false;

    public int Count
    {
        get => mods.Count;
    }

    public EntityModList()
    {
        mods = new Dictionary<Modifier, int>();
    }

    public void AddModifier(Modifier modifier, int turns) 
        => mods.Add(modifier, turns);
    public void RemoveModifier(Modifier modifier)
    {
        if (modifier.Name.Equals("Amped"))
            ampedCheck = false;

        mods.Remove(modifier);
    }
        
    public void ClearModifiers()
    {
        Dictionary<Modifier, int> removedMods = new Dictionary<Modifier, int>();
        foreach (KeyValuePair<Modifier, int> modifier in mods)
        {
            if (modifier.Value > 100)
                continue;
            else
                removedMods.Add(modifier.Key, 0);
        }

        foreach (KeyValuePair<Modifier, int> mod in removedMods)
        {
            mods.Remove(mod.Key);
            mods.Add(mod.Key, mod.Value);
        }
    }
    public bool Contains(Modifier mod) 
        => mods.ContainsKey(mod);
    public bool SmartModCheck(string modifierName)
    {
        bool hasMod = mods.Where  (mod => mod.Key.Name.Equals(modifierName))
                          .Select (mod => mod.Key)
                          .Count() is not 0;

        if (!hasMod)
            return false;

        switch (modifierName)
        {
            // Case Amped switches between true and false for when the battle handler calls this the first time,
            // switches to true, then when the entity attempts to take a third turn, switches to false and prevents
            // the entity from taking a turn until the entity is cycled back to in the handler
            case "Amped":
                ampedCheck = !ampedCheck;
                return ampedCheck;
            case "Bleeding":
                return true;
            default:
                return hasMod;
        }
    }
    public void DecrementTurnsForMods()
    {
        Dictionary<int, (int, Modifier)> modifierTurnList = new Dictionary<int, (int, Modifier)>();
        for (int i = 0; i < mods.Count; ++i)
        {
            if (mods.ElementAt(i).Key.LengthOfEffect.Contains(Dice.DiceR.Hundred))
                modifierTurnList.Add(i, (999, mods.ElementAt(i).Key));
            else
                modifierTurnList.Add(i, (mods.ElementAt(i).Value - 1, mods.ElementAt(i).Key));
        }
        mods.Clear();
        foreach (KeyValuePair<int, (int, Modifier)> mod in modifierTurnList)
            mods.Add(mod.Value.Item2, mod.Value.Item1);
    }

    public IEnumerator GetEnumerator()
    {
        foreach (KeyValuePair<Modifier, int> m in mods)
            yield return m;
    }

    public void IncreaseModifierTime(Modifier mod, Entity attachedEntity)
    {
        // Find the mod and increment its turn counter by the roll value of the mod itself
        int turnsLeft = mods.Where(m => m.Key.Name.Equals(mod.Name)).First().Value;
        if (mod.LengthOfEffect.Count is not 0)
            turnsLeft += UtilityHandler.RollValue(mod.LengthOfEffect.ToArray());
        else
        {
            turnsLeft += mod.LengthOfEffectNoDice;

            if (mod.Name is "Rage" && attachedEntity.GetInventory().Abilities.Contains(AbilityBrain.GetAbilityList()[83]))
                turnsLeft += 2;
        }

        // Re-add the mod with the new turn counter
        mods.Remove(mod);
        mods.Add(mod, turnsLeft);
    }

    public void IndicateModification(string modifierName, params int[] damageValues)
    {
        switch (modifierName)
        {
            case "Life Contract":
                Modifier mod = mods.Where(mod => mod.Key.Name is "Life Contract").FirstOrDefault().Key;

                List<int> retainedValues = new List<int>(mod.RetentionObject as List<int>);
                int currentDamageValue = retainedValues[1];
                foreach (int value in damageValues)
                    currentDamageValue += value;

                retainedValues[1] = currentDamageValue;

                break;
        }
    }

    public Modifier GatherModifier(string modifierName)
    {
        try
        {
            return mods.Where(mod => mod.Key.Name.Equals(modifierName)).FirstOrDefault().Key;
        }
        catch { }

        return null;
    }
}