1 files modified
56 ■■■■■ changed files
Assets/Scripts/GenerateTileMap.cs 56 ●●●●● patch | view | raw | blame | history
Assets/Scripts/GenerateTileMap.cs
@@ -57,7 +57,8 @@
    private GameManager gameManager;
    private Dictionary<Vector2Int, HashSet<Vector3Int>> activeChunkTiles = new Dictionary<Vector2Int, HashSet<Vector3Int>>();
    public TilePool tilePool; // Reference to the TilePool script
    private bool isGenerating = false;
    private bool isGenerating = false;
    private Dictionary<Vector2Int, List<(Vector3Int position, CustomRuleTile tile)>> chunkTileData = new Dictionary<Vector2Int, List<(Vector3Int position, CustomRuleTile tile)>>();
    private void Awake()
@@ -88,6 +89,7 @@
        Vector2Int currentChunk = GetChunkPosition(playerTransform.position);
        if (currentChunk != lastLoadedChunk)
        {
            Debug.Log($"Player moved to new chunk: {currentChunk}");
            isGenerating = true;
            StopAllCoroutines();
@@ -98,6 +100,7 @@
            foreach (var chunk in chunksToUnload)
            {
                Debug.Log($"Unloading chunk: {chunk}");
                UnloadChunk(chunk);
            }
@@ -173,11 +176,11 @@
                Mathf.Abs(chunk.y - currentChunk.y) > LOAD_DISTANCE)
            {
                chunksToUnload.Add(chunk);
                UnloadChunk(chunk);
            }
        }
        foreach (var chunk in chunksToUnload)
        {
            UnloadChunk(chunk);
            loadedChunks.Remove(chunk);
        }
@@ -282,10 +285,14 @@
        float adjustedX = worldPosition.x - transform.position.x;
        float adjustedY = worldPosition.y - transform.position.y;
        return new Vector2Int(
        // Calculate chunk position based on chunk boundaries
        // We don't add the CHUNK_SIZE/2 offset here anymore since we want to detect based on boundaries
        Vector2Int chunkPosition = new Vector2Int(
            Mathf.FloorToInt(adjustedX / CHUNK_SIZE),
            Mathf.FloorToInt(adjustedY / CHUNK_SIZE)
        );
        return chunkPosition;
    }
    private IEnumerator LoadChunksAroundPosition(Vector2Int centerChunk, List<Vector3Int> destroyedTiles)
    {
@@ -296,7 +303,29 @@
                Vector2Int chunkPos = new Vector2Int(centerChunk.x + x, centerChunk.y + y);
                if (!loadedChunks.ContainsKey(chunkPos))
                {
                    yield return GenerateChunk(chunkPos, destroyedTiles);
                    if (chunkTileData.ContainsKey(chunkPos))
                    {
                        Debug.Log($"Reloading chunk from saved data: {chunkPos}");
                        // Reload chunk from saved data
                        var tilesToUpdate = chunkTileData[chunkPos];
                        const int BATCH_SIZE = 32;
                        for (int i = 0; i < tilesToUpdate.Count; i += BATCH_SIZE)
                        {
                            int batchCount = Math.Min(BATCH_SIZE, tilesToUpdate.Count - i);
                            for (int j = 0; j < batchCount; j++)
                            {
                                var (pos, tile) = tilesToUpdate[i + j];
                                SetTileWithPool(pos, tile);
                            }
                            yield return null;
                        }
                    }
                    else
                    {
                        Debug.Log($"Generating new chunk: {chunkPos}");
                        // Generate new chunk
                        yield return GenerateChunk(chunkPos, destroyedTiles);
                    }
                    loadedChunks[chunkPos] = true;
                }
            }
@@ -343,7 +372,7 @@
                if (terrainMap[i])
                {
                    int x = startX + (i % CHUNK_SIZE);
                    int y = startY - (i / CHUNK_SIZE);
                    int y = startY + (i / CHUNK_SIZE);
                    Vector3Int tilePos = new Vector3Int(x, y);
                    if (!IsTileDestroyed(tilePos))
@@ -352,6 +381,9 @@
                    }
                }
            }
            // Save tile data to dictionary
            chunkTileData[chunk] = tilesToUpdate;
            // Dispose native arrays before yielding
            terrainMap.Dispose();
@@ -584,6 +616,11 @@
            Vector2Int currentChunk = GetChunkPosition(playerTransform.position);
            Gizmos.color = Color.yellow;
            // Draw player position for debugging
            Gizmos.color = Color.red;
            Gizmos.DrawSphere(playerTransform.position, 0.5f);
            Gizmos.color = Color.yellow;
            for (int x = -LOAD_DISTANCE; x <= LOAD_DISTANCE; x++)
            {
                for (int y = -LOAD_DISTANCE; y <= LOAD_DISTANCE; y++)
@@ -595,10 +632,19 @@
                        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 and boundaries
                    UnityEditor.Handles.Label(
                        worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
                        $"({chunk.x}, {chunk.y})\n" +
                        $"X: {worldPos.x} to {worldPos.x + CHUNK_SIZE}\n" +
                        $"Y: {worldPos.y} to {worldPos.y - CHUNK_SIZE}"
                    );
                }
            }
        }