stackable inv objects, inv container

This commit is contained in:
HelarJ
2022-03-28 10:08:54 +03:00
parent 84e21c94f3
commit 8246a9ef8a
20 changed files with 3595 additions and 637 deletions

View File

@@ -1,39 +1,77 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
[Serializable]
[RequireComponent(typeof(XRSocketInteractor))]
public class Inventory : MonoBehaviour
{
public int slots = 1;
private List<int> _inv;
private XRSocketInteractor xrSocketInteractor;
public List<InventorySlot> additionalSlots;
public GameObject itemContainerPrefab;
private List<InventorySlot> _inventorySlots;
private void Awake()
{
xrSocketInteractor = GetComponent<XRSocketInteractor>();
_inv = new List<int>(slots);
var oldestInteractableSelected = xrSocketInteractor.GetOldestInteractableSelected();
xrSocketInteractor.selectEntered.AddListener(Call);
xrSocketInteractor.hoverEntered.AddListener(Call);
_inventorySlots = new List<InventorySlot>();
foreach (var inventorySlot in additionalSlots)
{
inventorySlot.SetParentInventory(this);
_inventorySlots.Add(inventorySlot);
}
}
private void Call(HoverEnterEventArgs arg0)
private void OnEnable()
{
Debug.Log("hoverenter "+arg0.interactableObject + " " + xrSocketInteractor.hasHover + " " + xrSocketInteractor.hasSelection);
var inventorySlotsInChildren = GetComponentsInChildren<InventorySlot>();
foreach (var inventorySlot in inventorySlotsInChildren)
{
inventorySlot.SetParentInventory(this);
_inventorySlots.Add(inventorySlot);
}
}
public bool AddItem(ItemData item)
{
if (item.canStack)
{
return AddItem(ConvertToInventoryItem(item));
}
return AddToFirstOpenSlot(item.gameObject);
}
public bool AddItem(InventoryItem item)
{
//Stacks the item if it already exists in the inventory.
foreach (var inventorySlot in _inventorySlots.Where(inventorySlot =>
inventorySlot.ContainsItem(item.GetItemid())))
{
inventorySlot.GetItem().ChangeCount(item.Count);
return true;
}
return AddToFirstOpenSlot(item.gameObject);
}
private void Call(SelectEnterEventArgs arg0)
private bool AddToFirstOpenSlot(GameObject item)
{
Debug.Log("selectenter "+arg0.interactableObject);
foreach (var inventorySlot in _inventorySlots.Where(inventorySlot => !inventorySlot.ContainsItem()))
{
inventorySlot.AssignItem(item);
return true;
}
return false;
}
}
private InventoryItem ConvertToInventoryItem(ItemData item)
{
var inventoryItem = Instantiate(itemContainerPrefab, transform);
itemContainerPrefab.GetComponent<InventoryItem>().itemPrefab = item.gameObject;
return inventoryItem.gameObject.GetComponent<InventoryItem>();
}
}