Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class GenerateTerrain : MonoBehaviour {
- public Transform grassTilePrefab;
- public Transform edgePiecePrefab;
- public Transform markerPrefab;
- public float generateWait = 1.0f;
- public int tileIterations = 4;
- [HideInInspector]public int gridsnap = 4;
- [HideInInspector]public float placeX = 0f;
- [HideInInspector]public float placeY = 0f;
- [HideInInspector]public int itI = 0;
- public float tileStartChanceX = 0.45f;
- public float tileStartChanceY = 0.45f;
- [HideInInspector]public float tileChanceHorizontal = 0.45f;
- [HideInInspector]public float tileChanceVertical = 0.45f;
- [HideInInspector]public float tileChangeSmudgeFactorHorizontal = 0.2f;
- [HideInInspector]public float tileChangeSmudgeFactorVertical = 0.05f;
- [HideInInspector]public List<Transform> tileQueue = new List<Transform>();
- [HideInInspector]public List<Transform> tilePostProcessing = new List<Transform>();
- [HideInInspector]public List<Transform> tempTileQueue = new List<Transform>();
- [HideInInspector]public bool looping = true;
- public Transform[] terrainPieces;
- public int postProcessPlace = 0;
- void Start () {
- Random.seed = 42;
- //make the first tile, and add it to the queue
- Transform startTile = (Transform)Instantiate(grassTilePrefab,new Vector2(placeX,placeY),Quaternion.identity);
- foreach(Transform child in startTile){ //cycle through all tile children and add them to the list
- tilePostProcessing.Add(child);
- child.GetComponent<grassTileStats>().myPlaceInLine = postProcessPlace;
- postProcessPlace++;
- }
- tileQueue.Add(startTile);
- StartCoroutine("GenerationX");
- }
- IEnumerator GenerationX(){
- while(looping == true){
- print ("Cycling");
- foreach(Transform tile in tileQueue){
- print ("There's a tile");
- CheckCardinalDirection(new Vector2(tile.position.x,tile.position.y));
- yield return new WaitForSeconds(generateWait);
- //tile.collider2D.enabled = false;
- }
- tileQueue.Clear();
- if (tempTileQueue.Count == 0){ //Ran out of tiles
- print ("GENERATION HALTED PREMATURELY");
- looping = false;
- }
- foreach(Transform tile in tempTileQueue){ //add the temp list into the main list
- tileQueue.Add(tile);
- }
- tempTileQueue.Clear();//delete the temp list
- print ("Checking for loop break");
- if (itI++ >= tileIterations){
- print("LOOP IS BROKEN");
- looping = false;
- }
- else{
- float tempInttoFloat = (float)itI / (float)tileIterations;
- tileChanceHorizontal = (1 - tempInttoFloat) * tileStartChanceX;
- tileChanceVertical = (1 - tempInttoFloat) * tileStartChanceY;
- }
- //yield return new WaitForSeconds(generateWait);
- }
- int tileGrassCount = GameObject.FindGameObjectsWithTag("grassTile").Length;
- print ("Postprocessing Queue: "+ tileGrassCount.ToString() + " tiles, " + tilePostProcessing.Count.ToString() + " pieces");
- print ("Begin postprocessing");
- StartCoroutine("GenerationY");
- }
- bool CheckForCollision(Vector2 pos){ //return true if there is a collision
- Collider2D[] hitColliders = Physics2D.OverlapCircleAll(pos,1);
- if (hitColliders.Length > 1){
- return true;
- }
- else{
- return false;
- }
- }
- void CheckCardinalDirection(Vector2 pos){
- List<Vector2> cardinals = new List<Vector2>();
- cardinals.Add(new Vector2(pos.x + gridsnap,pos.y)); //East
- cardinals.Add(new Vector2(pos.x - gridsnap,pos.y)); //West
- cardinals.Add(new Vector2(pos.x,pos.y + gridsnap)); //North
- cardinals.Add(new Vector2(pos.x,pos.y - gridsnap)); //South
- for(int i = 0; i < cardinals.Count; i ++){
- print ("Iteration: "+i.ToString());
- if (!CheckForCollision(cardinals[i])){
- float chance = (i < 2) ? tileChanceHorizontal : tileChanceVertical;
- print ("Chance: "+chance.ToString());
- if (Random.value < chance){
- Transform startTile = (Transform)Instantiate(grassTilePrefab,cardinals[i],Quaternion.identity);
- foreach(Transform child in startTile){ //cycle through all tile children and add them to the list
- tilePostProcessing.Add(child);
- child.GetComponent<grassTileStats>().myPlaceInLine = postProcessPlace;
- postProcessPlace++;
- }
- tempTileQueue.Add(startTile);
- }
- }
- }
- }
- IEnumerator GenerationY(){ //add edge pieces
- //itI = 0;
- foreach(Transform tile in tilePostProcessing){
- CheckEightDirections(tile,new Vector2(tile.position.x,tile.position.y));
- yield return new WaitForSeconds(generateWait);
- }
- print ("Postprocessing complete");
- }
- void CheckEightDirections(Transform owner, Vector2 pos){
- //check each of 8 directions, and concantenate a string of 1s and 0s
- string tileSurrounds = "";
- gridsnap = 2;
- List<Vector2> eightDirections = new List<Vector2>();
- //10101111
- eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y)); //East
- eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y + gridsnap)); //North East
- eightDirections.Add(new Vector2(pos.x,pos.y + gridsnap)); //North
- eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y + gridsnap)); //North West
- eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y)); //West
- eightDirections.Add(new Vector2(pos.x - gridsnap,pos.y - gridsnap)); //South West
- eightDirections.Add(new Vector2(pos.x,pos.y - gridsnap)); //South
- eightDirections.Add(new Vector2(pos.x + gridsnap,pos.y - gridsnap)); //South East
- for(int i = 0; i < eightDirections.Count; i ++){
- Transform marker = (Transform)Instantiate(markerPrefab,eightDirections[i],Quaternion.identity);
- if (!CheckForCollision(eightDirections[i])){
- //Add a 0
- Transform startTile = (Transform)Instantiate(edgePiecePrefab,eightDirections[i],Quaternion.identity);
- //startTile.GetComponent<grassTileStats>().myPlaceInLine = itI;
- startTile.GetComponent<grassTileStats>().myPlaceInLine = owner.GetComponent<grassTileStats>().myPlaceInLine;
- //itI++;
- tileSurrounds+="0";
- }
- else{
- //Add a 1
- tileSurrounds+="1";
- }
- }
- //int test = owner.GetComponent<grassTileStats>().myPlaceInLine;
- print ("PieceID "+owner.GetComponent<grassTileStats>().myPlaceInLine.ToString()+ " scored " + tileSurrounds);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement