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

@@ -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}";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e6bef3249ab68fd4184bea62f15c461f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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}";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fc417ab3a5b06f04db2149be4cfdf6e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: