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

public class Modifier
{
    public string Name { get; private set; }
    public string Description { get; private set; }
    public ModifierSO.Type Type { get; private set; }
    public Sprite ModSprite { get; private set; }
    public ModifierSO.Effect Effect { get; private set; }
    public List<Dice.DiceR> LengthOfEffect { get; private set; }
    public int LengthOfEffectNoDice { get; private set; }
    public bool[] IsValues { get; private set; }
    public bool ModifierAppliedAfter { get; private set; }
    public ModifierSO EndModifier { get; private set; }
    
    protected int normalValue;
    protected List<EntityStats.Stat> statModified;
    protected int percentageValue;
    protected List<Dice.DiceR> diceValues;
    protected int? flagValue;

    public object RetentionObject { get; set; }

    public int GetNormalValue() 
        => normalValue;
    public List<EntityStats.Stat> GetStatsModified() 
    {
        if (statModified is null)
            return null;
        else 
            return statModified;
    }
    public int GetPercentageValue() 
        => percentageValue;
    public List<Dice.DiceR> GetDiceValues() 
        => diceValues;

    public Modifier(ModifierSO reference)
    {
        try
        {
            this.Name                   = reference.Name;
            this.Description            = reference.Description;
            this.Type                   = reference.Mod_Type;
            this.ModSprite              = reference.Mod_Sprite;
            this.Effect                 = reference.Modifier;

            if (this.Effect is ModifierSO.Effect.Other)
                this.flagValue = reference.FlagValue;

            this.LengthOfEffect         = reference.Length_Of_Effect;
            this.LengthOfEffectNoDice   = reference.LengthOfEffectNoDice;
            this.IsValues               = new bool[3] { reference.Is_Value, reference.Is_Percent, reference.Is_Dice };
            
            if (IsValues[0])
            {
                this.normalValue = reference.Value;
                if (this.Effect is not ModifierSO.Effect.Accuracy && this.Effect is not ModifierSO.Effect.ArmorModifier)
                    statModified       = new List<EntityStats.Stat>(reference.StatModified);
            }
            else if (IsValues[1])
                this.percentageValue    = reference.Percent;
            else
                this.diceValues         = reference.Dice_Value;

            this.ModifierAppliedAfter = reference.ModifierAppliedAtEndOfEffect;
            if (this.ModifierAppliedAfter)
                this.EndModifier = reference.EndModifier;
        }
        catch
        {
            this.Name                   = "I AM ERROR";
            this.Description            = string.Empty;
            this.Type                   = ModifierSO.Type.Buff;
            this.ModSprite              = Resources.Load<Sprite>("Sprites/Error");
            this.Effect                 = ModifierSO.Effect.Stat;
            this.LengthOfEffect         = new List<Dice.DiceR>() { Dice.DiceR.Four };
            this.IsValues               = new bool[3] { true, true, true };
            normalValue                 = 0;
            percentageValue             = 0;
            diceValues                  = new List<Dice.DiceR>() { Dice.DiceR.Four };
        }
    }

    public override string ToString()
        => $"{this.Name}";

    public override bool Equals(object obj)
    {
        return obj is Modifier modifier &&
               Name == modifier.Name;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Name);
    }

    public static bool operator ==(Modifier left, Modifier right)
    {
        return EqualityComparer<Modifier>.Default.Equals(left, right);
    }

    public static bool operator !=(Modifier left, Modifier right)
    {
        return !(left == right);
    }
}