stackable inv objects, inv container
This commit is contained in:
30
Assets/Project Files/Scripts/Helar/ConsolePrinter.cs
Normal file
30
Assets/Project Files/Scripts/Helar/ConsolePrinter.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class ConsolePrinter : MonoBehaviour
|
||||
{
|
||||
static string historyOutput = "";
|
||||
|
||||
public TMP_Text textBox;
|
||||
void OnEnable()
|
||||
{
|
||||
Application.logMessageReceived += Log;
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
Application.logMessageReceived -= Log;
|
||||
}
|
||||
|
||||
private void Log(string logString, string stackTrace, LogType type)
|
||||
{
|
||||
historyOutput = Time.fixedTime + ": " + logString + "\n" + historyOutput;
|
||||
if (historyOutput.Length > 5000)
|
||||
{
|
||||
historyOutput = historyOutput[..4000];
|
||||
}
|
||||
|
||||
textBox.text = historyOutput;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af6659aa65864ce9987c4e88ca2fa878
|
||||
timeCreated: 1648411791
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aca2b5bf18c279c4cb265c82010714b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
guid: 29f5f8aed8d641c3abc54850a6a876cc
|
||||
timeCreated: 1648431912
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryItem : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
172
Assets/Project Files/Scripts/Helar/InventorySlot.cs
Normal file
172
Assets/Project Files/Scripts/Helar/InventorySlot.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(XRSocketInteractor))]
|
||||
public class InventorySlot : MonoBehaviour
|
||||
{
|
||||
private XRSocketInteractor xrSocketInteractor;
|
||||
|
||||
private GameObject _itemInSlot;
|
||||
|
||||
private Inventory _parentInventory;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
xrSocketInteractor = GetComponent<XRSocketInteractor>();
|
||||
var oldestInteractableSelected = xrSocketInteractor.GetOldestInteractableSelected();
|
||||
|
||||
|
||||
xrSocketInteractor.selectEntered.AddListener(SelectEnter);
|
||||
xrSocketInteractor.selectExited.AddListener(SelectExit);
|
||||
|
||||
xrSocketInteractor.hoverEntered.AddListener(HoverEnter);
|
||||
xrSocketInteractor.hoverExited.AddListener(HoverExit);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void HoverEnter(HoverEnterEventArgs args)
|
||||
{
|
||||
if (_itemInSlot == null) return;
|
||||
if (_itemInSlot.GetComponent<InventoryItem>() == null) return;
|
||||
if (_itemInSlot == args.interactableObject.transform.gameObject) return;
|
||||
|
||||
//Debug.Log("hoverenter "+args.interactableObject + " " + xrSocketInteractor.hasHover + " " + xrSocketInteractor.hasSelection);
|
||||
|
||||
var newItem = args.interactableObject.transform.gameObject;
|
||||
|
||||
var itemData = newItem.GetComponentInChildren<ItemData>();
|
||||
Debug.Log("HOVER ENTER: "+itemData);
|
||||
if (itemData.canStack && itemData.itemId == _itemInSlot.GetComponentInChildren<ItemData>().itemId)
|
||||
{
|
||||
var inventoryItem = newItem.GetComponent<InventoryItem>();
|
||||
|
||||
if (inventoryItem != null)
|
||||
{
|
||||
_itemInSlot.GetComponent<InventoryItem>().ChangeCount(inventoryItem.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
_itemInSlot.GetComponent<InventoryItem>().ChangeCount(1);
|
||||
}
|
||||
|
||||
args.manager.interactableUnregistered += ManagerOninteractableUnregistered;
|
||||
args.manager.UnregisterInteractable(args.interactableObject);
|
||||
args.manager.interactableUnregistered -= ManagerOninteractableUnregistered;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void HoverExit(HoverExitEventArgs args)
|
||||
{
|
||||
Debug.Log("hoverexit "+args.interactableObject + " " + xrSocketInteractor.hasHover + " " + xrSocketInteractor.hasSelection);
|
||||
}
|
||||
|
||||
private void SelectEnter(SelectEnterEventArgs args)
|
||||
{
|
||||
Debug.Log("Added to slot item " + _itemInSlot);
|
||||
var newItem = args.interactableObject.transform.gameObject;
|
||||
|
||||
|
||||
//Converts the item into a inventory item
|
||||
if (newItem.GetComponent<InventoryItem>() == null && newItem.GetComponent<ItemData>().canStack)
|
||||
{
|
||||
|
||||
var load = Resources.Load("Helar/Item", typeof(GameObject)) as GameObject;
|
||||
|
||||
var instance = Instantiate(load, args.interactorObject.transform);
|
||||
|
||||
instance.GetComponent<InventoryItem>().itemPrefab = newItem.gameObject;
|
||||
|
||||
instance.transform.localScale = Vector3.one;
|
||||
instance.transform.localPosition = Vector3.zero;
|
||||
|
||||
instance.GetComponent<InventoryItem>().enabled = true;
|
||||
|
||||
|
||||
args.manager.interactableUnregistered += ManagerOninteractableUnregistered;
|
||||
args.manager.UnregisterInteractable(args.interactableObject);
|
||||
args.manager.interactableUnregistered -= ManagerOninteractableUnregistered;
|
||||
|
||||
args.interactableObject = instance.GetComponent<XRGrabInteractable>();
|
||||
|
||||
args.interactorObject.interactablesSelected.Add(instance.GetComponent<XRGrabInteractable>());
|
||||
|
||||
Debug.Log("Upgraded item to InventoryItem "+instance);
|
||||
|
||||
|
||||
_itemInSlot = instance;
|
||||
return;
|
||||
}
|
||||
|
||||
_itemInSlot = newItem;
|
||||
//xrSocketInteractor.attachTransform = _itemInSlot.transform;
|
||||
Debug.Log("Holstered item" + _itemInSlot.GetComponentInChildren<ItemData>());
|
||||
}
|
||||
|
||||
private void ManagerOninteractableUnregistered(InteractableUnregisteredEventArgs obj)
|
||||
{
|
||||
Debug.Log("Removing object "+ obj);
|
||||
Destroy(obj.interactableObject.transform.gameObject);
|
||||
}
|
||||
|
||||
private void SelectExit(SelectExitEventArgs args)
|
||||
{
|
||||
if (_itemInSlot == null) return;
|
||||
if (_itemInSlot.GetComponent<InventoryItem>() == null) return;
|
||||
|
||||
var component = _itemInSlot.GetComponent<Rigidbody>();
|
||||
component.useGravity = true;
|
||||
component.angularDrag = 2f;
|
||||
component.drag = 0.5f;
|
||||
|
||||
if (!_itemInSlot.GetComponentInChildren<ItemData>().canStack ||
|
||||
_itemInSlot.GetComponent<InventoryItem>().Count == 1)
|
||||
{
|
||||
//var transformGameObject = _itemInSlot.GetComponentInChildren<ItemData>().transform.gameObject;
|
||||
//Debug.Log(transformGameObject);
|
||||
//args.interactableObject = transformGameObject.GetComponent<XRGrabInteractable>();
|
||||
//todo change back to regular item
|
||||
}
|
||||
Debug.Log("Removed from slot item " + _itemInSlot);
|
||||
_itemInSlot = null;
|
||||
}
|
||||
|
||||
public void SetParentInventory(Inventory inventory)
|
||||
{
|
||||
_parentInventory = inventory;
|
||||
}
|
||||
|
||||
public bool ContainsItem()
|
||||
{
|
||||
return _itemInSlot != null;
|
||||
}
|
||||
|
||||
public bool ContainsItem(int id)
|
||||
{
|
||||
if (!ContainsItem()) return false;
|
||||
|
||||
|
||||
var inventoryItem = _itemInSlot.GetComponent<InventoryItem>();
|
||||
if (inventoryItem != null) return inventoryItem.GetItemid() == id;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void AssignItem(GameObject newObject)
|
||||
{
|
||||
_itemInSlot = newObject;
|
||||
}
|
||||
|
||||
public InventoryItem GetItem()
|
||||
{
|
||||
var inventoryItem = _itemInSlot.GetComponent<InventoryItem>();
|
||||
if (inventoryItem == null) Debug.LogError("Item in slot doesn't have InventoryItem component");
|
||||
return inventoryItem;
|
||||
}
|
||||
}
|
||||
11
Assets/Project Files/Scripts/Helar/InventorySlot.cs.meta
Normal file
11
Assets/Project Files/Scripts/Helar/InventorySlot.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aca2b5bf18c279c4cb265c82010714b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,6 +9,7 @@ public class ItemSpawner : MonoBehaviour
|
||||
public GameObject placeToSpawn;
|
||||
public void SpawnItem()
|
||||
{
|
||||
//Debug.Log("Spawned " + objectToSpawn);
|
||||
Instantiate(objectToSpawn, placeToSpawn.transform);
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/Project Files/Scripts/Helar/Items.meta
Normal file
8
Assets/Project Files/Scripts/Helar/Items.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c509c0c7d8d7f2942a05792798d31fc0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
115
Assets/Project Files/Scripts/Helar/Items/InventoryItem.cs
Normal file
115
Assets/Project Files/Scripts/Helar/Items/InventoryItem.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using Unity.Mathematics;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
[ExecuteAlways]
|
||||
public class InventoryItem : MonoBehaviour
|
||||
{
|
||||
|
||||
public GameObject itemPrefab;
|
||||
|
||||
private int _itemId;
|
||||
|
||||
private GameObject _childPrefab;
|
||||
|
||||
public int Count { get; private set; }
|
||||
|
||||
public TMP_Text itemCounterText;
|
||||
|
||||
private ItemData _itemData;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (itemPrefab == null)
|
||||
{
|
||||
enabled = false;
|
||||
//Debug.LogError("Item has no attached prefab");
|
||||
return;
|
||||
}
|
||||
|
||||
enabled = true;
|
||||
|
||||
if (itemCounterText == null) itemCounterText = GetComponentInChildren<TMP_Text>();
|
||||
|
||||
_childPrefab = GetComponentInChildren<ItemData>()?.gameObject;
|
||||
//Only attaches the prefab if it doesn't already have one ([ExecuteAlways] causes one to be spawned in editor).
|
||||
if (_childPrefab != null) return;
|
||||
|
||||
_childPrefab = Instantiate(itemPrefab, transform);
|
||||
_childPrefab.transform.localPosition = new Vector3(0, -0.45f, 0);
|
||||
_childPrefab.transform.localScale = new Vector3(2, 2, 2);
|
||||
_childPrefab.transform.rotation = quaternion.identity;
|
||||
|
||||
//Disable prefab components that arent relevant.
|
||||
var colliderComponent = _childPrefab.GetComponent<Collider>();
|
||||
if (colliderComponent != null) colliderComponent.enabled = false;
|
||||
|
||||
var rigidbodyComponent = _childPrefab.GetComponent<Rigidbody>();
|
||||
if (rigidbodyComponent != null)
|
||||
{
|
||||
rigidbodyComponent.isKinematic = true;
|
||||
rigidbodyComponent.useGravity = false;
|
||||
}
|
||||
|
||||
var xrGrabInteractableComponent = _childPrefab.GetComponent<XRGrabInteractable>();
|
||||
if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = false;
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_itemData = _childPrefab.GetComponent<ItemData>();
|
||||
|
||||
if (_itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab);
|
||||
|
||||
|
||||
_itemId = _itemData.itemId;
|
||||
if (Count == 0) Count = 1;
|
||||
UpdateText();
|
||||
}
|
||||
|
||||
private void UpdateText()
|
||||
{
|
||||
itemCounterText.text = _itemData.canStack ? Count.ToString() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes how many items are in this itemcontainer.
|
||||
* Returns false if attempted to remove more items than exists in the container.
|
||||
*/
|
||||
public bool ChangeCount(int amount)
|
||||
{
|
||||
if (Count + amount < 0) return false;
|
||||
|
||||
Count += amount;
|
||||
|
||||
if (Count == 0)
|
||||
{
|
||||
//Destroys this gameobject if the count is 0 or lower
|
||||
Destroy(this);
|
||||
return true;
|
||||
}
|
||||
UpdateText();
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetItemid()
|
||||
{
|
||||
return _itemId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{base.ToString()}, Count: {Count}, ItemData: {_itemData}";
|
||||
}
|
||||
}
|
||||
18
Assets/Project Files/Scripts/Helar/Items/ItemData.cs
Normal file
18
Assets/Project Files/Scripts/Helar/Items/ItemData.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class ItemData : MonoBehaviour
|
||||
{
|
||||
public int itemId;
|
||||
public bool canStack;
|
||||
public string itemName;
|
||||
public string itemDescription;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{base.ToString()}, itemId: {itemId}, canStack: {canStack}, itemName: {itemName}";
|
||||
}
|
||||
}
|
||||
11
Assets/Project Files/Scripts/Helar/Items/ItemData.cs.meta
Normal file
11
Assets/Project Files/Scripts/Helar/Items/ItemData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc417ab3a5b06f04db2149be4cfdf6e4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user