From f5e15fa93d84acbae6a26b86fddf20add38bb485 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Tue, 01 Apr 2025 13:21:41 +0000
Subject: [PATCH] #31 added inventory to savefile, added itemDatabase
---
/dev/null | 11 ---
Assets/Scripts/Inventory/ItemDatabase.cs | 75 +++++++++++++++++++++++++
Assets/Scripts/Inventory/Inventory.cs | 25 ++++++++
Assets/Scripts/Saving.meta | 8 ++
Assets/Scripts/Saving/SaveSystem.cs | 0
Assets/Scripts/Saving/SaveSystem.cs.meta | 2
Assets/Scripts/Saving/SaveData.cs.meta | 2
Assets/Scripts/Inventory/ItemDatabase.cs.meta | 2
Assets/Scripts/PauseMenu.cs | 3
Assets/Scripts/Saving/SaveData.cs | 29 +++++++++
Assets/Scripts/Inventory/Item.cs | 1
11 files changed, 142 insertions(+), 16 deletions(-)
diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs
index edb224e..f622519 100644
--- a/Assets/Scripts/Inventory/Inventory.cs
+++ b/Assets/Scripts/Inventory/Inventory.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -18,6 +19,30 @@
{
Destroy(gameObject);
}
+ if (SaveSystem.isGameLoaded)
+ {
+ LoadInventory();
+ }
+ }
+
+ private void LoadInventory()
+ {
+ SaveDataPlayer save = SaveSystem.LoadPlayer();
+ if (save != null)
+ {
+ foreach (SaveDataInventorySlot item in save.inventoryItems)
+ {
+ Item loadedItem = ItemDatabase.Instance.GetItem(item.itemId);
+ if (loadedItem != null)
+ {
+ items.Add(new InventorySlot(loadedItem, item.quantity));
+ }
+ else
+ {
+ Debug.LogError($"Failed to load item with ID: {item.itemId}");
+ }
+ }
+ }
}
public bool AddItem(Item item, int quantity = 1)
diff --git a/Assets/Scripts/Inventory/Item.cs b/Assets/Scripts/Inventory/Item.cs
index 59f44d7..6d0871d 100644
--- a/Assets/Scripts/Inventory/Item.cs
+++ b/Assets/Scripts/Inventory/Item.cs
@@ -5,6 +5,7 @@
[System.Serializable]
public class Item : MonoBehaviour
{
+ public int itemId;
public string itemName;
public Sprite itemIcon;
public string itemDescription;
diff --git a/Assets/Scripts/Inventory/ItemDatabase.cs b/Assets/Scripts/Inventory/ItemDatabase.cs
new file mode 100644
index 0000000..c0a0067
--- /dev/null
+++ b/Assets/Scripts/Inventory/ItemDatabase.cs
@@ -0,0 +1,75 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ItemDatabase : MonoBehaviour
+{
+ public static ItemDatabase Instance { get; private set; }
+
+ [SerializeField]
+ private List<Item> items = new List<Item>();
+
+ private Dictionary<string, Item> itemDictionary = new Dictionary<string, Item>();
+ private Dictionary<int, Item> itemIdDictionary = new Dictionary<int, Item>();
+
+ private void Awake()
+ {
+ if (Instance == null)
+ {
+ Instance = this;
+ InitializeItemDictionary();
+ }
+ else
+ {
+ Destroy(gameObject);
+ }
+ }
+
+ private void InitializeItemDictionary()
+ {
+ itemDictionary.Clear();
+ itemIdDictionary.Clear();
+
+ foreach (Item item in items)
+ {
+ if (!itemDictionary.ContainsKey(item.itemName))
+ {
+ itemDictionary.Add(item.itemName, item);
+ }
+ else
+ {
+ Debug.LogError($"Duplicate item name found in ItemDatabase: {item.itemName}");
+ }
+
+ if (!itemIdDictionary.ContainsKey(item.itemId))
+ {
+ itemIdDictionary.Add(item.itemId, item);
+ }
+ else
+ {
+ Debug.LogError($"Duplicate item ID found in ItemDatabase: {item.itemId}");
+ }
+ }
+ }
+
+ public Item GetItem(string itemName)
+ {
+ if (itemDictionary.TryGetValue(itemName, out Item item))
+ {
+ return item;
+ }
+
+ Debug.LogWarning($"Item not found in database: {itemName}");
+ return null;
+ }
+
+ public Item GetItem(int itemId)
+ {
+ if (itemIdDictionary.TryGetValue(itemId, out Item item))
+ {
+ return item;
+ }
+
+ Debug.LogWarning($"Item not found in database: ID {itemId}");
+ return null;
+ }
+}
diff --git a/Assets/Scripts/SaveData.cs.meta b/Assets/Scripts/Inventory/ItemDatabase.cs.meta
similarity index 83%
rename from Assets/Scripts/SaveData.cs.meta
rename to Assets/Scripts/Inventory/ItemDatabase.cs.meta
index c14f02e..705d7cf 100644
--- a/Assets/Scripts/SaveData.cs.meta
+++ b/Assets/Scripts/Inventory/ItemDatabase.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 4053217b507f2c34991a5d799a8ceeb6
+guid: e4d1fabb0e4577b4d987741f664e3d68
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Assets/Scripts/PauseMenu.cs b/Assets/Scripts/PauseMenu.cs
index 7227f6f..c995f60 100644
--- a/Assets/Scripts/PauseMenu.cs
+++ b/Assets/Scripts/PauseMenu.cs
@@ -62,7 +62,8 @@
public void OnGameSaveClicked()
{
PlayerController playerController = GameObject.Find("Player").GetComponent<PlayerController>();
- SaveSystem.SavePlayer(new SaveDataPlayer(playerController));
+ Inventory playerInventory = GameObject.Find("Player").GetComponent<Inventory>();
+ SaveSystem.SavePlayer(new SaveDataPlayer(playerController, playerInventory));
GameManager gameManager= GameObject.Find("GameManager").GetComponent<GameManager>();
SaveSystem.SaveMapState(new SaveDataMap(gameManager.destroyedTiles, gameManager.generateTileMap.seed.Value));
animator.SetTrigger("GameSaved");
diff --git a/Assets/Scripts/SaveSystem.cs.meta b/Assets/Scripts/SaveSystem.cs.meta
deleted file mode 100644
index dd3f492..0000000
--- a/Assets/Scripts/SaveSystem.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 48b32dceea7ccc1499e20f72c9af2d85
-MonoImporter:
- externalObjects: {}
- serializedVersion: 2
- defaultReferences: []
- executionOrder: 0
- icon: {instanceID: 0}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Assets/Scripts/Saving.meta b/Assets/Scripts/Saving.meta
new file mode 100644
index 0000000..e9a0c8c
--- /dev/null
+++ b/Assets/Scripts/Saving.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 6a565c28b8754cf498ef449b3ae393bd
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Scripts/SaveData.cs b/Assets/Scripts/Saving/SaveData.cs
similarity index 60%
rename from Assets/Scripts/SaveData.cs
rename to Assets/Scripts/Saving/SaveData.cs
index 5ee1f34..0bf35d0 100644
--- a/Assets/Scripts/SaveData.cs
+++ b/Assets/Scripts/Saving/SaveData.cs
@@ -4,6 +4,21 @@
using System.Collections.Generic;
using UnityEngine;
+#region player data
+[Serializable]
+public class SaveDataInventorySlot
+{
+ public int itemId;
+ public string itemName;
+ public int quantity;
+
+ public SaveDataInventorySlot(InventorySlot slot)
+ {
+ itemId = slot.item.itemId;
+ itemName = slot.item.itemName;
+ quantity = slot.quantity;
+ }
+}
[Serializable]
public class SaveDataPlayer
{
@@ -11,15 +26,26 @@
public int health;
public float[] position;
public float[] velocity;
- public SaveDataPlayer(PlayerController player)
+ public List<SaveDataInventorySlot> inventoryItems;
+
+ public SaveDataPlayer(PlayerController player, Inventory inventory)
{
maxHealth = player.health.MaxHealth;
health = player.health.Health;
position = player.transform.position.ConvertToFloatArray();
velocity = player.rb.velocity.ConvertToFloatArray();
+
+ // Save inventory items
+ inventoryItems = new List<SaveDataInventorySlot>();
+ foreach (var slot in inventory.items)
+ {
+ inventoryItems.Add(new SaveDataInventorySlot(slot));
+ }
}
}
+#endregion
+#region map data
[Serializable]
public class SaveDataMap
{
@@ -45,3 +71,4 @@
}
public int[] tileCoord;
}
+#endregion
diff --git a/Assets/Scripts/SaveData.cs.meta b/Assets/Scripts/Saving/SaveData.cs.meta
similarity index 83%
copy from Assets/Scripts/SaveData.cs.meta
copy to Assets/Scripts/Saving/SaveData.cs.meta
index c14f02e..ba75aac 100644
--- a/Assets/Scripts/SaveData.cs.meta
+++ b/Assets/Scripts/Saving/SaveData.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 4053217b507f2c34991a5d799a8ceeb6
+guid: 7564cb159a31f9348963c99d3b099315
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Assets/Scripts/SaveSystem.cs b/Assets/Scripts/Saving/SaveSystem.cs
similarity index 100%
rename from Assets/Scripts/SaveSystem.cs
rename to Assets/Scripts/Saving/SaveSystem.cs
diff --git a/Assets/Scripts/SaveData.cs.meta b/Assets/Scripts/Saving/SaveSystem.cs.meta
similarity index 83%
copy from Assets/Scripts/SaveData.cs.meta
copy to Assets/Scripts/Saving/SaveSystem.cs.meta
index c14f02e..b8fe8e7 100644
--- a/Assets/Scripts/SaveData.cs.meta
+++ b/Assets/Scripts/Saving/SaveSystem.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 4053217b507f2c34991a5d799a8ceeb6
+guid: cd24cd46198c6894eb18e9953978cb7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
--
Gitblit v1.10.0