123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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
|