using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
private int activeSlot;
public int totalInventorySlots = 5;
private List<Equipable> inventory = new List<Equipable>();
public Camera yourCameraReference; // Assign your camera reference in the Inspector
public GameObject yourPlayerReference; // Assign your player reference in the Inspector
// Declare drop-related variables
private void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
// Start is called before the first frame update
void Start()
{
InitializeInventory();
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectWeapon = activeSlot;
NumberInput();
ScrollWheelInput();
if (previousSelectWeapon != activeSlot)
{
SelectWeapon();
}
// Corrected SwitchWeapon method call
SwitchWeapon(-1, 0);
foreach (var item in inventory)
{
if (item.pickedUp && item.player)
{
item.readyToUse = true;
}
}
// Check for 'G' key press to drop the equipped item
if (Input.GetKeyDown(KeyCode.G))
{
DropEquippedItem();
}
}
// Add a method to drop the equipped item
void DropEquippedItem()
{
if (activeSlot >= 0 && activeSlot < inventory.Count)
{
Equipable equippedItem = inventory[activeSlot];
equippedItem.Drop();
inventory.RemoveAt(activeSlot);
// Select the next available item after dropping
activeSlot = Mathf.Clamp(activeSlot, 0, inventory.Count - 1);
SelectWeapon();
SwitchWeapon(-1, activeSlot);
}
}
void InitializeInventory()
{
// Assuming pickups are child objects of the WeaponSwitcher
foreach (Transform child in transform)
{
Equipable equipable = child.GetComponent<Equipable>();
if (equipable != null)
{
inventory.Add(equipable);
}
}
}
public void AddItemToInventory(Equipable item)
{
if (!inventory.Contains(item))
{
if (inventory.Count < totalInventorySlots)
{
// If there are empty slots, add the new item without dropping the current active slot
inventory.Add(item);
item.pickedUp = true;
item.player = true;
// Find the index of the newly added item in the inventory
int newItemIndex = inventory.IndexOf(item);
// Set activeSlot to the index of the newly added item
activeSlot = newItemIndex;
// Update the selection and draw the new item
SelectWeapon();
SwitchWeapon(-1, activeSlot);
}
else
{
// If all slots are taken, drop the current active slot and add the new item in the same slot
Equipable equippedItem = inventory[activeSlot];
equippedItem.Drop();
inventory[activeSlot] = item;
// Update the selection and draw the new item
SelectWeapon();
SwitchWeapon(-1, activeSlot);
}
}
}
void SelectWeapon()
{
int i = 0;
foreach (Equipable weapon in inventory)
{
if (i == activeSlot)
{
weapon.Enable();
}
else
{
weapon.Disable();
}
i++;
}
}
void SwitchWeapon(int previousSlot, int newSlot)
{
if (previousSlot == newSlot)
return;
if (previousSlot >= 0 && previousSlot < inventory.Count)
inventory[previousSlot].Holster();
if (newSlot >= 0 && newSlot < inventory.Count)
inventory[newSlot].Draw();
}
void NumberInput()
{
if (Input.GetKeyDown(KeyCode.Alpha1)) activeSlot = 0;
if (Input.GetKeyDown(KeyCode.Alpha2)) activeSlot = 1;
if (Input.GetKeyDown(KeyCode.Alpha3)) activeSlot = 2;
if (Input.GetKeyDown(KeyCode.Alpha4)) activeSlot = 3;
if (Input.GetKeyDown(KeyCode.Alpha5)) activeSlot = 4;
}
void ScrollWheelInput()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (activeSlot >= transform.childCount - 1)
{
activeSlot = 0;
}
else
{
activeSlot += 1;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (activeSlot <= 0)
{
activeSlot = transform.childCount - 1;
}
else
{
activeSlot -= 1;
}
}
}
}