unity 使用深度优先搜索生成迷宫之二

unity 使用深度优先搜索生成迷宫之二

之前写过一篇使用深度优先搜索生成随机迷宫的文章

https://www.cnblogs.com/JinT-Hwang/p/9599913.html

今天做了一下优化,使用unity的TileMap来做,并且代码减少到100行以内。

先看一下效果图

DFS

下面直接是代码,至于在unity中怎么创建tilemap资源这里就不讲了:

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Tilemaps; 5 6public class TileMapTestBehaviour : MonoBehaviour 7{ 8 public TileBase baseTile; 9 public Tilemap tilemap; 10 11 public int mapWidth; 12 public int mapHeight; 13 14 public float tileSize = 0.16f; 15 16 private Stack<Vector3Int> tileMapPosStack; 17 private List<Vector3Int> tileSaveList; 18 private Queue<Vector3Int> recordQueue; 19 20 private static readonly List<Vector3Int> tilesOffset = new List<Vector3Int>() 21 { 22 Vector3Int.down,Vector3Int.right,Vector3Int.up,Vector3Int.left 23 }; 24 25 // Use this for initialization 26 void Start() 27 { 28 tileMapPosStack = new Stack<Vector3Int>(); 29 tileSaveList = new List<Vector3Int>(); 30 recordQueue = new Queue<Vector3Int>(); 31 32 tileMapPosStack.Push(Vector3Int.zero); 33 tileSaveList.Add(Vector3Int.zero); 34 35 CreateMap_DFS(); 36 } 37 38 private void CreateMap_DFS() 39 { 40 Vector3Int currentTile; 41 Vector3Int nextTile; 42 43 List<Vector3Int> aroundTileList = new List<Vector3Int>(); 44 45 while (tileMapPosStack.Count > 0) 46 { 47 currentTile = tileMapPosStack.Pop(); 48 49 for (int i = 0; i < 4; i++) 50 { 51 nextTile = currentTile + tilesOffset[i]; 52 53 if (!tileSaveList.Contains(nextTile)) 54 { 55 aroundTileList.Add(nextTile); 56 } 57 } 58 59 if (aroundTileList.Count >= 3) 60 { 61 while (aroundTileList.Count > 0) 62 { 63 Vector3Int tilePos = aroundTileList[Random.Range(0, aroundTileList.Count)]; 64 aroundTileList.Remove(tilePos); 65 66 if (IsTileInRange(tilePos)) 67 { 68 tileMapPosStack.Push(tilePos); 69 } 70 } 71 72 if (!tileSaveList.Contains(currentTile)) 73 tileSaveList.Add(currentTile); 74 75 recordQueue.Enqueue(currentTile); 76 } 77 78 aroundTileList.Clear(); 79 } 80 81 StartCoroutine("Display"); 82 } 83 84 private bool IsTileInRange(Vector3Int tilePos) 85 { 86 return tilePos.x >= 0 && tilePos.x < mapWidth && tilePos.y >= 0 && tilePos.y < mapHeight; 87 } 88 89 private IEnumerator Display() 90 { 91 while (recordQueue.Count > 0) 92 { 93 yield return new WaitForSecondsRealtime(0.1f); 94 95 tilemap.SetTile(recordQueue.Dequeue(), baseTile); 96 } 97 } 98}

欢迎交流,转载注明出处。:)

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap