From 6b1abe7afb9350a7890f6b893f59f72a3988aaab Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Mon, 07 Apr 2025 16:15:36 +0000
Subject: [PATCH] #50 added saveDataManager
---
Assets/Scripts/Controls/PlayerController.cs | 2
Assets/Scripts/Inventory/Inventory.cs | 2
Assets/Scripts/Managers/GameManager.cs | 10 +-
Assets/Scripts/Managers/SaveSystemManager.cs | 102 +++++++++++++++++++++++++
Assets/Scenes/GameplayScene.unity | 24 +++++
Assets/Scripts/Managers/SaveSystemManager.cs.meta | 11 ++
Assets/Scripts/GenerateTileMap.cs | 10 +-
Assets/Scripts/Saving/SaveData.cs | 3
Assets/Scripts/UI/PauseMenu.cs | 6 -
Assets/Scripts/FogOfWar.cs | 28 +------
10 files changed, 153 insertions(+), 45 deletions(-)
diff --git a/Assets/Scenes/GameplayScene.unity b/Assets/Scenes/GameplayScene.unity
index 88982ad..1d75531 100644
--- a/Assets/Scenes/GameplayScene.unity
+++ b/Assets/Scenes/GameplayScene.unity
@@ -5473,19 +5473,39 @@
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
- m_AddedComponents: []
+ m_AddedComponents:
+ - targetCorrespondingSourceObject: {fileID: 8598998496262044661, guid: 7296d9a2424531f4ba42c0c75e9c48a0, type: 3}
+ insertIndex: -1
+ addedObject: {fileID: 75655679957548993}
m_SourcePrefab: {fileID: 100100000, guid: 7296d9a2424531f4ba42c0c75e9c48a0, type: 3}
--- !u!114 &75655679957548991 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 6908574976455911116, guid: 7296d9a2424531f4ba42c0c75e9c48a0, type: 3}
m_PrefabInstance: {fileID: 75655679957548990}
m_PrefabAsset: {fileID: 0}
- m_GameObject: {fileID: 0}
+ m_GameObject: {fileID: 75655679957548992}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ee189283a861cae42b728253b8229316, type: 3}
m_Name:
m_EditorClassIdentifier:
+--- !u!1 &75655679957548992 stripped
+GameObject:
+ m_CorrespondingSourceObject: {fileID: 8598998496262044661, guid: 7296d9a2424531f4ba42c0c75e9c48a0, type: 3}
+ m_PrefabInstance: {fileID: 75655679957548990}
+ m_PrefabAsset: {fileID: 0}
+--- !u!114 &75655679957548993
+MonoBehaviour:
+ m_ObjectHideFlags: 0
+ m_CorrespondingSourceObject: {fileID: 0}
+ m_PrefabInstance: {fileID: 0}
+ m_PrefabAsset: {fileID: 0}
+ m_GameObject: {fileID: 75655679957548992}
+ m_Enabled: 1
+ m_EditorHideFlags: 0
+ m_Script: {fileID: 11500000, guid: 00c47706c533aee4890c37a8bd5ea5f8, type: 3}
+ m_Name:
+ m_EditorClassIdentifier:
--- !u!223 &90387968388784992
Canvas:
m_ObjectHideFlags: 0
diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs
index 2b3b2db..58a9e21 100644
--- a/Assets/Scripts/Controls/PlayerController.cs
+++ b/Assets/Scripts/Controls/PlayerController.cs
@@ -172,7 +172,7 @@
private void LoadPlayer()
{
- SaveDataPlayer save = SaveSystem.LoadPlayer();
+ SaveDataPlayer save = SaveSystemManager.Instance.GetPlayerStateFromSave();
health.MaxHealth = save.maxHealth;
health.Health = save.health;
transform.position = VectorHelper.ConvertToVector3(save.position);
diff --git a/Assets/Scripts/FogOfWar.cs b/Assets/Scripts/FogOfWar.cs
index 251c3ce..4080304 100644
--- a/Assets/Scripts/FogOfWar.cs
+++ b/Assets/Scripts/FogOfWar.cs
@@ -61,10 +61,10 @@
if (SaveSystem.isGameLoaded)
{
- SaveDataMap mapState = SaveSystem.LoadMapState();
+ SaveDataMap mapState = GetComponent<SaveSystemManager>().GetMapStateFromSave();
if (mapState != null && mapState.fogOfWarData != null)
{
- GetComponent<FogOfWar>().LoadFromSaveData(mapState.fogOfWarData);
+ LoadFromSaveData(mapState.fogOfWarData);
}
}
}
@@ -141,29 +141,9 @@
}
}
- public FogOfWarData GetSaveData()
+ public (TileBase[,] discoveredFog, TileBase fogTile, FogLevel[] fogLevels) GetSaveValues()
{
- FogOfWarData saveData = new FogOfWarData();
-
- for (int x = 0; x < discoveredFog.GetLength(0); x++)
- {
- for (int y = 0; y < discoveredFog.GetLength(1); y++)
- {
- TileBase currentTile = discoveredFog[x, y];
- if (currentTile == null || currentTile != fogTile) // Only save revealed tiles
- {
- FogTileData tileData = new FogTileData
- {
- x = x,
- y = y,
- fogLevelIndex = currentTile == null ? -1 : System.Array.FindIndex(fogLevels, f => f.tile == currentTile)
- };
- saveData.discoveredTiles.Add(tileData);
- }
- }
- }
-
- return saveData;
+ return (discoveredFog,fogTile, fogLevels);
}
public void LoadFromSaveData(FogOfWarData saveData)
diff --git a/Assets/Scripts/GenerateTileMap.cs b/Assets/Scripts/GenerateTileMap.cs
index 8870c8e..d2ea3a2 100644
--- a/Assets/Scripts/GenerateTileMap.cs
+++ b/Assets/Scripts/GenerateTileMap.cs
@@ -28,6 +28,8 @@
private const int CACHE_CLEAR_DISTANCE = 8; // Distance in chunks before clearing cache (should be > LOAD_DISTANCE)
private Vector2Int lastCacheClearPosition; // Track position where cache was last cleared
+ [NonSerialized]
+ public List<Vector3Int> destroyedTiles = new List<Vector3Int>();
private Dictionary<Vector2Int, bool> loadedChunks = new Dictionary<Vector2Int, bool>();
private Dictionary<Vector2Int, TileBase[]> chunkCache = new Dictionary<Vector2Int, TileBase[]>();
private Transform playerTransform; // Reference to player/camera
@@ -62,16 +64,16 @@
//Debug.Log($"Current Chunk: {currentChunk}");
if (currentChunk != lastLoadedChunk)
{
- StartCoroutine(UpdateLoadedChunks(currentChunk, gameManager.destroyedTiles));
+ StartCoroutine(UpdateLoadedChunks(currentChunk, destroyedTiles));
lastLoadedChunk = currentChunk;
// Check if we need to clear the cache
ClearDistantChunks(currentChunk);
}
}
- public Dictionary<Vector2Int, TileBase[]> GetSaveData()
+ public (int seed, Dictionary<Vector2Int, TileBase[]> chunkCache, List<Vector3Int> destroyedTiles) GetSaveValues()
{
- return chunkCache;
+ return (seed.Value, chunkCache, destroyedTiles);
}
public void LoadChunkDataFromSave(List<SerializedChunkData> serializedChunks)
{
@@ -268,7 +270,7 @@
// tilemap.SetTile(tilePos, cachedTiles[i]);
// }
//}
- BatchSetTiles(chunk, cachedTiles, gameManager.destroyedTiles);
+ BatchSetTiles(chunk, cachedTiles, destroyedTiles);
}
yield return null;
}
diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs
index f622519..23b1365 100644
--- a/Assets/Scripts/Inventory/Inventory.cs
+++ b/Assets/Scripts/Inventory/Inventory.cs
@@ -27,7 +27,7 @@
private void LoadInventory()
{
- SaveDataPlayer save = SaveSystem.LoadPlayer();
+ SaveDataPlayer save = SaveSystemManager.Instance.GetPlayerStateFromSave();
if (save != null)
{
foreach (SaveDataInventorySlot item in save.inventoryItems)
diff --git a/Assets/Scripts/Managers/GameManager.cs b/Assets/Scripts/Managers/GameManager.cs
index cf3a5ab..a759205 100644
--- a/Assets/Scripts/Managers/GameManager.cs
+++ b/Assets/Scripts/Managers/GameManager.cs
@@ -22,8 +22,6 @@
public Tilemap tilemap;
[NonSerialized]
public GenerateTileMap generateTileMap;
- [NonSerialized]
- public List<Vector3Int> destroyedTiles = new List<Vector3Int>();
public GameObject pickups;
public Tilemap fogTilemap;
public FogOfWar fogOfWar;
@@ -47,7 +45,7 @@
LoadMapState();
}
Debug.Log("waiting for async map loading");
- StartCoroutine(generateTileMap.GenerateTiles(LoadTileMapsFinished, destroyedTiles));
+ StartCoroutine(generateTileMap.GenerateTiles(LoadTileMapsFinished, generateTileMap.destroyedTiles));
}
private void InitializeBackgroundTiles()
@@ -71,7 +69,7 @@
private void LoadMapState()
{
- SaveDataMap mapState = SaveSystem.LoadMapState();
+ SaveDataMap mapState = GetComponent<SaveSystemManager>().GetMapStateFromSave();
if (mapState != null)
{
//fogOfWar.LoadFromSaveData(mapState.fogOfWarData);
@@ -82,7 +80,7 @@
// TODO rework load map (it's fucky wucky currently as I had to make an extra class for it to jsonify the tiles correctly, as unity does not like lists of arrays or 2d arrays)
foreach (DestroyedTile tile in mapState.destroyedTiles)
{
- destroyedTiles.Add(tile.tileCoord.ConvertToVector3Int());
+ generateTileMap.destroyedTiles.Add(tile.tileCoord.ConvertToVector3Int());
} //destroyedTiles.AddRange(mapState.destroyedTiles.Select(tile => { return tile.tileCoord; }).ToList().ConvertToVector3Int());
}
// Load chunk cache
@@ -206,7 +204,7 @@
}
// Update tilemap
tilemap.SetTile(cellCoord, null);
- destroyedTiles.Add(cellCoord);
+ generateTileMap.destroyedTiles.Add(cellCoord);
generateTileMap.UpdateChunkCache(cellCoord, cellWorldPosition);
CharacterEvents.characterDrillingToPosition.Invoke(cellWorldPosition, drillDirection);
}
diff --git a/Assets/Scripts/Managers/SaveSystemManager.cs b/Assets/Scripts/Managers/SaveSystemManager.cs
new file mode 100644
index 0000000..c4d19fe
--- /dev/null
+++ b/Assets/Scripts/Managers/SaveSystemManager.cs
@@ -0,0 +1,102 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Tilemaps;
+
+public class SaveSystemManager : MonoBehaviour
+{
+ private FogOfWar fogOfWar;
+ private GenerateTileMap generateTileMap;
+ private PlayerController playerController;
+ private Inventory playerInventory;
+ private SaveDataMap mapState;
+ private SaveDataPlayer playerState;
+ public static SaveSystemManager Instance { get; private set; }
+ private void Awake()
+ {
+ if (Instance == null)
+ {
+ Instance = this;
+ fogOfWar = FindObjectOfType<FogOfWar>();
+ generateTileMap = FindObjectOfType<GenerateTileMap>();
+ playerController = GameObject.Find("Player").GetComponent<PlayerController>();
+ playerInventory = GameObject.Find("Player").GetComponent<Inventory>();
+ }
+ else
+ {
+ Destroy(gameObject);
+ }
+ }
+
+ public void SaveGame()
+ {
+ SaveSystem.SavePlayer(GetSaveDataPlayer());
+ SaveSystem.SaveMapState(GetSaveDataMap());
+ }
+
+ private SaveDataPlayer GetSaveDataPlayer()
+ {
+ return new SaveDataPlayer(playerController, playerInventory);
+ }
+
+ public SaveDataMap GetMapStateFromSave()
+ {
+ if (mapState == null)
+ {
+ mapState = SaveSystem.LoadMapState();
+ }
+ return mapState;
+ }
+
+ public SaveDataPlayer GetPlayerStateFromSave()
+ {
+ if (playerState == null)
+ {
+ playerState = SaveSystem.LoadPlayer();
+ Debug.Log("Player state loaded from save");
+ }
+ return playerState;
+ }
+
+ private SaveDataMap GetSaveDataMap()
+ {
+ var saveDataTileMap = GetSaveDataTileMap();
+ return new SaveDataMap(
+ saveDataTileMap.seed,
+ saveDataTileMap.destroyedTiles,
+ saveDataTileMap.chunkCache,
+ GetSaveDataFogOfWar()
+ );
+ }
+
+ private FogOfWarData GetSaveDataFogOfWar()
+ {
+ var fogSaveValues = fogOfWar.GetSaveValues();
+ FogOfWarData saveData = new FogOfWarData();
+
+ for (int x = 0; x < fogSaveValues.discoveredFog.GetLength(0); x++)
+ {
+ for (int y = 0; y < fogSaveValues.discoveredFog.GetLength(1); y++)
+ {
+ TileBase currentTile = fogSaveValues.discoveredFog[x, y];
+ if (currentTile == null || currentTile != fogSaveValues.fogTile) // Only save revealed tiles
+ {
+ FogTileData tileData = new FogTileData
+ {
+ x = x,
+ y = y,
+ fogLevelIndex = currentTile == null ? -1 : System.Array.FindIndex(fogSaveValues.fogLevels, f => f.tile == currentTile)
+ };
+ saveData.discoveredTiles.Add(tileData);
+ }
+ }
+ }
+
+ return saveData;
+ }
+
+ public (int seed, Dictionary<Vector2Int, TileBase[]> chunkCache, List<Vector3Int> destroyedTiles) GetSaveDataTileMap()
+ {
+ return generateTileMap.GetSaveValues();
+ }
+}
diff --git a/Assets/Scripts/Managers/SaveSystemManager.cs.meta b/Assets/Scripts/Managers/SaveSystemManager.cs.meta
new file mode 100644
index 0000000..c5c2bc5
--- /dev/null
+++ b/Assets/Scripts/Managers/SaveSystemManager.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 00c47706c533aee4890c37a8bd5ea5f8
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Scripts/Saving/SaveData.cs b/Assets/Scripts/Saving/SaveData.cs
index 939850c..4366667 100644
--- a/Assets/Scripts/Saving/SaveData.cs
+++ b/Assets/Scripts/Saving/SaveData.cs
@@ -1,6 +1,5 @@
using Assets.Scripts.Helpers;
using System;
-using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@@ -55,7 +54,7 @@
public List<DestroyedTile> destroyedTiles;
public FogOfWarData fogOfWarData;
public List<SerializedChunkData> chunkData; // New field
- public SaveDataMap(List<Vector3Int> destroyedTiles, int seed, FogOfWarData fogOfWarData, Dictionary<Vector2Int, TileBase[]> chunkCache)
+ public SaveDataMap(int seed, List<Vector3Int> destroyedTiles, Dictionary<Vector2Int, TileBase[]> chunkCache, FogOfWarData fogOfWarData)
{
this.seed = seed;
this.destroyedTiles = new List<DestroyedTile>();
diff --git a/Assets/Scripts/UI/PauseMenu.cs b/Assets/Scripts/UI/PauseMenu.cs
index 3a4d141..7310c13 100644
--- a/Assets/Scripts/UI/PauseMenu.cs
+++ b/Assets/Scripts/UI/PauseMenu.cs
@@ -61,11 +61,7 @@
public void OnGameSaveClicked()
{
- PlayerController playerController = GameObject.Find("Player").GetComponent<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, gameManager.GetComponent<FogOfWar>().GetSaveData(),gameManager.generateTileMap.GetSaveData()));
+ SaveSystemManager.Instance.SaveGame();
animator.SetTrigger("GameSaved");
}
--
Gitblit v1.10.0