| | |
| | | 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; |
| | |
| | | private void Awake() |
| | | { |
| | | tilemap = GetComponent<Tilemap>(); |
| | | #if DEBUG |
| | | //seed = 0123456789; |
| | | #endif |
| | | if (seed == null) |
| | | { |
| | | seed = GenerateSeed(9); |
| | | } |
| | | |
| | | SetSettingsFromSeed(seed.Value); |
| | | } |
| | | |
| | | public IEnumerator GenerateTiles(Action finishedCallback) |
| | | public void SetSettingsFromSeed(int seed) |
| | | { |
| | | UnityEngine.Random.State randomState = UnityEngine.Random.state; |
| | | |
| | | 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++) |
| | | { |
| | |
| | | |
| | | if (perlinNoise >= 0.3f) |
| | | { |
| | | tilemap.SetTile(new Vector3Int(x, y), ruleTile); |
| | | if (!destroyedTiles.Contains(new Vector3Int(x, y, 0))) |
| | | { |
| | | tilemap.SetTile(new Vector3Int(x, y), ruleTile); |
| | | } |
| | | } |
| | | |
| | | } |