Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEditor;
- public class LevelGenerator : MonoBehaviour
- {
- public GameObject layoutRoom; // The room prefab to generate.
- public Color startColor, endColor; // Colors for the start and end rooms.
- public int distanceToEnd; // Number of rooms generated.
- public Transform generatorPoint; // The distance between rooms.
- public enum Direction { up, right, down, left };
- public Direction selectedDirection;
- // Dimensions of each room - based off of camera size.
- public float xOffset = 18f, yOffset = 10f;
- public LayerMask whatIsRoom;
- //private Vector2Int startCoords = Vector2Int.zero, endCoords;
- [SerializeField]
- private List<GameObject> layoutRoomObjects = new List<GameObject>();
- //[SerializeField]
- //private List<Vector2Int> roomCoords = new List<Vector2Int>();
- public enum RoomVariations { SingleN, SingleE, SingleS, SingleW, DoubleNE, DoubleSE, DoubleSW, DoubleNW, DoubleNS, DoubleEW, TripleN, TripleE, TripleS, TripleW, Intersection };
- public RoomVariations variation;
- Direction entrance;
- // Start is called before the first frame update
- void Start()
- {
- //Decide upon direction of dungeon entrance in the start room.
- entrance = (Direction)Random.Range(0, 4);
- // Generates the map.
- for (int i = 0; i < distanceToEnd; i++)
- {
- GameObject newRoom = Instantiate(layoutRoom, generatorPoint.position, generatorPoint.rotation); // Generates a new room.
- newRoom.name = GenerateRoomName(newRoom); // Names the room based on an x,y coordinate system.
- layoutRoomObjects.Add(newRoom); // Add the room to the room database.
- // Tagging of start and end rooms.
- if (i == 0)
- {
- newRoom.GetComponent<SpriteRenderer>().color = startColor;
- //doorways.AssignCoords(0, 0);
- }
- else if (i + 1 == distanceToEnd)
- newRoom.GetComponent<SpriteRenderer>().color = endColor;
- Vector3 lastPos;
- while (Physics2D.OverlapCircle(generatorPoint.position, .2f, whatIsRoom))
- {
- lastPos = generatorPoint.position;
- // Pick a random direction to create the next room.
- selectedDirection = AssignNextDirection(newRoom);
- MoveGenerationPoint();
- if (Physics2D.OverlapCircle(generatorPoint.position, .2f, whatIsRoom))
- generatorPoint.position = lastPos;
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- SceneManager.LoadScene("Test");
- }
- }
- // Method for determining what direction to spawn the next room.
- public void MoveGenerationPoint()
- {
- Vector3[] dir = new[] { new Vector3(0f, yOffset, 0f), new Vector3(xOffset, 0f, 0f), new Vector3(0f, -yOffset, 0f), new Vector3(-xOffset, 0f, 0f) };
- for (int i = 0; i < 4; i++)
- if (selectedDirection == (Direction)i)
- generatorPoint.position += dir[(int)selectedDirection];
- }
- private string GenerateRoomName(GameObject _room)
- {
- int _x, _y;
- if (_room.transform.position.x != 0)
- _x = (int)(_room.transform.position.x / xOffset);
- else _x = 0;
- if (_room.transform.position.y != 0)
- _y = (int)(_room.transform.position.y / yOffset);
- else _y = 0;
- return string.Format("Room {0}, {1}", _x, _y);
- }
- private Vector2Int GetCoords(GameObject _room)
- {
- string _name = _room.name;
- string[] _coords = Regex.Split(_name, @"\D+");
- // _coords[0] is "" for some reason.
- int _x = int.Parse(_coords[1]),
- _y = int.Parse(_coords[2]);
- return new Vector2Int(_x, _y);
- }
- private Direction AssignNextDirection(GameObject _room)
- {
- Direction _next = 0; // Ultimately this will be the direction for the next generated room.
- bool _rerollDirection = false; // This determines whether or not the generator is trying to generate past the entrance direction.
- Vector2Int _coords = GetCoords(_room);
- /* How this works:
- * If the dungeon entrance is placed at the top of the room, that means no room y-coords should positively exceed 0.
- * Vice Versa applies: If the enrances is at the bottom, then no room y-coords should negatively exceed 0.
- * The same rules applies for x-coords regarding eastern and western dungeon entrances.
- */
- while (!_rerollDirection)
- {
- _next = (Direction)Random.Range(0, 4);
- print(_next);
- switch (entrance)
- {
- case Direction.up:
- if (_coords.y + 1 > 0 && _next == Direction.up)
- {
- _rerollDirection = false;
- //print(_coords + " wrong turn going up!");
- }
- else
- _rerollDirection = true;
- break;
- case Direction.right:
- if (_coords.x + 1 > 0 && _next == Direction.right)
- {
- _rerollDirection = false;
- //print(_coords + " wrong turn going right!");
- }
- else
- _rerollDirection = true;
- break;
- case Direction.down:
- if (_coords.y - 1 < 0 && _next == Direction.down)
- {
- _rerollDirection = false;
- //print(_coords + " wrong turn going down!");
- }
- else
- _rerollDirection = true;
- break;
- case Direction.left:
- if (_coords.x - 1 < 0 && _next == Direction.left)
- {
- _rerollDirection = false;
- //print(_coords + " wrong turn going left!");
- }
- else
- _rerollDirection = true;
- break;
- }
- }
- //print("Next room will be at " + _next + " from " + _coords);
- return _next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment