miepzerino
2025-04-01 f5e15fa93d84acbae6a26b86fddf20add38bb485
#31 added inventory to savefile, added itemDatabase
3 files modified
3 files renamed
2 files added
1 files deleted
2 files copied
158 ■■■■ changed files
Assets/Scripts/Inventory/Inventory.cs 25 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Inventory/Item.cs 1 ●●●● patch | view | raw | blame | history
Assets/Scripts/Inventory/ItemDatabase.cs 75 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Inventory/ItemDatabase.cs.meta 2 ●●● patch | view | raw | blame | history
Assets/Scripts/PauseMenu.cs 3 ●●●● patch | view | raw | blame | history
Assets/Scripts/SaveData.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Saving.meta 8 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Saving/SaveData.cs 29 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Saving/SaveData.cs.meta 2 ●●● patch | view | raw | blame | history
Assets/Scripts/Saving/SaveSystem.cs patch | view | raw | blame | history
Assets/Scripts/Saving/SaveSystem.cs.meta 2 ●●● patch | view | raw | blame | history
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)
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;
Assets/Scripts/Inventory/ItemDatabase.cs
New file
@@ -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;
    }
}
Assets/Scripts/Inventory/ItemDatabase.cs.meta
File was renamed from Assets/Scripts/SaveSystem.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 48b32dceea7ccc1499e20f72c9af2d85
guid: e4d1fabb0e4577b4d987741f664e3d68
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
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");
Assets/Scripts/SaveData.cs.meta
File was deleted
Assets/Scripts/Saving.meta
New file
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6a565c28b8754cf498ef449b3ae393bd
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Scripts/Saving/SaveData.cs
File was renamed from Assets/Scripts/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
Assets/Scripts/Saving/SaveData.cs.meta
copy from Assets/Scripts/SaveSystem.cs.meta copy to Assets/Scripts/Saving/SaveData.cs.meta
File was copied from Assets/Scripts/SaveSystem.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 48b32dceea7ccc1499e20f72c9af2d85
guid: 7564cb159a31f9348963c99d3b099315
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
Assets/Scripts/Saving/SaveSystem.cs
Assets/Scripts/Saving/SaveSystem.cs.meta
copy from Assets/Scripts/SaveSystem.cs.meta copy to Assets/Scripts/Saving/SaveSystem.cs.meta
File was copied from Assets/Scripts/SaveSystem.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 48b32dceea7ccc1499e20f72c9af2d85
guid: cd24cd46198c6894eb18e9953978cb7e
MonoImporter:
  externalObjects: {}
  serializedVersion: 2