Advertisement
Guest User

GenerateTerrain

a guest
Sep 7th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GenerateTerrain : MonoBehaviour {
  6.  
  7.     public Transform grassTilePrefab;
  8.     public Transform edgePiecePrefab;
  9.     public Transform markerPrefab;
  10.     public float generateWait = 1.0f;
  11.     public int tileIterations = 4;
  12.     [HideInInspector]public int gridsnap = 4;
  13.     [HideInInspector]public float placeX = 0f;
  14.     [HideInInspector]public float placeY = 0f;
  15.     [HideInInspector]public int itI = 0;
  16.     public float tileStartChanceX = 0.45f;
  17.     public float tileStartChanceY = 0.45f;
  18.     [HideInInspector]public float tileChanceHorizontal = 0.45f;
  19.     [HideInInspector]public float tileChanceVertical = 0.45f;
  20.     [HideInInspector]public float tileChangeSmudgeFactorHorizontal = 0.2f;
  21.     [HideInInspector]public float tileChangeSmudgeFactorVertical = 0.05f;
  22.     [HideInInspector]public List<Transform> tileQueue = new List<Transform>();
  23.     [HideInInspector]public List<Transform> tilePostProcessing = new List<Transform>();
  24.     [HideInInspector]public List<Transform> tempTileQueue = new List<Transform>();
  25.     [HideInInspector]public bool looping = true;
  26.     public Transform[] terrainPieces;
  27.     public int postProcessPlace = 0;
  28.  
  29.     void Start () {
  30.         Random.seed = 42;
  31.         //make the first tile, and add it to the queue
  32.         Transform startTile = (Transform)Instantiate(grassTilePrefab,new Vector2(placeX,placeY),Quaternion.identity);
  33.         foreach(Transform child in startTile){ //cycle through all tile children and add them to the list
  34.             tilePostProcessing.Add(child);
  35.             child.GetComponent<grassTileStats>().myPlaceInLine = postProcessPlace;
  36.             postProcessPlace++;
  37.         }
  38.         tileQueue.Add(startTile);
  39.         StartCoroutine("GenerationX");
  40.     }
  41.  
  42.     IEnumerator GenerationX(){
  43.         while(looping == true){
  44.             print ("Cycling");
  45.             foreach(Transform tile in tileQueue){
  46.                 print ("There's a tile");
  47.                 CheckCardinalDirection(new Vector2(tile.position.x,tile.position.y));
  48.                 yield return new WaitForSeconds(generateWait);
  49.                 //tile.collider2D.enabled = false;
  50.             }
  51.            
  52.             tileQueue.Clear();
  53.  
  54.             if (tempTileQueue.Count == 0){ //Ran out of tiles
  55.                 print ("GENERATION HALTED PREMATURELY");
  56.                 looping = false;
  57.             }
  58.            
  59.             foreach(Transform tile in tempTileQueue){ //add the temp list into the main list
  60.                 tileQueue.Add(tile);
  61.             }
  62.            
  63.             tempTileQueue.Clear();//delete the temp list
  64.  
  65.             print ("Checking for loop break");
  66.             if (itI++ >= tileIterations){
  67.                 print("LOOP IS BROKEN");
  68.                 looping = false;
  69.             }
  70.             else{
  71.                 float tempInttoFloat = (float)itI / (float)tileIterations;
  72.                 tileChanceHorizontal = (1 - tempInttoFloat) * tileStartChanceX;
  73.                 tileChanceVertical = (1 - tempInttoFloat) * tileStartChanceY;
  74.             }
  75.             //yield return new WaitForSeconds(generateWait);
  76.         }
  77.  
  78.         int tileGrassCount = GameObject.FindGameObjectsWithTag("grassTile").Length;
  79.         print ("Postprocessing Queue: "+ tileGrassCount.ToString() + " tiles, " + tilePostProcessing.Count.ToString() + " pieces");
  80.         print ("Begin postprocessing");
  81.         StartCoroutine("GenerationY");
  82.     }
  83.  
  84.  
  85.  
  86.     bool CheckForCollision(Vector2 pos){ //return true if there is a collision
  87.         Collider2D[] hitColliders = Physics2D.OverlapCircleAll(pos,1);
  88.         if (hitColliders.Length > 1){
  89.             return true;
  90.         }
  91.         else{
  92.             return false;
  93.         }
  94.     }
  95.  
  96.     void CheckCardinalDirection(Vector2 pos){
  97.  
  98.         List<Vector2> cardinals = new List<Vector2>();
  99.         cardinals.Add(new Vector2(pos.x + gridsnap,pos.y)); //East
  100.         cardinals.Add(new Vector2(pos.x - gridsnap,pos.y)); //West
  101.         cardinals.Add(new Vector2(pos.x,pos.y + gridsnap)); //North
  102.         cardinals.Add(new Vector2(pos.x,pos.y - gridsnap)); //South
  103.  
  104.         for(int i = 0; i < cardinals.Count; i ++){
  105.             print ("Iteration: "+i.ToString());
  106.             if (!CheckForCollision(cardinals[i])){
  107.                 float chance = (i < 2) ? tileChanceHorizontal : tileChanceVertical;
  108.                 print ("Chance: "+chance.ToString());
  109.                 if (Random.value < chance){
  110.                     Transform startTile = (Transform)Instantiate(grassTilePrefab,cardinals[i],Quaternion.identity);
  111.                     foreach(Transform child in startTile){ //cycle through all tile children and add them to the list
  112.                         tilePostProcessing.Add(child);
  113.                         child.GetComponent<grassTileStats>().myPlaceInLine = postProcessPlace;
  114.                         postProcessPlace++;
  115.                     }
  116.                     tempTileQueue.Add(startTile);
  117.                 }
  118.  
  119.             }
  120.         }
  121.  
  122.     }
  123.  
  124.     IEnumerator GenerationY(){ //add edge pieces
  125.         //itI = 0;
  126.         foreach(Transform tile in tilePostProcessing){
  127.             CheckEightDirections(tile,new Vector2(tile.position.x,tile.position.y));
  128.             yield return new WaitForSeconds(generateWait);
  129.         }
  130.         print ("Postprocessing complete");
  131.     }
  132.  
  133.     void CheckEightDirections(Transform owner, Vector2 pos){
  134.         //check each of 8 directions, and concantenate a string of 1s and 0s
  135.         string tileSurrounds = "";
  136.         gridsnap = 2;
  137.         List<Vector2> eightDirections = new List<Vector2>();
  138.  
  139.         //10101111
  140.         eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y)); //East
  141.         eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y + gridsnap)); //North East
  142.         eightDirections.Add(new Vector2(pos.x,pos.y + gridsnap)); //North
  143.         eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y + gridsnap)); //North West
  144.         eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y)); //West
  145.         eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y - gridsnap)); //South West
  146.         eightDirections.Add(new Vector2(pos.x,pos.y - gridsnap)); //South
  147.         eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y - gridsnap)); //South East
  148.  
  149.         for(int i = 0; i < eightDirections.Count; i ++){
  150.             Transform marker = (Transform)Instantiate(markerPrefab,eightDirections[i],Quaternion.identity);
  151.             if (!CheckForCollision(eightDirections[i])){
  152.                 //Add a 0
  153.                 Transform startTile = (Transform)Instantiate(edgePiecePrefab,eightDirections[i],Quaternion.identity);
  154.                 //startTile.GetComponent<grassTileStats>().myPlaceInLine = itI;
  155.                 startTile.GetComponent<grassTileStats>().myPlaceInLine = owner.GetComponent<grassTileStats>().myPlaceInLine;
  156.                 //itI++;
  157.                 tileSurrounds+="0";
  158.             }
  159.             else{
  160.                 //Add a 1
  161.                 tileSurrounds+="1";
  162.             }
  163.         }
  164.         //int test = owner.GetComponent<grassTileStats>().myPlaceInLine;
  165.         print ("PieceID "+owner.GetComponent<grassTileStats>().myPlaceInLine.ToString()+ " scored " + tileSurrounds);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement