Update
This commit is contained in:
@@ -56,33 +56,39 @@ namespace Hallucinate.GameSetup.Maze
|
||||
[ContextMenu("Regenerate")]
|
||||
public void Regenerate()
|
||||
{
|
||||
// Bước 1: Khởi tạo tất cả các tầng mê cung
|
||||
for (int i = 0; i < mazes.Length; i++)
|
||||
if (_generationCoroutine != null)
|
||||
{
|
||||
mazes[i].width = width;
|
||||
mazes[i].depth = depth;
|
||||
mazes[i].level = i;
|
||||
mazes[i].Build(); // Lưu ý: Hàm Build này của bạn nên được tối ưu để KHÔNG Instantiate Model vội.
|
||||
StopCoroutine(_generationCoroutine);
|
||||
}
|
||||
|
||||
// Bước 2: Tạo điểm nối giữa các cặp tầng (0->1, 1->2, 2->3...)
|
||||
mazeRenderer.Clear();
|
||||
|
||||
// Step 1: Initialize all maze floors
|
||||
for (int i = 0; i < mazes.Length; i++)
|
||||
{
|
||||
mazes[i] = new MazeGrid(width, depth);
|
||||
mazes[i].Level = i;
|
||||
|
||||
// Generate each floor using the selected algorithm
|
||||
IMazeAlgorithm algorithmForFloor = GetAlgorithm(selectedAlgorithm);
|
||||
algorithmForFloor.Generate(mazes[i]);
|
||||
}
|
||||
|
||||
// Step 2: Create connections between adjacent floors
|
||||
for (int i = 0; i < mazes.Length - 1; i++)
|
||||
{
|
||||
MazeGrid currentFloor = mazes[i];
|
||||
MazeGrid nextFloor = mazes[i + 1];
|
||||
|
||||
// 2.1: Quét tìm TẤT CẢ các tọa độ (x, z) có thể làm điểm nối
|
||||
List<Vector2Int> possibleConnections = new List<Vector2Int>();
|
||||
|
||||
for (int z = 0; z < depth; z++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
// Nếu cả Tầng Dưới và Tầng Trên đều đang là đường đi (Path) ở tọa độ này
|
||||
// (Bạn cần đổi điều kiện này cho khớp với Enum/Class thực tế của bạn)
|
||||
bool isCurrentFloorPath =
|
||||
currentFloor.piecePlace[x, z].piece == MazeGrid.PieceType.Vertical_Straight;
|
||||
bool isNextFloorPath = nextFloor.piecePlace[x, z].piece == MazeGrid.PieceType.Vertical_Straight;
|
||||
// Check if both floors have a corridor at this position
|
||||
bool isCurrentFloorPath = currentFloor.GetCell(x, z) == MazeCellType.Corridor;
|
||||
bool isNextFloorPath = nextFloor.GetCell(x, z) == MazeCellType.Corridor;
|
||||
|
||||
if (isCurrentFloorPath && isNextFloorPath)
|
||||
{
|
||||
@@ -91,60 +97,48 @@ namespace Hallucinate.GameSetup.Maze
|
||||
}
|
||||
}
|
||||
|
||||
// 2.2: Chọn ngẫu nhiên N điểm từ danh sách để làm thang nối
|
||||
int connectionsMade = 0;
|
||||
|
||||
// Trộn ngẫu nhiên danh sách (Shuffle) để các điểm nối không bị dồn về 1 góc
|
||||
ShuffleList(possibleConnections);
|
||||
|
||||
int connectionsMade = 0;
|
||||
foreach (Vector2Int pos in possibleConnections)
|
||||
{
|
||||
if (connectionsMade >= connectionsPerFloor) break; // Đã đủ số lượng đường lên thì dừng
|
||||
if (connectionsMade >= connectionsPerFloor) break;
|
||||
|
||||
int x = pos.x;
|
||||
int z = pos.y;
|
||||
|
||||
// Xóa model đường đi cũ (nếu hàm Build() đã lỡ Instantiate)
|
||||
if (currentFloor.piecePlace[x, z].model != null) Destroy(currentFloor.piecePlace[x, z].model);
|
||||
if (nextFloor.piecePlace[x, z].model != null) Destroy(nextFloor.piecePlace[x, z].model);
|
||||
|
||||
// TÍNH TOẠ ĐỘ Y CHUẨN XÁC: Y = Tầng * Độ_Cao_Tầng
|
||||
Vector3 upManHolePos = new Vector3(x * currentFloor.scale, currentFloor.level * floorHeight,
|
||||
z * currentFloor.scale);
|
||||
Vector3 ladderManPos = new Vector3(x * nextFloor.scale, nextFloor.level * floorHeight,
|
||||
z * nextFloor.scale);
|
||||
|
||||
// Sinh Model mới tại điểm đã chốt
|
||||
currentFloor.piecePlace[x, z].model =
|
||||
Instantiate(straightManHoleUp, upManHolePos, Quaternion.identity);
|
||||
nextFloor.piecePlace[x, z].model =
|
||||
Instantiate(straightManHoleLadder, ladderManPos, Quaternion.identity);
|
||||
|
||||
// Cập nhật loại dữ liệu để hệ thống ghi nhận đây là ô cầu thang
|
||||
// currentFloor.piecePlace[x, z].piece = Maze.PieceType.StairsUp; (Nếu bạn có enum này)
|
||||
// Set stair cells
|
||||
currentFloor.SetCell(x, z, MazeCellType.StairUp);
|
||||
nextFloor.SetCell(x, z, MazeCellType.StairDown);
|
||||
|
||||
connectionsMade++;
|
||||
}
|
||||
}
|
||||
|
||||
if (_generationCoroutine != null)
|
||||
// Step 3: Render all floors
|
||||
if (mazes.Length > 0)
|
||||
{
|
||||
StopCoroutine(_generationCoroutine);
|
||||
}
|
||||
|
||||
mazeRenderer.Clear();
|
||||
_grid = new MazeGrid(width, depth);
|
||||
mazeRenderer.Initialize(_grid, mazeContainer);
|
||||
|
||||
IMazeAlgorithm algorithm = GetAlgorithm(selectedAlgorithm);
|
||||
|
||||
if (debugMode)
|
||||
{
|
||||
_generationCoroutine = StartCoroutine(algorithm.GenerateStepByStep(_grid, visualizationInterval));
|
||||
for (int i = 0; i < mazes.Length; i++)
|
||||
{
|
||||
mazeRenderer.Initialize(mazes[i], mazeContainer, i == 0);
|
||||
}
|
||||
_grid = mazes[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
algorithm.Generate(_grid);
|
||||
_grid = new MazeGrid(width, depth);
|
||||
mazeRenderer.Initialize(_grid, mazeContainer);
|
||||
|
||||
IMazeAlgorithm algorithm = GetAlgorithm(selectedAlgorithm);
|
||||
|
||||
if (debugMode)
|
||||
{
|
||||
_generationCoroutine = StartCoroutine(algorithm.GenerateStepByStep(_grid, visualizationInterval));
|
||||
}
|
||||
else
|
||||
{
|
||||
algorithm.Generate(_grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void ShuffleList<T>(List<T> list)
|
||||
|
||||
Reference in New Issue
Block a user