Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
+31
-41
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
public class WFC
|
||||
public static class WFC
|
||||
{
|
||||
public static Dictionary<string, Dictionary<Direction, List<string>>> adjacency = new Dictionary<string, Dictionary<Direction, List<string>>>();
|
||||
public enum Direction
|
||||
@@ -122,8 +122,6 @@ public class WFC
|
||||
return aOpen == bOpen;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void FillAdjacencies()
|
||||
{
|
||||
foreach (string tile in tileConnections.Keys)
|
||||
@@ -136,8 +134,7 @@ public class WFC
|
||||
|
||||
foreach (string other in tileConnections.Keys)
|
||||
{
|
||||
if (CanConnect(tile, other, dir, false))
|
||||
valid.Add(other);
|
||||
if (CanConnect(tile, other, dir, false)) valid.Add(other);
|
||||
}
|
||||
|
||||
adjacency[tile][dir] = valid;
|
||||
@@ -156,8 +153,7 @@ public class WFC
|
||||
Tile fromTile = layer[from.X, from.Y];
|
||||
Tile toTile = layer[to.X, to.Y];
|
||||
|
||||
if (!IsWalkable(toTile))
|
||||
return false;
|
||||
if (!IsWalkable(toTile)) return false;
|
||||
|
||||
return CanConnect(fromTile.collapsedMesh, toTile.collapsedMesh, dir, true);
|
||||
}
|
||||
@@ -167,29 +163,21 @@ public class WFC
|
||||
Tile fromTile = GameData.map[from.Y].tiles[from.X, from.Z];
|
||||
Tile toTile = GameData.map[to.Y].tiles[to.X, to.Z];
|
||||
|
||||
if (fromTile == null || toTile == null)
|
||||
return false;
|
||||
if (fromTile == null || toTile == null) return false;
|
||||
|
||||
if (from.Y != to.Y)
|
||||
{
|
||||
if (Math.Abs(from.Y - to.Y) != 1)
|
||||
return false;
|
||||
if (Math.Abs(from.Y - to.Y) != 1) return false;
|
||||
|
||||
if (from.Y > to.Y)
|
||||
{
|
||||
return toTile.collapsedMesh == "gate";
|
||||
}
|
||||
else
|
||||
{
|
||||
return fromTile.collapsedMesh == "gate";
|
||||
}
|
||||
return from.Y > to.Y
|
||||
? toTile.collapsedMesh == "gate"
|
||||
: fromTile.collapsedMesh == "gate";
|
||||
}
|
||||
|
||||
int dx = to.X - from.X;
|
||||
int dz = to.Z - from.Z;
|
||||
|
||||
if (Math.Abs(dx) + Math.Abs(dz) != 1)
|
||||
return false;
|
||||
if (Math.Abs(dx) + Math.Abs(dz) != 1) return false;
|
||||
|
||||
Direction dir;
|
||||
|
||||
@@ -207,29 +195,23 @@ public class WFC
|
||||
);
|
||||
}
|
||||
|
||||
public static bool IsMapConnected(Tile[,] layer, float accessibilityThreshhold)
|
||||
public static bool IsMapConnected(Tile[,] layer, float accessibilityThreshold)
|
||||
{
|
||||
bool result = false;
|
||||
HashSet<Vector2I> visited = new HashSet<Vector2I>();
|
||||
List<Vector2I> toCheck = new List<Vector2I>();
|
||||
Vector2I position;
|
||||
toCheck.Add(new Vector2I(1, 1));
|
||||
|
||||
int safetyCounter = 0;
|
||||
while (true)
|
||||
while (toCheck.Count > 0)
|
||||
{
|
||||
if (toCheck.Count <= 0) break;
|
||||
int index = GameData.rand.Next(toCheck.Count);
|
||||
position = toCheck[index];
|
||||
toCheck[index] = toCheck[^1];
|
||||
toCheck.RemoveAt(toCheck.Count - 1);
|
||||
Vector2I position = TakeRandomPosition(toCheck);
|
||||
if (!visited.Add(position)) continue;
|
||||
|
||||
for (int i = 0; i < offsets2D.Length; i++)
|
||||
{
|
||||
Vector2I next = position + offsets2D[i];
|
||||
|
||||
if (!InBounds(next, layer.GetLength(0)))
|
||||
continue;
|
||||
if (!InBounds(next, layer.GetLength(0))) continue;
|
||||
|
||||
if (CanWalk(layer, position, next, dirs[i]))
|
||||
{
|
||||
@@ -238,22 +220,30 @@ public class WFC
|
||||
}
|
||||
safetyCounter++;
|
||||
if (safetyCounter > layer.Length * 2) break;
|
||||
if (visited.Count >= Math.Pow(layer.GetLength(0) - 1, 2) * accessibilityThreshhold)
|
||||
if (visited.Count >= Math.Pow(layer.GetLength(0) - 1, 2) * accessibilityThreshold)
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Vector2I TakeRandomPosition(List<Vector2I> positions)
|
||||
{
|
||||
int index = GameData.rand.Next(positions.Count);
|
||||
Vector2I position = positions[index];
|
||||
positions[index] = positions[positions.Count - 1];
|
||||
positions.RemoveAt(positions.Count - 1);
|
||||
return position;
|
||||
}
|
||||
|
||||
public static bool InBounds(Vector2I pos, int layerSize)
|
||||
{
|
||||
return pos.X > 0 &&
|
||||
pos.Y > 0 &&
|
||||
pos.X < layerSize &&
|
||||
pos.Y < layerSize;
|
||||
return pos.X > 0
|
||||
&& pos.Y > 0
|
||||
&& pos.X < layerSize
|
||||
&& pos.Y < layerSize;
|
||||
}
|
||||
|
||||
public static List<string> GetBorderPossibilities(int x, int z)
|
||||
|
||||
Reference in New Issue
Block a user