Basic saving, currently hardcoded to the one "chest" inventory in HomeBase.

Loads when scene name contains "Homebase" and saves when button pressed.
This commit is contained in:
HelarJ
2022-04-18 08:59:26 +03:00
parent be8b9a987d
commit b1954dd40e
34 changed files with 39886 additions and 306 deletions

View File

@@ -1,17 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
[Serializable]
public class Inventory : MonoBehaviour
{
//perhaps use these additional slots for holsters attached to the player?
public List<InventorySlot> additionalSlots;
public GameObject itemContainerPrefab;
private List<InventorySlot> _inventorySlots;
public InventorySaveData inventorySaveData;
private void Awake()
{
_inventorySlots = new List<InventorySlot>();
@@ -20,10 +25,15 @@ public class Inventory : MonoBehaviour
inventorySlot.SetParentInventory(this);
_inventorySlots.Add(inventorySlot);
}
Debug.Log(_inventorySlots.Count);
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
var inventorySlotsInChildren = GetComponentsInChildren<InventorySlot>();
foreach (var inventorySlot in inventorySlotsInChildren)
{
@@ -32,6 +42,12 @@ public class Inventory : MonoBehaviour
}
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
public bool AddItem(ItemData item)
{
if (item.canStack)
@@ -39,16 +55,17 @@ public class Inventory : MonoBehaviour
return AddItem(ConvertToInventoryItem(item));
}
return AddToFirstOpenSlot(item.gameObject);
return AddToFirstOpenSlot(item.prefab.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);
inventorySlot.GetItem().ChangeCount(item.saveData.count);
return true;
}
@@ -67,11 +84,91 @@ public class Inventory : MonoBehaviour
return false;
}
private void AddToSpecificSlot(GameObject item, int slot)
{
_inventorySlots[slot].AssignItem(item);
}
private InventoryItem ConvertToInventoryItem(ItemData item)
{
var inventoryItem = Instantiate(itemContainerPrefab, transform);
itemContainerPrefab.GetComponent<InventoryItem>().itemPrefab = item.gameObject;
itemContainerPrefab.GetComponent<InventoryItem>().itemPrefab = item.prefab.gameObject;
return inventoryItem.gameObject.GetComponent<InventoryItem>();
}
public void SaveInventory()
{
inventorySaveData.inventoryContents.Clear();
foreach (var inventoryItem in _inventorySlots.Select(inventorySlot => inventorySlot.GetItem()))
{
//adds an itemsavedata to the list if it exists or an empty one if it doesnt
inventorySaveData.inventoryContents.Add(inventoryItem != null
? inventoryItem.saveData
: new ItemSaveData());
}
var json = JsonUtility.ToJson(inventorySaveData);
Debug.Log(json);
using var sw = new StreamWriter(Path.Combine(Application.persistentDataPath, "invSave.dat"));
sw.Write(json);
Debug.Log("Inv data saved!");
}
public IEnumerator LoadInventory()
{
//Wait for 1 second after loading the scene to prevent missing prefabs. (1 second delay might also break something in the future)
yield return new WaitForSeconds(1);
//todo make this modular, currently only supports one inventory, although the slots can be anywhere.
if (File.Exists(Path.Combine(Application.persistentDataPath, "invSave.dat")))
{
var saveData = JsonUtility.FromJson<InventorySaveData>(File.ReadAllText(Path.Combine(Application.persistentDataPath, "invSave.dat")));
Debug.Log(saveData);
Debug.Log("Inv data loaded! total "+saveData.inventoryContents.Count);
for (var i = 0; i < saveData.inventoryContents.Count; i++)
{
//prevents overwriting inventory slots when there is something already in there.
//if (_inventorySlots[i]._itemInSlot != null) continue;
var data = saveData.inventoryContents[i];
//if there isnt an item in the slot the count is 0
Debug.Log(data.assetPath + " "+data.count);
if (data.count == 0) continue;
//Loads the prefab from resources folder
var load = Resources.Load(data.assetPath, typeof(GameObject)) as GameObject;
if (load == null)
{
Debug.LogError("Didn't find a prefab at " + data.assetPath);
yield break;
}
Debug.Log(load);
AddToSpecificSlot(load, i);
//sets the count after the item has been converted into inventoryitem
if (load.GetComponent<ItemData>().canStack) _inventorySlots[i]._itemInSlot.GetComponent<InventoryItem>().saveData.count = data.count;
}
}
else
Debug.LogError("There is no save data!");
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name);
//primitive check to only load the inventory when you are in the home scene.
if (scene.name.Contains("Homebase")) StartCoroutine(LoadInventory());
}
}

View File

@@ -4,16 +4,14 @@ 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;
public GameObject _itemInSlot;
private Inventory _parentInventory;
private void Awake()
{
xrSocketInteractor = GetComponent<XRSocketInteractor>();
@@ -35,6 +33,7 @@ public class InventorySlot : MonoBehaviour
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;
@@ -47,7 +46,7 @@ public class InventorySlot : MonoBehaviour
if (inventoryItem != null)
{
_itemInSlot.GetComponent<InventoryItem>().ChangeCount(inventoryItem.Count);
_itemInSlot.GetComponent<InventoryItem>().ChangeCount(inventoryItem.saveData.count);
}
else
{
@@ -70,11 +69,13 @@ public class InventorySlot : MonoBehaviour
private void SelectEnter(SelectEnterEventArgs args)
{
Debug.Log("Added to slot item " + _itemInSlot);
var newItem = args.interactableObject.transform.gameObject;
//todo: fix ones that cant stack
//Converts the item into a inventory item
if (newItem.GetComponent<InventoryItem>() == null && newItem.GetComponent<ItemData>().canStack)
if (newItem.GetComponent<InventoryItem>() == null)
{
var load = Resources.Load("Helar/Item", typeof(GameObject)) as GameObject;
@@ -101,12 +102,39 @@ public class InventorySlot : MonoBehaviour
_itemInSlot = instance;
//_parentInventory.AddItem(instance.GetComponent<InventoryItem>());
return;
}
_itemInSlot = newItem;
//_parentInventory.AddItem(newItem.GetComponentInChildren<ItemData>());
//xrSocketInteractor.attachTransform = _itemInSlot.transform;
Debug.Log("Holstered item" + _itemInSlot.GetComponentInChildren<ItemData>());
}
private GameObject ConvertItem(GameObject item)
{
//load container object
var load = Resources.Load("Helar/Item", typeof(GameObject)) as GameObject;
//instantiate the container object
var instance = Instantiate(load, transform);
//load the container with the item to convert
instance.GetComponent<InventoryItem>().itemPrefab = item.gameObject;
//set the position and scale so it fits in the container
instance.transform.localScale = Vector3.one;
instance.transform.localPosition = Vector3.zero;
instance.transform.rotation = Quaternion.identity;
//enable the itemcontainer (more logic in InventoryItem OnEnable() method)
instance.GetComponent<InventoryItem>().enabled = true;
return instance;
}
private void ManagerOninteractableUnregistered(InteractableUnregisteredEventArgs obj)
@@ -126,12 +154,14 @@ public class InventorySlot : MonoBehaviour
component.drag = 0.5f;
if (!_itemInSlot.GetComponentInChildren<ItemData>().canStack ||
_itemInSlot.GetComponent<InventoryItem>().Count == 1)
_itemInSlot.GetComponent<InventoryItem>().saveData.count == 1)
{
//todo change back to regular item
//_itemInSlot.GetComponent<InventoryItem>().PopItem();
//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;
@@ -160,11 +190,20 @@ public class InventorySlot : MonoBehaviour
public void AssignItem(GameObject newObject)
{
_itemInSlot = newObject;
if (newObject.GetComponent<InventoryItem>() == null)
{
_itemInSlot = ConvertItem(newObject);
}
else
{
_itemInSlot = newObject;
}
}
public InventoryItem GetItem()
{
if (_itemInSlot == null) return null;
var inventoryItem = _itemInSlot.GetComponent<InventoryItem>();
if (inventoryItem == null) Debug.LogError("Item in slot doesn't have InventoryItem component");
return inventoryItem;

View File

@@ -9,7 +9,7 @@ public class ItemSpawner : MonoBehaviour
public GameObject placeToSpawn;
public void SpawnItem()
{
Debug.Log("Spawned " + objectToSpawn);
Instantiate(objectToSpawn, placeToSpawn.transform);
Debug.Log("Spawned " + objectToSpawn);
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
@@ -13,15 +12,13 @@ public class InventoryItem : MonoBehaviour
public GameObject itemPrefab;
private int _itemId;
private GameObject _childPrefab;
public int Count { get; private set; }
private TMP_Text itemCounterText;
public TMP_Text itemCounterText;
private ItemData itemData;
private ItemData _itemData;
public ItemSaveData saveData;
private void OnEnable()
@@ -37,13 +34,15 @@ public class InventoryItem : MonoBehaviour
if (itemCounterText == null) itemCounterText = GetComponentInChildren<TMP_Text>();
_childPrefab = GetComponentInChildren<ItemData>()?.gameObject;
_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);
_childPrefab.transform.localPosition = new Vector3(0, -0.45f, 0);
_childPrefab.transform.localScale = new Vector3(2, 2, 2);
//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.
@@ -59,24 +58,29 @@ public class InventoryItem : MonoBehaviour
var xrGrabInteractableComponent = _childPrefab.GetComponent<XRGrabInteractable>();
if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = false;
}
private void Start()
{
_itemData = _childPrefab.GetComponent<ItemData>();
itemData = _childPrefab.GetComponent<ItemData>();
if (_itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab);
_itemId = _itemData.itemId;
if (Count == 0) Count = 1;
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 ? Count.ToString() : "";
itemCounterText.text = itemData.canStack ? saveData.count.ToString() : "";
}
/**
@@ -85,11 +89,11 @@ public class InventoryItem : MonoBehaviour
*/
public bool ChangeCount(int amount)
{
if (Count + amount < 0) return false;
if (saveData.count + amount < 0) return false;
Count += amount;
saveData.count += amount;
if (Count == 0)
if (saveData.count == 0)
{
//Destroys this gameobject if the count is 0 or lower
Destroy(gameObject);
@@ -103,10 +107,11 @@ public class InventoryItem : MonoBehaviour
public void PopItem()
{
GameObject item = Instantiate(_itemData.prefab, transform.position + transform.forward, Quaternion.identity);
GameObject item = Instantiate(itemData.prefab, transform.position + transform.forward, Quaternion.identity);
item.transform.localScale = Vector3.one;
item.transform.name = _itemData.name;
item.transform.name = itemData.itemName;
var colliderComponent = item.GetComponent<Collider>();
if (colliderComponent != null) colliderComponent.enabled = true;
@@ -127,13 +132,13 @@ public class InventoryItem : MonoBehaviour
public int GetItemid()
{
return _itemId;
return itemData.itemId;;
}
public override string ToString()
{
return $"{base.ToString()}, Count: {Count}, ItemData: {_itemData}";
return $"{base.ToString()}, Count: {saveData.count}, ItemData: {itemData}";
}
}

View File

@@ -0,0 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class InventorySaveData
{
public List<ItemSaveData> inventoryContents;
}

View File

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

View File

@@ -1,16 +1,44 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[Serializable]
public class ItemData : MonoBehaviour
{
[Tooltip("Location is populated automatically")]
public string assetLocation;
public int itemId;
public bool canStack;
public string itemName;
public string itemDescription;
public GameObject prefab;
public Vector3 scaleInContainer;
public Vector3 positionInContainer;
private void OnValidate()
{
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(prefab))) assetLocation = AssetDatabase.GetAssetPath(prefab);
AdjustPath();
}
private void AdjustPath()
{
//trims the location for use with Resources.Load later
if (assetLocation.StartsWith("Assets/Resources/"))
assetLocation = assetLocation[17 .. ^7];
else if (!string.IsNullOrEmpty(assetLocation))
if (assetLocation.StartsWith("Assets"))
{
Debug.LogError(itemName +
": item with this script should be placed in the Assets/Resources/ folder. Currently: " +
assetLocation);
}
}
public override string ToString()
{

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class ItemSaveData
{
public string assetPath;
public int count;
}

View File

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