stackable inv objects, inv container
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user