Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class TerrainSpawner : MonoBehaviour
- {
- public GameObject SOLID_BLOCK_PREFAB;
- public Vector2 roomSize;
- public Vector2 roomGenLocation;
- public int CHANCE_UP1, CHANCE_DOWN1;
- public int startGen_offset, end_offset;
- private enum PrevBlock
- {
- WENT_UP,
- WENT_DOWN,
- STAYED
- }
- //next is used to force blocks when they have reached a limit
- private enum NextBlock
- {
- GO_UP,
- GO_DOWN,
- STAY,
- }
- NextBlock next;
- PrevBlock prev;
- private bool BoundsCheck(Vector3 a, Vector3 b)
- {
- return (a.y < b.y - 1 && a.y > b.y - (roomSize.y / 2) + 1);
- }
- private bool BoundsCheck2(Vector3 a, Vector3 b)
- {
- return (a.y > b.y + 1 && a.y < b.y + (roomSize.y / 2) - 1);
- }
- void Start()
- {
- Random.seed = (int)System.DateTime.Now.Ticks;
- if (CHANCE_UP1 + CHANCE_DOWN1 != 100)
- {
- Debug.Log("Percentages must add up to 100%");
- return;
- }
- else if (startGen_offset >= roomSize.x)
- {
- Debug.Log("Your starting offset cannot be more than horizontal roomsize");
- return;
- }
- prev = PrevBlock.STAYED;
- next = NextBlock.STAY;
- GameObject upBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x, roomGenLocation.y + roomSize.y / 2f), Quaternion.identity) as GameObject;
- upBlock.transform.localScale = new Vector3(roomSize.x - 1f, 1f, 1f);
- upBlock.gameObject.name = "UpBlock";
- GameObject downBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x, roomGenLocation.y - roomSize.y / 2f), Quaternion.identity) as GameObject;
- downBlock.transform.localScale = new Vector3(roomSize.x - 1f, 1f, 1f);
- downBlock.gameObject.name = "DownBlock";
- GameObject rightBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x + roomSize.x / 2f, roomGenLocation.y), Quaternion.identity) as GameObject;
- rightBlock.transform.localScale = new Vector3(1f, roomSize.y + 1f, 1f);
- rightBlock.gameObject.name = "RightBlock";
- GameObject leftBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x - roomSize.x / 2f, roomGenLocation.y), Quaternion.identity) as GameObject;
- leftBlock.transform.localScale = new Vector3(1f, roomSize.y + 1f, 1f);
- leftBlock.gameObject.name = "LeftBlock";
- Vector3 prevPos = new Vector3(leftBlock.transform.position.x + startGen_offset, downBlock.transform.position.y + 1);
- prev = PrevBlock.STAYED;
- next = NextBlock.STAY;
- for (; prevPos.x < rightBlock.transform.position.x - end_offset; prevPos.x++)
- {
- for (float b = downBlock.transform.position.y + 1; b < (int)roomGenLocation.y + 1; b++)
- {
- if ((Random.Range(1, 101) < CHANCE_UP1 && BoundsCheck(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_UP))
- || next == NextBlock.GO_UP)
- {// nextblock goes up by 1
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y + 1f), Quaternion.identity) as GameObject;
- prevPos = tempBlock.transform.position;
- tempBlock.gameObject.name = "wentUp";
- prev = PrevBlock.WENT_UP;
- next = NextBlock.STAY;
- break;
- }
- else if ((Random.Range(1, 101) < CHANCE_DOWN1 && BoundsCheck(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_DOWN))
- || next == NextBlock.GO_DOWN)
- { // nextblock goes down by 1
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y - 1f), Quaternion.identity) as GameObject;
- prevPos = tempBlock.transform.position;
- tempBlock.gameObject.name = "wentDown";
- prev = PrevBlock.WENT_DOWN;
- next = NextBlock.STAY;
- break;
- }
- else
- {//height stays
- if (prevPos.y >= roomGenLocation.y - 1) // IF I HIT A LIMIT
- {
- next = NextBlock.GO_DOWN;
- }
- else if (prevPos.y <= roomGenLocation.y - (roomSize.y / 2) + 1) // IF I HIT A LIMIT
- {
- next = NextBlock.GO_UP;
- }
- else
- {
- prev = PrevBlock.STAYED;
- }
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y), Quaternion.identity) as GameObject;
- tempBlock.gameObject.name = "stayed";
- prevPos = tempBlock.transform.position;
- break;
- }
- }
- }
- prevPos = new Vector3(leftBlock.transform.position.x + startGen_offset, upBlock.transform.position.y - 1);
- for (; prevPos.x < rightBlock.transform.position.x - end_offset; prevPos.x++)
- {
- for (float b = downBlock.transform.position.y + 1; b < (int)roomGenLocation.y + 1; b++)
- {
- if ((Random.Range(1, 101) < CHANCE_UP1 && BoundsCheck2(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_UP))
- || next == NextBlock.GO_UP)
- {// nextblock goes up by 1
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y + 1f), Quaternion.identity) as GameObject;
- prevPos = tempBlock.transform.position;
- tempBlock.gameObject.name = "wentUp";
- prev = PrevBlock.WENT_UP;
- next = NextBlock.STAY;
- break;
- }
- else if ((Random.Range(1, 101) < CHANCE_DOWN1 && BoundsCheck2(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_DOWN))
- || next == NextBlock.GO_DOWN)
- { // nextblock goes down by 1
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y - 1f), Quaternion.identity) as GameObject;
- prevPos = tempBlock.transform.position;
- tempBlock.gameObject.name = "wentDown";
- prev = PrevBlock.WENT_DOWN;
- next = NextBlock.STAY;
- break;
- }
- else
- {//height stays
- if (prevPos.y >= upBlock.transform.position.y - 1) // IF I HIT A LIMIT
- {
- next = NextBlock.GO_DOWN;
- }
- else if (prevPos.y <= roomGenLocation.y + 1) // IF I HIT A LIMIT
- {
- next = NextBlock.GO_UP;
- }
- else
- {
- prev = PrevBlock.STAYED;
- }
- GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y), Quaternion.identity) as GameObject;
- tempBlock.gameObject.name = "stayed";
- prevPos = tempBlock.transform.position;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement