HelarJ b1954dd40e Basic saving, currently hardcoded to the one "chest" inventory in HomeBase.
Loads when scene name contains "Homebase" and saves when button pressed.
2022-04-18 08:59:26 +03:00

145 lines
4.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
[ExecuteAlways]
public class InventoryItem : MonoBehaviour
{
public GameObject itemPrefab;
private GameObject _childPrefab;
private TMP_Text itemCounterText;
private ItemData itemData;
public ItemSaveData saveData;
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>()?.prefab.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);
//todo switch to multiply instead so items with non 1,1,1 scale will retain their proportions
_childPrefab.transform.localPosition = GetComponentInChildren<ItemData>().positionInContainer;
_childPrefab.transform.localScale = GetComponentInChildren<ItemData>().scaleInContainer;
_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);
Debug.Log(saveData);
if (string.IsNullOrEmpty(saveData.assetPath))
{
saveData.assetPath = itemData.assetLocation;
saveData.count = saveData.count == 0 ? 1 : saveData.count;
}
UpdateText();
}
private void UpdateText()
{
itemCounterText.text = itemData.canStack ? saveData.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 (saveData.count + amount < 0) return false;
saveData.count += amount;
if (saveData.count == 0)
{
//Destroys this gameobject if the count is 0 or lower
Destroy(gameObject);
return true;
}
UpdateText();
return true;
}
public void PopItem()
{
GameObject item = Instantiate(itemData.prefab, transform.position + transform.forward, Quaternion.identity);
item.transform.localScale = Vector3.one;
item.transform.name = itemData.itemName;
var colliderComponent = item.GetComponent<Collider>();
if (colliderComponent != null) colliderComponent.enabled = true;
var rigidbodyComponent = item.GetComponent<Rigidbody>();
if (rigidbodyComponent != null)
{
rigidbodyComponent.isKinematic = false;
rigidbodyComponent.useGravity = true;
}
var xrGrabInteractableComponent = item.GetComponent<XRGrabInteractable>();
if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = true;
ChangeCount(-1);
}
public int GetItemid()
{
return itemData.itemId;;
}
public override string ToString()
{
return $"{base.ToString()}, Count: {saveData.count}, ItemData: {itemData}";
}
}