Restructered project folders

This commit is contained in:
=
2026-04-28 11:09:47 +02:00
parent eccaec859f
commit 287cb4df78
18 changed files with 57 additions and 11 deletions
+277
View File
@@ -0,0 +1,277 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using static WFC;
public partial class Layer : Node3D
{
Random rand = new Random();
private Node3D decorationRoot;
public Tile[,] tiles;
int layerSize;
Tile tile;
int level;
bool updateFailed = false;
public bool hasContentGenerated = false;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
decorationRoot = new Node3D
{
Name = "Decorations"
};
AddChild(decorationRoot);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public void ClearDecorations()
{
foreach (var tile in tiles)
{
foreach (Node child in tile.ContentNode.GetChildren())
{
child.QueueFree();
}
}
}
public void SetupLayer(int layerSize, int level, Dictionary<string, MeshInstance3D> tileMeshes)
{
this.layerSize = layerSize;
this.level = level;
tiles = new Tile[layerSize, layerSize];
GenerateBaseStructure(tileMeshes);
int safetyCounter = 0;
while (true)
{
if (GenerateLayer()) break;
ResetLayer(tileMeshes);
safetyCounter++;
if (safetyCounter > 1000) break;
}
CreateTileNodes();
}
private void GenerateBaseStructure(Dictionary<string, MeshInstance3D> tileMeshes)
{
Vector3 position;
float offsetX;
float offsetY = level * 4 * -1;
float offsetZ;
for (int x = 0; x < layerSize; x++)
{
offsetX = x * 6;
for (int y = 0; y < layerSize; y++)
{
offsetZ = y * 6;
position = new Vector3(offsetX, offsetY, offsetZ);
tile = new Tile();
tile.SetMeshes(tileMeshes);
tile.Position = position;
tile.GridPosition = new Vector2I(x, y);
tiles[x, y] = tile;
}
}
}
private void CreateTileNodes()
{
foreach (var tile in tiles)
{
var node = new Node3D
{
Position = tile.Position
};
decorationRoot.AddChild(node);
tile.ContentNode = node;
}
}
private void ResetLayer(Dictionary<string, MeshInstance3D> tileMeshes)
{
for (int x = 0; x < layerSize; x++)
{
for (int y = 0; y < layerSize; y++)
{
tiles[x, y].Reset(tileMeshes);
}
}
}
private bool GenerateBorder()
{
for (int x = 0; x < layerSize; x++)
{
for (int y = 0; y < layerSize; y++)
{
if (x == 0 || y == 0 || x == layerSize - 1 || y == layerSize - 1)
{
var tile = tiles[x, y];
List<string> possibilities = new();
if(x == 0 && y == 0)
{
possibilities.Add("corner_down_right");
}
else if(x == 0 && y == layerSize - 1)
{
possibilities.Add("corner_up_right");
}
else if(x == layerSize - 1 && y == 0)
{
possibilities.Add("corner_down_left");
}
else if(x == layerSize - 1 && y == layerSize - 1)
{
possibilities.Add("corner_up_left");
}
else if(y == 0)
{
possibilities.Add("straight_left_right");
possibilities.Add("t_down");
}
else if(y == layerSize - 1)
{
possibilities.Add("straight_left_right");
possibilities.Add("t_up");
}
else if(x == 0)
{
possibilities.Add("straight_up_down");
possibilities.Add("t_right");
}
else if(x == layerSize - 1)
{
possibilities.Add("straight_up_down");
possibilities.Add("t_left");
}
string result = tile.Collapse(possibilities[rand.Next(0, possibilities.Count)]);
if (result == "ERR")
return false;
NewPropagate(new Vector2I(x, y));
}
}
}
return true;
}
public bool GenerateLayer()
{
bool result = true;
int safetyCounter = 0;
if (!GenerateBorder())
{
return false;
}
Vector2I position = GetSmallestPossibilities();
while (true)
{
string keyword = tiles[position.X, position.Y].Collapse("");
if (keyword == "ERR") return false;
if (keyword != "")
{
NewPropagate(position);
if (updateFailed) break;
position = GetSmallestPossibilities();
if (position == new Vector2(-100, -100))
{
break;
}
continue;
}
safetyCounter++;
if (safetyCounter == layerSize * layerSize) return false;
}
if (updateFailed) return false;
//Player has over 80% of tiles available without destroying walls => Results in about 95% success rate
//Not necessarily needed but improves the overall generation percentage at a low resource cost
if (!WFC.IsMapConnected(tiles, 0.8f)) return false;
return result;
}
private void NewPropagate(Vector2I startPos)
{
Queue<Vector2I> queue = new Queue<Vector2I>();
queue.Enqueue(startPos);
while (queue.Count > 0)
{
Vector2I currentPos = queue.Dequeue();
Tile currentTile = tiles[currentPos.X, currentPos.Y];
// Use CURRENT state of tile
var currentPossibilities = currentTile.collapsedMesh != null
? new HashSet<string> { currentTile.collapsedMesh }
: new HashSet<string>(currentTile.tileMeshes.Keys);
for (int i = 0; i < dirs.Length; i++)
{
Vector2I newPos = currentPos + offsets[i];
if (!InBounds(newPos, layerSize)) continue;
Tile neighborTile = tiles[newPos.X, newPos.Y];
HashSet<string> allowed = new HashSet<string>();
foreach (string neighborOption in neighborTile.tileMeshes.Keys)
{
foreach (string item in currentPossibilities)
{
if (WFC.CanConnect(item, neighborOption, dirs[i], false))
{
allowed.Add(neighborOption);
break;
}
}
}
int updateCount = neighborTile.Propagate(allowed);
if (updateCount == int.MaxValue)
{
updateFailed = true;
return;
}
// ONLY enqueue if something changed
if (updateCount > 0)
{
queue.Enqueue(newPos);
}
}
}
}
private Vector2I GetSmallestPossibilities()
{
Vector2I result = new Vector2I(-100, -100);
int lowest = 100;
int current;
for (int x = 0; x < layerSize; x++)
{
for (int y = 0; y < layerSize; y++)
{
if (tiles[x, y].collapsedMesh != null) continue;
current = tiles[x, y].tileMeshes.Count;
if (current < lowest)
{
result = new Vector2I(x, y);
lowest = current;
}
}
}
return result;
}
}
+1
View File
@@ -0,0 +1 @@
uid://dkg0vq75koeig
+137
View File
@@ -0,0 +1,137 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class Tile
{
public Dictionary<string, MeshInstance3D> tileMeshes;
public string collapsedMesh;
Random rand = new Random();
public Vector3 Position;
public Vector2I GridPosition;
public Node3D ContentNode;
public bool containsLight, containsDecoration, containsResource;
public void SetMeshes(Dictionary<string, MeshInstance3D> tileMeshes)
{
this.tileMeshes = new Dictionary<string, MeshInstance3D>(tileMeshes);
}
public string Collapse(string tile)
{
if (collapsedMesh != null) return "";
if (tileMeshes.Keys.Count <= 0) return "ERR";
collapsedMesh = (tile.Length > 0) ? tile : ChooseWeighted();
tileMeshes.Clear();
return collapsedMesh;
}
private string ChooseWeighted()
{
float totalWeight = 0f;
foreach (string tile in tileMeshes.Keys)
{
totalWeight += WFC.weights[tile];
}
float r = (float)(rand.NextDouble() * totalWeight);
float cumulative = 0f;
foreach (string tile in tileMeshes.Keys)
{
cumulative += WFC.weights[tile];
if (r <= cumulative)
return tile;
}
return "junction";
}
public int Propagate(HashSet<string> possibleKeys)
{
int amountRemoved = 0;
if (collapsedMesh != null) return 0;
foreach (string key in tileMeshes.Keys.ToList())
{
if (!possibleKeys.Contains(key))
{
tileMeshes.Remove(key);
amountRemoved++;
}
}
if (tileMeshes.Count == 0) return int.MaxValue;
return amountRemoved;
}
public void Reset(Dictionary<string, MeshInstance3D> tileMeshes)
{
collapsedMesh = null;
SetMeshes(tileMeshes);
}
public void SpawnContent(Dictionary<string, MeshInstance3D> contentMeshes, Transform3D transform, List<Placeholder> placeholders)
{
foreach (Placeholder placeholder in placeholders)
{
if (containsLight && placeholder.name == "light") SpawnLight(contentMeshes["light"], placeholder, transform);
else if (containsResource && placeholder.name == "resource") SpawnResource(contentMeshes["resource"], placeholder, transform);
else if (containsDecoration && placeholder.name != "light" && placeholder.name != "resource") SpawnDecorations(contentMeshes, placeholder, transform);
}
}
private void SpawnLight(MeshInstance3D lightMesh, Placeholder placeholder, Transform3D transform)
{
MeshInstance3D light = new MeshInstance3D
{
Mesh = lightMesh.Mesh,
Position = placeholder.transform.Origin
};
OmniLight3D lightSource = new OmniLight3D()
{
OmniAttenuation = 2f,
LightColor = GameData.lightColor,
ShadowEnabled = true,
LightEnergy = 100f,
LightIndirectEnergy = 1.5f,
OmniRange = 20f,
Position = placeholder.transform.Origin
};
lightSource.Position.MoveToward(transform.Origin, 0.1f);
LightHandler.lights.Add(lightSource);
light.AddChild(lightSource);
ContentNode.AddChild(light);
light.LookAt(transform.Origin, Vector3.Up);
}
private void SpawnResource(MeshInstance3D resourceMesh, Placeholder placeholder, Transform3D transform)
{
MeshInstance3D resource = new MeshInstance3D
{
Mesh = resourceMesh.Mesh,
Position = placeholder.transform.Origin
};
ContentNode.AddChild(resource);
resource.LookAt(transform.Origin, Vector3.Up);
}
private void SpawnDecorations(Dictionary<string, MeshInstance3D> contentMeshes, Placeholder placeholder, Transform3D transform)
{
foreach (string key in contentMeshes.Keys)
{
if (key.ToLower() != placeholder.name) continue;
MeshInstance3D decoration = new MeshInstance3D
{
Mesh = contentMeshes[key].Mesh,
Position = placeholder.transform.Origin
};
ContentNode.AddChild(decoration);
decoration.LookAt(transform.Origin, Vector3.Up);
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://crimbrc78gxkc
+195
View File
@@ -0,0 +1,195 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
public class WFC
{
public static Dictionary<string, Dictionary<Direction, List<string>>> adjacency = new Dictionary<string, Dictionary<Direction, List<string>>>();
public static Random rand = new Random();
public enum Direction
{
Up,
Down,
Left,
Right,
None
}
public static readonly Vector2I[] offsets =
{
new Vector2I(0, -1),
new Vector2I(0, 1),
new Vector2I(-1, 0),
new Vector2I(1, 0)
};
public static readonly Direction[] dirs =
{
Direction.Up,
Direction.Down,
Direction.Left,
Direction.Right
};
public static Dictionary<string, HashSet<Direction>> tileConnections = new Dictionary<string, HashSet<Direction>>
{
["t_right"] = new() { Direction.Up, Direction.Down, Direction.Right },
["t_left"] = new() { Direction.Up, Direction.Down, Direction.Left },
["t_up"] = new() { Direction.Left, Direction.Right, Direction.Up },
["t_down"] = new() { Direction.Left, Direction.Right, Direction.Down },
["end_up"] = new() { Direction.Up },
["end_down"] = new() { Direction.Down },
["end_left"] = new() { Direction.Left },
["end_right"] = new() { Direction.Right },
["straight_left_right"] = new() { Direction.Left, Direction.Right },
["straight_up_down"] = new() { Direction.Up, Direction.Down },
["corner_up_left"] = new() { Direction.Up, Direction.Left },
["corner_up_right"] = new() { Direction.Up, Direction.Right },
["corner_down_left"] = new() { Direction.Down, Direction.Left },
["corner_down_right"] = new() { Direction.Down, Direction.Right },
["junction"] = new() { Direction.Up, Direction.Down, Direction.Left, Direction.Right },
["gate"] = new() { Direction.Up, Direction.Down, Direction.Left, Direction.Right }
};
public static Dictionary<string, float> weights = new()
{
["junction"] = 3f,
["t_up"] = 3f,
["t_down"] = 3f,
["t_left"] = 3f,
["t_right"] = 3f,
["straight_left_right"] = 2f,
["straight_up_down"] = 2f,
["corner_up_left"] = 0.7f,
["corner_up_right"] = 0.7f,
["corner_down_left"] = 0.7f,
["corner_down_right"] = 0.7f,
["end_up"] = 0.2f,
["end_down"] = 0.1f,
["end_left"] = 0.2f,
["end_right"] = 0.3f,
["gate"] = 0.1f
};
public static Direction Opposite(Direction dir)
{
return dir switch
{
Direction.Up => Direction.Down,
Direction.Down => Direction.Up,
Direction.Left => Direction.Right,
Direction.Right => Direction.Left,
_ => dir
};
}
public static bool CanConnect(string a, string b, Direction direction, bool checkWalking)
{
var aDirs = tileConnections[a];
var bDirs = tileConnections[b];
bool aOpen = aDirs.Contains(direction);
bool bOpen = bDirs.Contains(Opposite(direction));
if (checkWalking) return aOpen && bOpen;
return aOpen == bOpen;
}
public static void FillAdjacencies()
{
foreach (var tile in tileConnections.Keys)
{
adjacency[tile] = new Dictionary<Direction, List<string>>();
foreach (Direction dir in Enum.GetValues(typeof(Direction)))
{
var valid = new List<string>();
foreach (var other in tileConnections.Keys)
{
if (CanConnect(tile, other, dir, false))
valid.Add(other);
}
adjacency[tile][dir] = valid;
}
}
}
public static bool IsWalkable(Tile tile)
{
var dirs = tileConnections[tile.collapsedMesh];
return dirs.Count > 0 && !dirs.Contains(Direction.None);
}
public static bool CanWalk(Tile[,] layer, Vector2I from, Vector2I to, Direction dir)
{
var fromTile = layer[from.X, from.Y];
var toTile = layer[to.X, to.Y];
if (!IsWalkable(toTile))
return false;
return CanConnect(fromTile.collapsedMesh, toTile.collapsedMesh, dir, true);
}
public static bool IsMapConnected(Tile[,] layer, float accessibilityThreshhold)
{
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)
{
if (toCheck.Count <= 0) break;
int index = rand.Next(toCheck.Count);
position = toCheck[index];
toCheck[index] = toCheck[^1];
toCheck.RemoveAt(toCheck.Count - 1);
if (!visited.Add(position)) continue;
for (int i = 0; i < 4; i++)
{
var next = position + offsets[i];
if (!InBounds(next, layer.GetLength(0)))
continue;
if (CanWalk(layer, position, next, dirs[i]))
{
toCheck.Add(next);
}
}
safetyCounter++;
if (safetyCounter > layer.Length * 2) break;
if (visited.Count >= Math.Pow(layer.GetLength(0) - 1, 2) * accessibilityThreshhold)
{
result = true;
break;
}
}
return result;
}
public static bool InBounds(Vector2I pos, int layerSize)
{
return pos.X > 0 &&
pos.Y > 0 &&
pos.X < layerSize &&
pos.Y < layerSize;
}
}
+1
View File
@@ -0,0 +1 @@
uid://d3jw4gk5f8hhg
+182
View File
@@ -0,0 +1,182 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using static GameData;
public partial class World : Node3D
{
Random rand = new Random();
public Dictionary<string, MeshInstance3D> tileMeshes;
public Dictionary<string, MeshInstance3D> contentMeshes;
public Dictionary<string, List<Placeholder>> tilePlaceholders;
PackedScene layerPrefab = ResourceLoader.LoadLayerPrefab();
private Dictionary<string, MultiMeshInstance3D> multiMeshes = new();
private Dictionary<string, Mesh> meshLibrary = new();
Layer[] map;
Layer layerNode;
private MultiMeshHandler multiMeshHandler;
public override void _Ready()
{
WFC.FillAdjacencies();
tileMeshes = ResourceLoader.LoadTiles();
contentMeshes = ResourceLoader.LoadDecorations();
tilePlaceholders = new Dictionary<string, List<Placeholder>>();
foreach (var kvp in tileMeshes)
{
tilePlaceholders[kvp.Key] = new List<Placeholder>();
foreach (Node3D child in kvp.Value.GetChildren())
{
tilePlaceholders[kvp.Key].Add(new Placeholder(child.Name, child.Transform));
}
meshLibrary[kvp.Key] = kvp.Value.Mesh;
kvp.Value.QueueFree();
}
multiMeshes = CreateMultiMeshes(meshLibrary);
multiMeshHandler = new MultiMeshHandler(multiMeshes);
map = new Layer[ruinSize];
GenerateWorld();
HandleRenderData(BuildRenderData(0));
}
private Dictionary<string, MultiMeshInstance3D> CreateMultiMeshes(Dictionary<string, Mesh> meshLibrary)
{
var result = new Dictionary<string, MultiMeshInstance3D>();
foreach (var kvp in meshLibrary)
{
var mm = new MultiMesh
{
Mesh = kvp.Value,
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D
};
var instance = new MultiMeshInstance3D
{
Multimesh = mm
};
AddChild(instance);
result[kvp.Key] = instance;
}
return result;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("layer_up") && currentLayer > 0) currentLayer--;
if (Input.IsActionJustPressed("layer_down") && currentLayer < ruinSize - 1) currentLayer++;
if (currentLayer != visibleLayer)
{
map[visibleLayer].ClearDecorations();
HandleRenderData(BuildRenderData(currentLayer));
visibleLayer = currentLayer;
}
if (Input.IsActionJustPressed("spawn_robot") && robots.Count < maxRobotCount)
{
Robot robot = ResourceLoader.LoadRobotPrefab().Instantiate<Robot>();
robot.Position = map[0].tiles[rand.Next(layerSize), rand.Next(layerSize)].Position;
AddChild(robot);
robots.Add(robot);
}
}
private void GenerateWorld()
{
for (int layer = 0; layer < ruinSize; layer++)
{
layerNode = layerPrefab.Instantiate<Layer>();
AddChild(layerNode);
layerNode.SetupLayer(layerSize, layer, tileMeshes);
map[layer] = layerNode;
}
}
private List<TileRenderData> BuildRenderData(int layerIndex)
{
var result = new List<TileRenderData>();
Layer layer = map[layerIndex];
layer.ClearDecorations();
for (int x = 0; x < layerSize; x++)
{
for (int y = 0; y < layerSize; y++)
{
Tile tile = layer.tiles[x, y];
result.Add(new TileRenderData
{
Tile = tile,
MeshKey = tile.collapsedMesh,
Transform = new Transform3D(Basis.Identity, tile.Position),
Placeholders = tilePlaceholders[tile.collapsedMesh]
});
}
}
if (!layer.hasContentGenerated)
{
DistributeTileContent(layer);
layer.hasContentGenerated = true;
}
return result;
}
private void DistributeTileContent(Layer layer)
{
int currentDecoration = 0;
int currentLight = 0;
int currentResource = 0;
int posX, posY;
while(currentLight < layerSize * 3)
{
posX = rand.Next(layerSize);
posY = rand.Next(layerSize);
//Skip already placed lights and skip junction and gate as they do not contain lights
if(layer.tiles[posX, posY].collapsedMesh == "junction" || layer.tiles[posX, posY].collapsedMesh == "gate") continue;
if(layer.tiles[posX, posY].containsLight) continue;
layer.tiles[posX, posY].containsLight = true;
currentLight++;
}
while(currentDecoration < layerSize)
{
posX = rand.Next(layerSize);
posY = rand.Next(layerSize);
if(layer.tiles[posX, posY].containsDecoration) continue;
layer.tiles[posX, posY].containsDecoration = true;
currentDecoration++;
}
while(currentResource < layerSize)
{
posX = rand.Next(layerSize);
posY = rand.Next(layerSize);
if(layer.tiles[posX, posY].containsResource) continue;
layer.tiles[posX, posY].containsResource = true;
currentResource++;
}
}
private void HandleRenderData(List<TileRenderData> renderData)
{
multiMeshHandler.Build(renderData);
foreach (TileRenderData data in renderData)
{
data.Tile.SpawnContent(contentMeshes, data.Transform, data.Placeholders);
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://br2udyi6t8yvf