From fe3c8a5515acdfcc50913d5b83de8f9504e95b73 Mon Sep 17 00:00:00 2001 From: miepzerino <o.skotnik@gmail.com> Date: Sat, 23 Dec 2023 23:21:22 +0000 Subject: [PATCH] Added map save/load + seed generation --- Assets/GenerateTileMap.cs | 63 +++++++++++++++++++++++++------ 1 files changed, 51 insertions(+), 12 deletions(-) diff --git a/Assets/GenerateTileMap.cs b/Assets/GenerateTileMap.cs index 4239562..46742ed 100644 --- a/Assets/GenerateTileMap.cs +++ b/Assets/GenerateTileMap.cs @@ -1,31 +1,66 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; +using UnityEngine.UIElements; public class GenerateTileMap : MonoBehaviour { + public int? seed; public int width = 256; public int height = 256; - public float scale = 20f; - public float offsetX = 0f; - public float offsetY = 0f; + private float scale; + private float offsetX; + private float offsetY; Tilemap tilemap; public RuleTile ruleTile; //public List<TileBase> tiles; - // Start is called before the first frame update - void Start() + + private void Awake() { tilemap = GetComponent<Tilemap>(); - GenerateTiles(); +#if DEBUG + //seed = 0123456789; +#endif + if (seed == null) + { + seed = GenerateSeed(9); + } + + SetSettingsFromSeed(seed.Value); } + public void SetSettingsFromSeed(int seed) + { + UnityEngine.Random.State randomState = UnityEngine.Random.state; - public void GenerateTiles() + UnityEngine.Random.InitState(seed); + scale = UnityEngine.Random.Range(17f, 23f); + offsetX = UnityEngine.Random.Range(-10000f, 10000f); + offsetY = UnityEngine.Random.Range(-10000f, 10000f); + + UnityEngine.Random.state = randomState; + + } + + private int GenerateSeed(int size) + { + System.Random rand = new System.Random(); + string seedNumbers = "0123456789"; + char[] chars = new char[size]; + for (int i = 0; i < size; i++) + { + chars[i] = seedNumbers[rand.Next(seedNumbers.Length)]; + } + return int.Parse(new string(chars)); + } + + public IEnumerator GenerateTiles(Action finishedCallback, List<Vector3Int> destroyedTiles) { for (int x = 0; x < width; x++) { - for(int y = 0; y < height; y++) + for (int y = 0; y < height; y++) { float xPerlin = ((float)x / width) * scale + offsetX; float yPerlin = ((float)y / height) * scale + offsetY; @@ -33,16 +68,20 @@ if (perlinNoise >= 0.3f) { - if (!tilemap.HasTile(new Vector3Int(x,y+1))) { - tilemap.SetTile(new Vector3Int(x, y), ruleTile); - } - else + if (!destroyedTiles.Contains(new Vector3Int(x, y, 0))) { tilemap.SetTile(new Vector3Int(x, y), ruleTile); } } } + + // Update UI every 8 lines + if ((x % 8) == 0) + { + yield return null; + } } + finishedCallback(); } } -- Gitblit v1.9.3