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