From 0f43d3a24a26435c5805261af40880cb96bdc9f2 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Sun, 06 Apr 2025 16:38:43 +0000
Subject: [PATCH] #48 added chunk caching

---
 Assets/Scripts/GenerateTileMap.cs |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/Assets/Scripts/GenerateTileMap.cs b/Assets/Scripts/GenerateTileMap.cs
index 1d14849..cb32cb4 100644
--- a/Assets/Scripts/GenerateTileMap.cs
+++ b/Assets/Scripts/GenerateTileMap.cs
@@ -3,6 +3,7 @@
 using System.Collections.Generic;
 using Unity.Collections;
 using Unity.Jobs;
+using Unity.VisualScripting;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
@@ -25,6 +26,7 @@
     public int LOAD_DISTANCE = 2; // Number of chunks to load around player
 
     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
     private Vector2Int lastLoadedChunk;
     private GameManager gameManager;
@@ -206,16 +208,50 @@
                 Vector2Int chunkPos = new Vector2Int(centerChunk.x + x, centerChunk.y + y);
                 if (!loadedChunks.ContainsKey(chunkPos))
                 {
-                    yield return GenerateChunk(chunkPos, destroyedTiles);
+                    if (chunkCache.ContainsKey(chunkPos))
+                    {
+                        // Load the chunk from the cache
+                        yield return LoadChunkFromCache(chunkPos);
+                    }
+                    else
+                    {
+                        // Generate the chunk
+                        yield return GenerateChunk(chunkPos, destroyedTiles);
+                    }
                     loadedChunks[chunkPos] = true;
                 }
             }
         }
     }
+
+    private IEnumerator LoadChunkFromCache(Vector2Int chunk)
+    {
+        int startX = chunk.x * CHUNK_SIZE;
+        int startY = (chunk.y * CHUNK_SIZE) - 1;
+
+        if (chunkCache.TryGetValue(chunk, out TileBase[] cachedTiles))
+        {
+            for (int i = 0; i < cachedTiles.Length; i++)
+            {
+                int x = startX + (i % CHUNK_SIZE);
+                int y = startY - (i / CHUNK_SIZE);
+                Vector3Int tilePos = new Vector3Int(x, y);
+
+                if (!IsTileDestroyed(tilePos))
+                {
+                    tilemap.SetTile(tilePos, cachedTiles[i]);
+                }
+            }
+            Debug.Log($"Loaded chunk from cache: {chunk}");
+        }
+        yield return null;
+    }
     private IEnumerator GenerateChunk(Vector2Int chunk, List<Vector3Int> destroyedTiles)
     {
         int startX = chunk.x * CHUNK_SIZE;
         int startY = (chunk.y * CHUNK_SIZE) - 1;
+        // Check if the chunk is already in the cache
+        LoadChunkFromCache(chunk);
 
         // Create job data
         var groundJob = new GenerateGroundJob
@@ -240,6 +276,7 @@
 
         // Apply the results
         groundJobHandle.Complete();
+        TileBase[] newChunkTiles = new TileBase[CHUNK_SIZE * CHUNK_SIZE];
         for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
         {
             if (groundJob.groundTiles[i])
@@ -251,9 +288,21 @@
                 if (!IsTileDestroyed(tilePos))
                 {
                     tilemap.SetTile(tilePos, forestRuleTile);
+                    newChunkTiles[i] = forestRuleTile;
+                }
+                else
+                {
+                    newChunkTiles[i] = null;
                 }
             }
+            else
+            {
+                newChunkTiles[i] = null;
+            }
         }
+
+        // Save the generated chunk to the cache
+        chunkCache[chunk] = newChunkTiles;
 
         // Clean up native array
         groundJob.groundTiles.Dispose();
@@ -301,6 +350,7 @@
                     {
                         float xPerlin = ((float)x / maxWidth) * (float)generateable.clusterWeight + offsetX;
                         float yPerlin = ((float)Mathf.Abs(y) / maxDepth) * (float)generateable.clusterWeight + offsetY;
+
                         float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin);
 
                         if (perlinNoise <= (1f / (float)generateable.weight))
@@ -394,6 +444,7 @@
         {
             perlinNoiseCache.Dispose();
         }
+        chunkCache.Clear();
     }
 #if UNITY_EDITOR
     private void OnDrawGizmos()

--
Gitblit v1.9.3