using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class Tile { public Dictionary tileMeshes; public string collapsedMesh; Random rand = new Random(); public Vector3 Position; public Vector2I GridPosition; public Node3D DecorationNode; public void SetMeshes(Dictionary tileMeshes) { this.tileMeshes = new Dictionary(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 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 tileMeshes) { collapsedMesh = null; SetMeshes(tileMeshes); } }