Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MapGenerator : MonoBehaviour
- {
- #region Editor Exposed Data
- [SerializeField, Header("Settings")]
- private int width = 128;
- [SerializeField]
- private int height = 128;
- [SerializeField, Range(0,100)]
- private int randomFillPercent = 77;
- [SerializeField, ColorUsageAttribute(true, true)]
- private Color floorColor = new Color(5.25f, 0, 0, 1);
- [SerializeField, ColorUsageAttribute(true, true)]
- private Color wallColor = Color.black;
- [SerializeField, Header("RNG Settings")]
- private string seed = "QWERTY";
- [SerializeField]
- private bool useRandomSeed = false;
- [SerializeField, Header("RNG Fill Settings")]
- private float rngFillRate = 0.001f;
- [SerializeField, Header("Smoothing Settings")]
- private float smoothingRate = 0.001f;
- [SerializeField]
- private float smoothingIterations = 4;
- #endregion
- #region Public Data
- #endregion
- #region Private Data
- private int[,] _map;
- private Renderer _renderer = null;
- private Texture2D _texture = null;
- private System.Random _prng = null;
- private ProgressDisplay _progressDisplay = null;
- #endregion
- // Awaken my minions...
- void Awake()
- {
- _renderer = GetComponent<Renderer>();
- _progressDisplay = GameObject.FindGameObjectsWithTag("ProgressDisplay")[0].GetComponent<ProgressDisplay>();
- if (_progressDisplay == null)
- {
- Debug.LogError("Error:: Cannot find STAGE DISPLAY gameobject");
- }
- }
- // Start all the things...
- void Start()
- {
- _texture = new Texture2D(width, height);
- _renderer.material.EnableKeyword("_EMISSION");
- _renderer.material.SetTexture("_EmissionMap", _texture);
- _renderer.material.SetColor("_EmissionColor", floorColor);
- _renderer.material.SetTexture("_BaseMap", _texture);
- GenerateMap();
- floorColor.r = 4.75f;
- }
- private void GenerateMap()
- {
- // Init map array
- _map = new int[width, height];
- RandomFillMap();
- }
- private void RandomFillMap()
- {
- #region Setup RNG
- if (useRandomSeed)
- {
- seed = Time.time.ToString();
- }
- _prng = new System.Random(seed.GetHashCode());
- #endregion
- StartCoroutine(RandomFillMap_Step());
- }
- private IEnumerator RandomFillMap_Step()
- {
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
- {
- _map[x, y] = 1;
- }
- else
- {
- _map[x, y] = _prng.Next(0, 100) < randomFillPercent ? 1 : 0;
- }
- }
- UpdateMap();
- yield return new WaitForSeconds(rngFillRate);
- }
- yield return new WaitForSeconds(2);
- _progressDisplay.AdvanceStage();
- SmoothMap();
- }
- private void SmoothMap()
- {
- StartCoroutine(SmoothMap_Step());
- }
- IEnumerator SmoothMap_Step()
- {
- for (int i = 0; i < smoothingIterations; i++)
- {
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- int neighbourWallTiles = GetSurroundingWallTiles(x, y);
- if (neighbourWallTiles > 4)
- _map[x, y] = 1;
- else if (neighbourWallTiles < 4)
- _map[x, y] = 0;
- }
- UpdateMap();
- yield return new WaitForSeconds(smoothingRate);
- }
- UpdateMap();
- yield return new WaitForSeconds(1);
- }
- yield return new WaitForSeconds(1);
- _progressDisplay.AdvanceStage(true);
- }
- private int GetSurroundingWallTiles(int iGridX, int iGridY)
- {
- int wallCount = 0;
- for (int neighbourX = iGridX - 1; neighbourX <= iGridX + 1; neighbourX++)
- {
- for (int neighbourY = iGridY - 1; neighbourY <= iGridY + 1; neighbourY++)
- {
- if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height)
- {
- if (neighbourX != iGridX || neighbourY != iGridY)
- {
- wallCount += _map[neighbourX, neighbourY];
- }
- }
- else
- {
- wallCount++;
- }
- }
- }
- return wallCount;
- }
- // Update all the things
- void Update()
- {
- // Spin the plane
- transform.Rotate(Vector3.up * Time.deltaTime * 20);
- UpdateMap();
- floorColor.r = Mathf.Lerp(floorColor.r, 5.25f, 0.0025f);
- }
- void UpdateMap()
- {
- for (int y = 0; y < _texture.height; y++)
- {
- for (int x = 0; x < _texture.height; x++)
- {
- Color color = _map[x, y] == 0 ? floorColor : wallColor;
- _texture.SetPixel(x, y, color);
- }
- }
- _texture.Apply();
- _renderer.material.EnableKeyword("_EMISSION");
- _renderer.material.SetTexture("_EmissionMap", _texture);
- _renderer.material.SetColor("_EmissionColor", floorColor);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement