- Home /
 
               Question by 
               MadmanSilver · Mar 02, 2015 at 12:51 PM · 
                randomproceduralgenerationtilesoverlapping  
              
 
              Overlapping Tile Generation
Ok, so for some reason that I cannot figure out, my generation script keeps overlapping itself. It is a very basic tile based generation script, but every time I run it, it spawns a tile where one already exists. If anybody can figure out why, please do.
Generator:
 public class Generator : MonoBehaviour {
     
     public GameObject tile;
     public bool done = false;
     public int size = 10;
 
     private GridCords origin = new GridCords(0, 0);
     private GridCords current;
     private GridCords last;
     public List<GridCords> hist = new List<GridCords>();
     private GridCords[] choices = new GridCords[4];
     private GameObject newTile;
     private bool first = true;
     private int past = 0;
 
     // Initialize stuff.
     public void Start() {
         current = origin;
         last = origin;
         choices[0] = new GridCords(last.GetX() + 1, last.GetY());
         choices[1] = new GridCords(last.GetX() - 1, last.GetY());
         choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
         choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
     }
     
     public void Generate() {
         if (size < 1) {
             done = true;
             return;
         }
 
         if (size == 9) {
             first = false;
         }
 
         if (first) {
             newTile = (GameObject)Instantiate(tile, origin.ToVect(), tile.transform.rotation);
             hist.Add(origin);
             last = origin;
             size--;
         } else {
             // Wait a bit.
             //Wait(2.0f);
             
             // Prepare for generation.
             choices[0] = new GridCords(last.GetX() + 1, last.GetY());
             choices[1] = new GridCords(last.GetX() - 1, last.GetY());
             choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
             choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
             
             // Generate.
             foreach (GridCords gc in choices) {
                 if (hist.Contains(gc)) {
                     past++;
                 }
             }
 
             if (past == 4) {
                 done = true;
                 return;
             }
 
             current = choices[Random.Range(0, 3)];
 
             while (hist.Contains(current)) {
                 current = choices[Random.Range(0, 3)];
             }
 
             newTile = (GameObject)Instantiate(tile, current.ToVect(), tile.transform.rotation);
             
             // Post-generation.
             hist.Add(current);
             last = current;
             size--;
             past = 0;
         }
     }
 
     private IEnumerator Wait(float secs) {
         yield return new WaitForSeconds(secs);
     }
 }
And here is the GridCords script:
 public class GridCords {
 
     private int x, y;
 
     public GridCords(int x, int y) {
         this.x = x;
         this.y = y;
     }
 
     // Creates world co-ordinates using the grid co-ordinates specified.
     public Vector3 ToVect() {
         return new Vector3(y * 10, 0, x * 10);
     }
 
     public int GetX() {
         return x;
     }
 
     public int GetY() {
         return y;
     }
 }
This class is mainly for a bit of organization in the code.
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                