using Assets.Scripts.Helpers;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using UnityEngine;
|
using UnityEngine.Tilemaps;
|
|
#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
|
{
|
public int maxHealth;
|
public int health;
|
public float[] position;
|
public float[] velocity;
|
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
|
{
|
public int seed;
|
public List<DestroyedTile> destroyedTiles;
|
public FogOfWarData fogOfWarData;
|
public List<SerializedChunkData> chunkData; // New field
|
public SaveDataMap(int seed, List<Vector3Int> destroyedTiles, Dictionary<Vector2Int, TileBase[]> chunkCache, FogOfWarData fogOfWarData)
|
{
|
this.seed = seed;
|
this.destroyedTiles = new List<DestroyedTile>();
|
this.fogOfWarData = fogOfWarData;
|
foreach (var item in destroyedTiles.ConvertToListIntArray())
|
{
|
this.destroyedTiles.Add(new DestroyedTile(item));
|
}
|
// Convert chunk cache to serializable format
|
this.chunkData = chunkCache.Select(kvp => new SerializedChunkData(kvp.Key, kvp.Value)).ToList();
|
}
|
}
|
|
[Serializable]
|
public class DestroyedTile
|
{
|
public DestroyedTile(int[] tileCoord)
|
{
|
this.tileCoord = tileCoord;
|
}
|
public int[] tileCoord;
|
}
|
|
[System.Serializable]
|
public class FogTileData
|
{
|
public int x;
|
public int y;
|
public int fogLevelIndex; // -1 for no fog, 0+ for fog level index
|
}
|
|
[System.Serializable]
|
public class FogOfWarData
|
{
|
public List<FogTileData> discoveredTiles = new List<FogTileData>();
|
}
|
[System.Serializable]
|
public class SerializedChunkData
|
{
|
public Vector2Int position;
|
public string[] tileNames; // Store tile names instead of TileBase references
|
|
public SerializedChunkData(Vector2Int pos, TileBase[] tiles)
|
{
|
position = pos;
|
tileNames = tiles.Select(t => t != null ? t.name : "null").ToArray();
|
}
|
}
|
#endregion
|