SimpleTiledWFC.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. [ExecuteInEditMode]
  9. public class SimpleTiledWFC : MonoBehaviour{
  10. public TextAsset xml = null;
  11. private string subset = "";
  12. public int gridsize = 1;
  13. public int width = 20;
  14. public int depth = 20;
  15. public int seed = 0;
  16. public bool periodic = false;
  17. public int iterations = 0;
  18. public bool incremental;
  19. public SimpleTiledModel model = null;
  20. public GameObject[,] rendering;
  21. public GameObject output;
  22. private Transform group;
  23. public Dictionary<string, GameObject> obmap = new Dictionary<string, GameObject>();
  24. private bool undrawn = true;
  25. public void destroyChildren (){
  26. foreach (Transform child in this.transform) {
  27. GameObject.DestroyImmediate(child.gameObject);
  28. }
  29. }
  30. void Start(){
  31. Generate();
  32. Run();
  33. }
  34. void Update(){
  35. if (incremental){
  36. Run();
  37. }
  38. }
  39. public void Run(){
  40. if (model == null){return;}
  41. if (undrawn == false) { return; }
  42. if (model.Run(seed, iterations)){
  43. Draw();
  44. }
  45. }
  46. public void OnDrawGizmos(){
  47. Gizmos.matrix = transform.localToWorldMatrix;
  48. Gizmos.color = Color.magenta;
  49. Gizmos.DrawWireCube(new Vector3(width*gridsize/2f-gridsize*0.5f, depth*gridsize/2f-gridsize*0.5f, 0f),new Vector3(width*gridsize, depth*gridsize, gridsize));
  50. }
  51. public void Generate(){
  52. obmap = new Dictionary<string, GameObject>();
  53. if (output == null){
  54. Transform ot = transform.Find("output-tiled");
  55. if (ot != null){output = ot.gameObject;}}
  56. if (output == null){
  57. output = new GameObject("output-tiled");
  58. output.transform.parent = transform;
  59. output.transform.position = this.gameObject.transform.position;
  60. output.transform.rotation = this.gameObject.transform.rotation;}
  61. for (int i = 0; i < output.transform.childCount; i++){
  62. GameObject go = output.transform.GetChild(i).gameObject;
  63. if (Application.isPlaying){Destroy(go);} else {DestroyImmediate(go);}
  64. }
  65. group = new GameObject(xml.name).transform;
  66. group.parent = output.transform;
  67. group.position = output.transform.position;
  68. group.rotation = output.transform.rotation;
  69. group.localScale = new Vector3(1f, 1f, 1f);
  70. rendering = new GameObject[width, depth];
  71. this.model = new SimpleTiledModel(xml.text, subset, width, depth, periodic);
  72. undrawn = true;
  73. }
  74. public void Draw(){
  75. if (output == null){return;}
  76. if (group == null){return;}
  77. undrawn = false;
  78. for (int y = 0; y < depth; y++){
  79. for (int x = 0; x < width; x++){
  80. if (rendering[x,y] == null){
  81. string v = model.Sample(x, y);
  82. int rot = 0;
  83. GameObject fab = null;
  84. if (v != "?"){
  85. rot = int.Parse(v.Substring(0,1));
  86. v = v.Substring(1);
  87. if (!obmap.ContainsKey(v)){
  88. fab = (GameObject)Resources.Load(v, typeof(GameObject));
  89. obmap[v] = fab;
  90. } else {
  91. fab = obmap[v];
  92. }
  93. if (fab == null){
  94. continue;}
  95. Vector3 pos = new Vector3(x*gridsize, y*gridsize, 0f);
  96. GameObject tile = (GameObject)Instantiate(fab, new Vector3() , Quaternion.identity);
  97. Vector3 fscale = tile.transform.localScale;
  98. tile.transform.parent = group;
  99. tile.transform.localPosition = pos;
  100. tile.transform.localEulerAngles = new Vector3(0, 0, 360-(rot*90));
  101. tile.transform.localScale = fscale;
  102. rendering[x,y] = tile;
  103. } else
  104. {
  105. undrawn = true;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. #if UNITY_EDITOR
  113. [CustomEditor (typeof(SimpleTiledWFC))]
  114. public class TileSetEditor : Editor {
  115. public override void OnInspectorGUI () {
  116. SimpleTiledWFC me = (SimpleTiledWFC)target;
  117. if (me.xml != null){
  118. if(GUILayout.Button("generate")){
  119. me.Generate();
  120. }
  121. if (me.model != null){
  122. if(GUILayout.Button("RUN")){
  123. me.model.Run(me.seed, me.iterations);
  124. me.Draw();
  125. }
  126. }
  127. }
  128. DrawDefaultInspector ();
  129. }
  130. }
  131. #endif