#50 added saveDataManager
8 files modified
2 files added
| | |
| | | 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 |
| | |
| | | |
| | | 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); |
| | |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | 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) |
| | |
| | | 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 |
| | |
| | | //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) |
| | | { |
| | |
| | | // tilemap.SetTile(tilePos, cachedTiles[i]); |
| | | // } |
| | | //} |
| | | BatchSetTiles(chunk, cachedTiles, gameManager.destroyedTiles); |
| | | BatchSetTiles(chunk, cachedTiles, destroyedTiles); |
| | | } |
| | | yield return null; |
| | | } |
| | |
| | | |
| | | private void LoadInventory() |
| | | { |
| | | SaveDataPlayer save = SaveSystem.LoadPlayer(); |
| | | SaveDataPlayer save = SaveSystemManager.Instance.GetPlayerStateFromSave(); |
| | | if (save != null) |
| | | { |
| | | foreach (SaveDataInventorySlot item in save.inventoryItems) |
| | |
| | | public Tilemap tilemap; |
| | | [NonSerialized] |
| | | public GenerateTileMap generateTileMap; |
| | | [NonSerialized] |
| | | public List<Vector3Int> destroyedTiles = new List<Vector3Int>(); |
| | | public GameObject pickups; |
| | | public Tilemap fogTilemap; |
| | | public FogOfWar fogOfWar; |
| | |
| | | LoadMapState(); |
| | | } |
| | | Debug.Log("waiting for async map loading"); |
| | | StartCoroutine(generateTileMap.GenerateTiles(LoadTileMapsFinished, destroyedTiles)); |
| | | StartCoroutine(generateTileMap.GenerateTiles(LoadTileMapsFinished, generateTileMap.destroyedTiles)); |
| | | } |
| | | |
| | | private void InitializeBackgroundTiles() |
| | |
| | | |
| | | private void LoadMapState() |
| | | { |
| | | SaveDataMap mapState = SaveSystem.LoadMapState(); |
| | | SaveDataMap mapState = GetComponent<SaveSystemManager>().GetMapStateFromSave(); |
| | | if (mapState != null) |
| | | { |
| | | //fogOfWar.LoadFromSaveData(mapState.fogOfWarData); |
| | |
| | | // 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 |
| | |
| | | } |
| | | // Update tilemap |
| | | tilemap.SetTile(cellCoord, null); |
| | | destroyedTiles.Add(cellCoord); |
| | | generateTileMap.destroyedTiles.Add(cellCoord); |
| | | generateTileMap.UpdateChunkCache(cellCoord, cellWorldPosition); |
| | | CharacterEvents.characterDrillingToPosition.Invoke(cellWorldPosition, drillDirection); |
| | | } |
New file |
| | |
| | | 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(); |
| | | } |
| | | } |
New file |
| | |
| | | fileFormatVersion: 2 |
| | | guid: 00c47706c533aee4890c37a8bd5ea5f8 |
| | | MonoImporter: |
| | | externalObjects: {} |
| | | serializedVersion: 2 |
| | | defaultReferences: [] |
| | | executionOrder: 0 |
| | | icon: {instanceID: 0} |
| | | userData: |
| | | assetBundleName: |
| | | assetBundleVariant: |
| | |
| | | using Assets.Scripts.Helpers; |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using UnityEngine; |
| | |
| | | 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>(); |
| | |
| | | |
| | | 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"); |
| | | } |
| | | |