Added WFC and respective parts (Tile, World, Layer, WFC, ResourceLoader)
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user