From 4f1ed3919b0ee3f89dbcbacf49990888a7d9274a Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Thu, 03 Apr 2025 21:02:03 +0000
Subject: [PATCH] #42 still bugged
---
Assets/Scripts/GenerateTileMap.cs | 53 ++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 46 insertions(+), 7 deletions(-)
diff --git a/Assets/Scripts/GenerateTileMap.cs b/Assets/Scripts/GenerateTileMap.cs
index f17a0aa..3c27d48 100644
--- a/Assets/Scripts/GenerateTileMap.cs
+++ b/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))
@@ -353,6 +382,9 @@
}
}
+ // Save tile data to dictionary
+ chunkTileData[chunk] = tilesToUpdate;
+
// Dispose native arrays before yielding
terrainMap.Dispose();
noiseMap.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++)
@@ -601,10 +638,12 @@
new Vector3(CHUNK_SIZE, CHUNK_SIZE, 0)
);
- // Draw chunk coordinates
+ // Draw chunk coordinates and boundaries
UnityEditor.Handles.Label(
worldPos + new Vector3(CHUNK_SIZE / 2f, -CHUNK_SIZE / 2f, 0),
- $"({chunk.x}, {chunk.y})"
+ $"({chunk.x}, {chunk.y})\n" +
+ $"X: {worldPos.x} to {worldPos.x + CHUNK_SIZE}\n" +
+ $"Y: {worldPos.y} to {worldPos.y - CHUNK_SIZE}"
);
}
}
--
Gitblit v1.10.0