update map gen
This commit is contained in:
parent
3246ab391a
commit
a6da7e1208
@ -28,28 +28,18 @@ public class MapGenerator : MonoBehaviour
|
|||||||
// 4. Shuffle (random rotation)
|
// 4. Shuffle (random rotation)
|
||||||
ShuffleTiles(tiles, width, height, seed);
|
ShuffleTiles(tiles, width, height, seed);
|
||||||
}
|
}
|
||||||
private static byte[,] GenerateSolution(int width, int height, bool wrapping)
|
|
||||||
|
private static byte[,] GenerateSolution(int width, int height, bool wrapping)
|
||||||
{
|
{
|
||||||
byte[,] tiles = new byte[width, height];
|
byte[,] tiles = new byte[width, height];
|
||||||
int cx = width / 2;
|
int cx = width / 2;
|
||||||
int cy = height / 2;
|
int cy = height / 2;
|
||||||
|
|
||||||
// Force center tile to be a TriCorner (3 connections)
|
// Use a priority queue (simplified with List for this example)
|
||||||
// We'll connect it in 3 directions (right, up, left)
|
|
||||||
tiles[cx, cy] = (byte)((1 << 0) | (1 << 1) | (1 << 2)); // R, U, L
|
|
||||||
|
|
||||||
// Connect the adjacent tiles to match
|
|
||||||
if (cx + 1 < width || wrapping) tiles[Wrap(cx + 1, width, wrapping), cy] |= (byte)(1 << 2); // Left on right neighbor
|
|
||||||
if (cy + 1 < height || wrapping) tiles[cx, Wrap(cy + 1, height, wrapping)] |= (byte)(1 << 3); // Down on up neighbor
|
|
||||||
if (cx - 1 >= 0 || wrapping) tiles[Wrap(cx - 1, width, wrapping), cy] |= (byte)(1 << 0); // Right on left neighbor
|
|
||||||
|
|
||||||
var possibilities = new List<(int x, int y, int dir)>();
|
var possibilities = new List<(int x, int y, int dir)>();
|
||||||
|
|
||||||
// Add possible directions from the center's connected neighbors
|
// Start from center
|
||||||
AddPossibleDirections(possibilities, Wrap(cx + 1, width, wrapping), cy, width, height, wrapping);
|
AddPossibleDirections(possibilities, cx, cy, width, height, wrapping);
|
||||||
AddPossibleDirections(possibilities, cx, Wrap(cy + 1, height, wrapping), width, height, wrapping);
|
|
||||||
AddPossibleDirections(possibilities, Wrap(cx - 1, width, wrapping), cy, width, height, wrapping);
|
|
||||||
|
|
||||||
while (possibilities.Count > 0)
|
while (possibilities.Count > 0)
|
||||||
{
|
{
|
||||||
// Get random possibility
|
// Get random possibility
|
||||||
@ -57,17 +47,16 @@ public class MapGenerator : MonoBehaviour
|
|||||||
var (x1, y1, d1) = possibilities[idx];
|
var (x1, y1, d1) = possibilities[idx];
|
||||||
possibilities.RemoveAt(idx);
|
possibilities.RemoveAt(idx);
|
||||||
|
|
||||||
// Skip if this is the center tile (already fully connected)
|
|
||||||
if (x1 == cx && y1 == cy) continue;
|
|
||||||
|
|
||||||
// Calculate destination
|
// Calculate destination
|
||||||
int x2 = Wrap(x1 + directions[d1].x, width, wrapping);
|
int x2 = Wrap(x1 + directions[d1].x, width, wrapping);
|
||||||
int y2 = Wrap(y1 + directions[d1].y, height, wrapping);
|
int y2 = Wrap(y1 + directions[d1].y, height, wrapping);
|
||||||
int d2 = (d1 + 2) % 4; // Opposite direction
|
int d2 = (d1 + 2) % 4; // Opposite direction
|
||||||
|
|
||||||
// Skip if destination already has connections (except center)
|
// Skip if destination already has connections
|
||||||
if ((x2 != cx || y2 != cy) && tiles[x2, y2] != 0)
|
if (tiles[x2, y2] != 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Make the connection
|
// Make the connection
|
||||||
tiles[x1, y1] |= (byte)(1 << d1);
|
tiles[x1, y1] |= (byte)(1 << d1);
|
||||||
@ -84,50 +73,6 @@ public class MapGenerator : MonoBehaviour
|
|||||||
return tiles;
|
return tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CreateTiles(Tile[,] tiles, GameObject[] prefabs, int width, int height,
|
|
||||||
byte[,] solution, byte[,] barriers)
|
|
||||||
{
|
|
||||||
float offsetX = -((width - 1) / 2f);
|
|
||||||
float offsetY = ((height - 1) / 2f);
|
|
||||||
|
|
||||||
int centerX = width / 2;
|
|
||||||
int centerY = height / 2;
|
|
||||||
|
|
||||||
for (int x = 0; x < width; x++)
|
|
||||||
{
|
|
||||||
for (int y = 0; y < height; y++)
|
|
||||||
{
|
|
||||||
byte connections = solution[x, y];
|
|
||||||
string type = GetTileType(connections);
|
|
||||||
int rotation = GetRotation(connections, type);
|
|
||||||
|
|
||||||
GameObject prefab = FindPrefab(prefabs, type);
|
|
||||||
GameObject tileObj = Instantiate(prefab);
|
|
||||||
tileObj.transform.position = new Vector2(offsetX + x, offsetY - y);
|
|
||||||
|
|
||||||
Tile tile = tileObj.GetComponent<Tile>();
|
|
||||||
tile.X = x;
|
|
||||||
tile.Y = y;
|
|
||||||
|
|
||||||
tile.baseConnections = GetBaseConnections(type);
|
|
||||||
tile.graphicRotationOffset = GetGraphicOffset(type);
|
|
||||||
|
|
||||||
tile.ApplyRotation(rotation);
|
|
||||||
tile.SolutionRotation = rotation;
|
|
||||||
tile.SolutionConnections = connections;
|
|
||||||
tile.Barriers = barriers[x, y];
|
|
||||||
|
|
||||||
// Mark center tile
|
|
||||||
if (x == centerX && y == centerY)
|
|
||||||
{
|
|
||||||
tile.isCenterTile = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
tile.SetConnectionState(true); // Show connected state (green)
|
|
||||||
tiles[x, y] = tile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private static void AddPossibleDirections(List<(int x, int y, int dir)> possibilities,
|
private static void AddPossibleDirections(List<(int x, int y, int dir)> possibilities,
|
||||||
int x, int y, int width, int height, bool wrapping)
|
int x, int y, int width, int height, bool wrapping)
|
||||||
{
|
{
|
||||||
@ -203,41 +148,44 @@ public class MapGenerator : MonoBehaviour
|
|||||||
return barriers;
|
return barriers;
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static void CreateTiles(Tile[,] tiles, GameObject[] prefabs, int width, int height,
|
private static void CreateTiles(Tile[,] tiles, GameObject[] prefabs, int width, int height,
|
||||||
// byte[,] solution, byte[,] barriers)
|
byte[,] solution, byte[,] barriers)
|
||||||
// {
|
{
|
||||||
// float offsetX = -((width - 1) / 2f);
|
float offsetX = -((width - 1) / 2f);
|
||||||
// float offsetY = ((height - 1) / 2f);
|
float offsetY = ((height - 1) / 2f);
|
||||||
|
|
||||||
// for (int x = 0; x < width; x++)
|
for (int x = 0; x < width; x++)
|
||||||
// {
|
{
|
||||||
// for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
// {
|
{
|
||||||
// byte connections = solution[x, y];
|
byte connections = solution[x, y];
|
||||||
// string type = GetTileType(connections);
|
string type = GetTileType(connections);
|
||||||
// int rotation = GetRotation(connections, type);
|
int rotation = GetRotation(connections, type);
|
||||||
|
|
||||||
// GameObject prefab = FindPrefab(prefabs, type);
|
GameObject prefab = FindPrefab(prefabs, type);
|
||||||
// GameObject tileObj = Instantiate(prefab);
|
GameObject tileObj = Instantiate(prefab);
|
||||||
// tileObj.transform.position = new Vector2(offsetX + x, offsetY - y);
|
tileObj.transform.position = new Vector2(offsetX + x, offsetY - y);
|
||||||
|
|
||||||
// Tile tile = tileObj.GetComponent<Tile>();
|
Tile tile = tileObj.GetComponent<Tile>();
|
||||||
// tile.X = x;
|
tile.X = x;
|
||||||
// tile.Y = y;
|
tile.Y = y;
|
||||||
|
|
||||||
// tile.baseConnections = GetBaseConnections(type);
|
tile.baseConnections = GetBaseConnections(type);
|
||||||
// tile.graphicRotationOffset = GetGraphicOffset(type);
|
tile.graphicRotationOffset = GetGraphicOffset(type);
|
||||||
|
|
||||||
// tile.ApplyRotation(rotation);
|
tile.ApplyRotation(rotation);
|
||||||
// tile.SolutionRotation = rotation;
|
tile.SolutionRotation = rotation;
|
||||||
// tile.SolutionConnections = connections;
|
tile.SolutionConnections = connections;
|
||||||
// tile.Barriers = barriers[x, y];
|
tile.Barriers = barriers[x, y];
|
||||||
|
|
||||||
// tile.SetConnectionState(true); // Show connected state (green)
|
tile.SetConnectionState(true); // Show connected state (green)
|
||||||
// tiles[x, y] = tile;
|
tiles[x, y] = tile;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
// Set center tile to true
|
||||||
|
tiles[width / 2, height / 2].isCenterTile = true;
|
||||||
|
tiles[width / 2, height / 2].SetConnectionState(true); // Show connected state (green)
|
||||||
|
}
|
||||||
|
|
||||||
private static void ShuffleTiles(Tile[,] tiles, int width, int height, int seed)
|
private static void ShuffleTiles(Tile[,] tiles, int width, int height, int seed)
|
||||||
{
|
{
|
||||||
@ -276,8 +224,6 @@ public class MapGenerator : MonoBehaviour
|
|||||||
private static string GetTileType(byte connections)
|
private static string GetTileType(byte connections)
|
||||||
{
|
{
|
||||||
int count = CountBits(connections);
|
int count = CountBits(connections);
|
||||||
if (count == 4)
|
|
||||||
Debug.Log($"Tile Type: {connections} ({count})");
|
|
||||||
switch (count)
|
switch (count)
|
||||||
{
|
{
|
||||||
case 1: return "Node";
|
case 1: return "Node";
|
||||||
@ -286,7 +232,6 @@ public class MapGenerator : MonoBehaviour
|
|||||||
return "Straight"; // Opposite directions
|
return "Straight"; // Opposite directions
|
||||||
return "Corner";
|
return "Corner";
|
||||||
case 3: return "TriCorner";
|
case 3: return "TriCorner";
|
||||||
case 4: return "TriCorner"; // Full cross (not allowed in this game)
|
|
||||||
default: return "Empty";
|
default: return "Empty";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -342,6 +287,7 @@ public class MapGenerator : MonoBehaviour
|
|||||||
case "Straight": return new int[] { 0, 2 }; // Right and Left
|
case "Straight": return new int[] { 0, 2 }; // Right and Left
|
||||||
case "Corner": return new int[] { 0, 1 }; // Right and Up
|
case "Corner": return new int[] { 0, 1 }; // Right and Up
|
||||||
case "TriCorner": return new int[] { 0, 1, 2 }; // Right, Up, Left
|
case "TriCorner": return new int[] { 0, 1, 2 }; // Right, Up, Left
|
||||||
|
// case "Cross": return new int[] { 0, 1, 2, 3 }; // All directions
|
||||||
default: return new int[0];
|
default: return new int[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user