Encounter / Assets / Scripts / InventoryManagement / Item.cs
Item.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Serves as a retainer for the scriptable objects when being parsed
/// </summary>
public class Item
{
    #region Properties
    public int ItemId { get; private set; }                        // Important ID information for the Item
    public string ItemName { get; private set; }                   // Name of the item constructed
    public int ItemValue { get; private set; }                     // Value of the item for shops to buy from the player
    public int ItemCost { get; private set; }                      // Cost of the item for shops to sell to the player
    public Sprite ItemSprite { get; private set; }                 // Sprite of the item object
    public string ItemDescription { get; set; }            // Description component of the item object
    public ItemSO.Rarity ItemRarity { get; private set; }          // Rarity level of the item object
    public bool CantBeSold { get; private set; }                   // Indicator if the item can be sold or not
    public bool IsUniqueItem { get; private set; }                 // Indicator if it is possible to hold multiple of this item
    public List<string> HelperEffects { get; set; }        // List of Effects that are displayed in the Aspect Helper GUI
    public bool HasRequirements { get; private set; }
    public List<ItemSO.PossReqs> Requirements { get; private set; }

    public readonly int numberOfStatReqs;
    public readonly List<Vector3Int> statListReqs;
    public readonly int numberOfSecStatReqs;
    public readonly List<Vector3Int> secStatListReqs;
    public readonly int numberOfItems;
    public readonly List<string> reqItemNames;
    public readonly int InitialCost;

    #endregion

    public Item(ItemSO reference)
    {
        try
        {
            // (1) If the Item does not contain important pieces of information...
            if (string.IsNullOrEmpty(reference.Name) || string.IsNullOrEmpty(reference.Description) || reference.ID < 0 || reference.Item_Sprite == null)
                throw new ArgumentNullException("An item value has shown to be impossible. Creating error Item.");

            this.ItemId                     = reference.ID;
            this.ItemName                   = reference.Name;
            this.ItemValue                  = reference.Value;
            this.ItemCost                   = reference.Cost;
            this.InitialCost                = reference.Cost;
            this.ItemSprite                 = reference.Item_Sprite;
            this.ItemDescription            = reference.Description;
            this.ItemRarity                 = reference.Item_Rarity;
            this.CantBeSold                 = reference.CantBeSold;
            this.IsUniqueItem               = reference.IsUniqueItem;
            this.HelperEffects              = reference.HelperEffects;
            this.HasRequirements            = reference.Has_Requirement;
            this.Requirements               = new List<ItemSO.PossReqs>(reference.Requirements);

            if (this.Requirements.Contains(ItemSO.PossReqs.Stats))
            {
                this.numberOfStatReqs       = reference.Number_Of_Stat_Reqs;
                this.statListReqs           = new List<Vector3Int>(reference.Stats_List);
            }
            if (this.Requirements.Contains(ItemSO.PossReqs.Sec_Stats))
            {
                this.numberOfSecStatReqs    = reference.Number_Of_Sec_Stat_Reqs;
                this.secStatListReqs        = new List<Vector3Int>(reference.Sec_Stats_List);
            }
            if (this.Requirements.Contains(ItemSO.PossReqs.Item))
            {
                this.numberOfItems          = reference.Number_Of_Items;
                this.reqItemNames           = new List<string>(reference.Item_List);
            }
        }
        catch
        {
            // (2) Then an Error item is created
            this.ItemId                = -1;
            this.ItemName              = "I am Error";
            this.ItemValue             = -1;
            this.ItemCost              = -1;
            this.ItemSprite            = Resources.Load<Sprite>("Sprites/Error");
            this.ItemDescription       = "Impossible... You yield the void...";
            this.ItemRarity            = ItemSO.Rarity.Strange;
            this.CantBeSold            = true;
            this.IsUniqueItem          = true;
            this.HelperEffects = new List<string>()
            {
                "Error",
                "Error",
                "Error"
            };

            this.HasRequirements        = false;
        }
    }
    /// <summary>
    /// Implements a discount for the item instance
    /// </summary>
    /// <param name="v">The percentage discount</param>
    public void ImplementDiscountValue(float v)
    {
        float newValue = this.ItemCost * v;

        // If the newValue var decrement was less than 1 than simply just detract 1 value from the cost
        if (this.ItemCost - 1 <= newValue)
            this.ItemCost--;
        // Otherwise just take the ceil of the newValue for the new cost
        else
            this.ItemCost = (int)Math.Ceiling(newValue);
    }
    public void ImplementPriceSurge(float v)
    {
        float newValue = this.InitialCost * v;

        // If the newValue increment was less than 1, simply increment the cost by 1
        if (newValue <= 1)
            this.ItemCost++;
        else
            this.ItemCost = (int)Math.Floor(newValue);
    }
    /// <summary>
    /// Implements an increase in the item value for selling for this instance of item
    /// </summary>
    /// <param name="v">The increase percentage in value</param>
    public void ImplementItemValueModifier(float v)
    {
        float newValue = this.ItemValue * v;

        // If the newValue var increment was less than 1, simply increment the value by 1
        if (this.ItemValue + 1 >= newValue)
            this.ItemValue++;
        // Otherwise increments the item value to the floor of the new value
        else 
            this.ItemValue = (int)Math.Floor(newValue);
    }
    #region Operators and Overrides
    public static bool operator ==(Item left, Item right)
    {
        bool leftNullCheck = false;
        bool rightNullCheck = false;

        try
        {
            string check = left.ItemName;
        }
        catch { leftNullCheck = true; }
        try
        {
            string check = right.ItemName;
        }
        catch {  rightNullCheck = true; }

        if (leftNullCheck && rightNullCheck)
            return true;
        else if ((leftNullCheck && !rightNullCheck) || (!leftNullCheck && rightNullCheck))
            return false;

        return left.ItemId == right.ItemId && left.ItemName == right.ItemName;
    }
    public static bool operator !=(Item left, Item right) 
        => !(left == right);
    public override bool Equals(object obj)
    {
        return obj as Item == this;
    }
    public override int GetHashCode()
    {
        if (this.ItemName is null)
            return 0;

        return ItemName.GetHashCode();
    }
    #endregion
}