miepzerino
2025-04-06 0f43d3a24a26435c5805261af40880cb96bdc9f2
#48 added chunk caching
1 files modified
51 ■■■■■ changed files
Assets/Scripts/GenerateTileMap.cs 51 ●●●●● patch | view | raw | blame | history
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))
                {
                    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()