Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Managers / InventoryManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour
{
    [Header("Inventory Settings: ")]
    [SerializeField] private int inventoryCapacity;
    [SerializeField] private int quickInventoryCapacity = 5;

    [SerializeField] private Inventory inventory;
    [SerializeField] private Inventory quickInventory;

    public Inventory Inventory => inventory;
    public Inventory QuickInventory => quickInventory;

    //Testing purposes
    [Header("Item Stack Prefabs")]
    [SerializeField] private Item item;
    public GameObject itemStack;

    void Awake()
    {
        inventory = new Inventory(inventoryCapacity, gameObject);
        quickInventory = new Inventory(quickInventoryCapacity, gameObject);
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        //Perform inventory slot interaction
        QuickInventoryActions();

        if (Input.GetKeyDown(KeyCode.F))
        {
            AddItem(item, 1);
            Debug.Log($"Added Item: {item.ItemName}");
        }
    }
    private void QuickInventoryActions()
    {
        if (Input.GetKeyDown(InputManager.GetKey(InputAction.use1)))
        {
            UseInventoryItems(0);
        }

        else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use2)))
        {
            UseInventoryItems(1);
        }

        else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use3)))
        {
            UseInventoryItems(2);
        }

        else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use4)))
        {
            UseInventoryItems(3);
        }

        else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use5)))
        {
            UseInventoryItems(4);
        }
    }

    private void UseInventoryItems(int index)
    {
       InventorySlot slot = quickInventory.GetSlot(index);

        if (slot.IsEmpty()) return;

        Item item = slot.Item;

        if (item == null) return;

        if (transform.parent.TryGetComponent(out PlayerController controller))
        {
            switch (item)
            {
                case Potion potion:

                    Debug.Log("Try to apply effect");
                    //Apply Potion Effects
                    potion.Visit(controller);
                    slot.Amount--;
                    return;
            }
        }

        else
        {
            Debug.LogError($"PlayerController is NULL!");
        }
    }

    //Handles the item store on the inventories
    public bool AddItem(Item item, int amount)
    {
        //Tries to add the item on the quick inventory
        if (quickInventory.AddItem(item, amount))
        {
            return true;
        }

        //Tries to add the item to the internal inventory if the quickInventory is full
        else if(inventory.AddItem(item, amount))
        {
            return true;
        }

        else
        {
            return false;
        }
    }

    public void SwapInventorySlot(InventorySlot slotA, InventorySlot slotB)
    {
        if (slotA.Owner != slotB.Owner)
        {
            //Inventory slotAinventory
        }

        else
        {
            //Get the inventory of the slot
            Inventory inventory = slotA.Owner;
            inventory.SwapItemSlot(slotA.SlotIndex, slotB.SlotIndex);

        }
    }

    public void DropItem(int index, int amount)
    {
        //Item item = GetItem(index);
        //GameObject go = gameObject.itemStack;

        //if (go != null)
        //{
        //    Vector3 targePos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0);
        //    GameObject obj = Instantiate(go, targePos, Quaternion.identity);
        //    ItemStack stack = obj.GetComponent<ItemStack>();
        //    stack.ItemStackInit(item, amount);
        //    RemoveItem(index, amount);
        //    return;
        //}

        //else
        //{
        //    Debug.LogError($"Item stack is Null");
        //}
    }
}