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