miepzerino
2025-04-08 40ac185dc7a017d95771fe580c77eab20e663908
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;
    }
@@ -586,41 +588,41 @@
        chunkCache.Clear();
    }
#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        if (!Application.isPlaying) return;
    //private void OnDrawGizmos()
    //{
    //    if (!Application.isPlaying) return;
        // Draw current chunk boundaries
        if (playerTransform != null)
        {
            Vector2Int currentChunk = GetChunkPosition(playerTransform.position);
            Gizmos.color = Color.yellow;
    //    // Draw current chunk boundaries
    //    if (playerTransform != null)
    //    {
    //        Vector2Int currentChunk = GetChunkPosition(playerTransform.position);
    //        Gizmos.color = Color.yellow;
            for (int x = -LOAD_DISTANCE; x <= LOAD_DISTANCE; x++)
            {
                for (int y = -LOAD_DISTANCE; y <= LOAD_DISTANCE; y++)
                {
                    Vector2Int chunk = new Vector2Int(currentChunk.x + x, currentChunk.y + y);
                    Vector3 worldPos = new Vector3(
                        chunk.x * CHUNK_SIZE + transform.position.x,
                        chunk.y * CHUNK_SIZE + transform.position.y,
                        0
                    );
    //        for (int x = -LOAD_DISTANCE; x <= LOAD_DISTANCE; x++)
    //        {
    //            for (int y = -LOAD_DISTANCE; y <= LOAD_DISTANCE; y++)
    //            {
    //                Vector2Int chunk = new Vector2Int(currentChunk.x + x, currentChunk.y + y);
    //                Vector3 worldPos = new Vector3(
    //                    chunk.x * CHUNK_SIZE + transform.position.x,
    //                    chunk.y * CHUNK_SIZE + transform.position.y,
    //                    0
    //                );
                    // Draw chunk boundary
                    Gizmos.DrawWireCube(
                        worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
                        new Vector3(CHUNK_SIZE, CHUNK_SIZE, 0)
                    );
    //                // Draw chunk boundary
    //                Gizmos.DrawWireCube(
    //                    worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
    //                    new Vector3(CHUNK_SIZE, CHUNK_SIZE, 0)
    //                );
                    // Draw chunk coordinates
                    UnityEditor.Handles.Label(
                        worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
                        $"({chunk.x}, {chunk.y})"
                    );
                }
            }
        }
    }
    //                // Draw chunk coordinates
    //                UnityEditor.Handles.Label(
    //                    worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
    //                    $"({chunk.x}, {chunk.y})"
    //                );
    //            }
    //        }
    //    }
    //}
#endif
}