• 【用unity实现100个游戏之16】Unity程序化生成随机2D地牢游戏2(附项目源码)


    先看看最终效果

    在这里插入图片描述

    前言

    上期已经实现了房间的生成,本期紧跟着上期内容,生成走廊并结合上期内容生成连通的房间。

    生成走廊

    修改ProceduralGenerationAlgorithms

    /// 
    /// 随机生成走廊的方法。
    /// 
    /// 起始位置
    /// 走廊长度
    /// 走廊位置列表
    public static List<Vector2Int> RandomWalkCorridor(Vector2Int startPosition, int corridorLength)
    {
        // 创建走廊位置列表
        List<Vector2Int> corridor = new List<Vector2Int>();
    
        // 随机选择一个基本方向
        var direction = Direction2D.GetRandomCardinalDirection();
    
        // 将当前位置设置为起始位置
        var currentPosition = startPosition;
    
        // 将起始位置添加到走廊位置列表中
        corridor.Add(currentPosition);
    
        // 沿着基本方向移动,并将每个新位置添加到走廊位置列表中
        for (int i = 0; i < corridorLength; i++)
        {
            currentPosition += direction;
            corridor.Add(currentPosition);
        }
    
        // 返回走廊位置列表
        return corridor;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    修改SimpleRandomWalkDungeonGenerator

    HashSet<Vector2Int> floorPositions = RunRandomWalk(randomWalkParameters); // 获取地牢地板坐标集合
    
    protected HashSet<Vector2Int> RunRandomWalk(SimpleRandomWalkSO parameters)
    {
       //。。。
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    新增CorridorFirstDungeonGenerator,走廊优先地牢生成器

    // 走廊优先地牢生成器,继承自简单随机行走地牢生成器
    public class CorridorFirstDungeonGenerator : SimpleRandomWalkDungeonGenerator
    {
        [SerializeField, Header("走廊长度")] private int corridorLength = 14;
        [SerializeField, Header("走廊数量")] private int corridorCount = 5;
        [SerializeField, Header("房间占比")] [Range(0.1f, 1)] private float roomPercent = 0.8f;
    
        // 执行过程化生成
        protected override void RunProceduralGeneration()
        {
            CorridorFirstGeneration();
        }
    
        // 走廊优先生成方法
        private void CorridorFirstGeneration()
        {
            HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();  // 地板位置集合
            CreateCorridors(floorPositions);  // 创建走廊
            tilemapVisualizer.PaintFloorTiles(floorPositions);  // 绘制地板瓦片
            WallGenerator.CreateWalls(floorPositions, tilemapVisualizer);  // 创建墙壁
        }
    
        // 创建走廊的方法
        private void CreateCorridors(HashSet<Vector2Int> floorPositions)
        {
            var currentPosition = startPosition;  // 当前位置设为起始位置
            for (int i = 0; i < corridorCount; i++)  // 循环生成指定数量的走廊
            {
                var corridor = ProceduralGenerationAlgorithms.RandomWalkCorridor(currentPosition, corridorLength);  // 生成随机走廊
                currentPosition = corridor[corridor.Count - 1];  // 更新当前位置为走廊的最后一个位置
                floorPositions.UnionWith(corridor);  // 将走廊位置添加到地板位置集合中
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    配置参数,
    在这里插入图片描述

    效果,创建了走廊
    在这里插入图片描述

    生成房间

    所以后续只需要在走廊的末端生成房间即可连通各个房间,添加走廊长度以防止房间之间相交

    修改CorridorFirstDungeonGenerator

    // 走廊优先生成方法
    private void CorridorFirstGeneration()
    {
        HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();  // 地板位置集合
    
        HashSet<Vector2Int> potentialRoomPositions = new HashSet<Vector2Int>();// 房间位置集合
        CreateCorridors(floorPositions, potentialRoomPositions);// 创建走廊
    
        HashSet<Vector2Int> roomPositions = CreateRooms(potentialRoomPositions);// 创建房间
        floorPositions.UnionWith(roomPositions);// 将房间位置添加到地板位置集合中
    
        tilemapVisualizer.PaintFloorTiles(floorPositions);  // 绘制地板瓦片
        WallGenerator.CreateWalls(floorPositions, tilemapVisualizer);  // 创建墙壁
    }
    
    // 创建走廊的方法
    private void CreateCorridors(HashSet<Vector2Int> floorPositions, HashSet<Vector2Int> potentialRoomPositions)
    {
        var currentPosition = startPosition;  // 当前位置设为起始位置
    
        potentialRoomPositions.Add(currentPosition);// 将当前位置添加到潜在房间位置集合中
    
        for (int i = 0; i < corridorCount; i++)  // 循环生成指定数量的走廊
        {
            var corridor = ProceduralGenerationAlgorithms.RandomWalkCorridor(currentPosition, corridorLength);  // 生成随机走廊
            currentPosition = corridor[corridor.Count - 1];  // 更新当前位置为走廊的最后一个位置
    
            potentialRoomPositions.Add(currentPosition);// 将当前位置添加到潜在房间位置集合中
    
            floorPositions.UnionWith(corridor);  // 将走廊位置添加到地板位置集合中
        }
    }
    
    // 创建房间的方法
    private HashSet<Vector2Int> CreateRooms(HashSet<Vector2Int> potentialRoomPositions)
    {
        HashSet<Vector2Int> roomPositions = new HashSet<Vector2Int>();
        int roomToCreateCount = Mathf.RoundToInt(potentialRoomPositions.Count * roomPercent);// 计算需要创建的房间数量
        // 根据潜在房间位置集合随机选择要创建的房间位置
        List<Vector2Int> roomsToCreate = potentialRoomPositions.OrderBy(x => Guid.NewGuid()).Take(roomToCreateCount).ToList();
        foreach (var roomPosition in roomsToCreate)
        {
            var roomFloor = RunRandomWalk(randomWalkParameters, roomPosition);// 在选定的位置运行随机行走算法以生成房间地板
            roomPositions.UnionWith(roomFloor);// 将房间地板位置添加到房间位置集合中
        }
        return roomPositions;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    修改SimpleRandomWalkDungeonGenerator

    // 获取地牢地板坐标集合
    HashSet<Vector2Int> floorPositions = RunRandomWalk(randomWalkParameters, startPosition);
    
    protected HashSet<Vector2Int> RunRandomWalk(SimpleRandomWalkSO parameters, Vector2Int position)
    {
        var currentPosition = position; // 当前位置初始化为起始位置
        //。。。
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    配置参数,增加走廊长度
    在这里插入图片描述
    效果
    在这里插入图片描述

    修复死胡同

    前面生成房间存在一些死胡同,这非常不好,我么需要修复一下

    修改CorridorFirstDungeonGenerator

    // 走廊优先生成方法
    private void CorridorFirstGeneration()
    {
        HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();  // 地板位置集合
    
        HashSet<Vector2Int> potentialRoomPositions = new HashSet<Vector2Int>();// 房间位置集合
        CreateCorridors(floorPositions, potentialRoomPositions);// 创建走廊
    
        HashSet<Vector2Int> roomPositions = CreateRooms(potentialRoomPositions);// 创建房间
    
        List<Vector2Int> dradEnds = FindAllDeadEnds(floorPositions);
        CreateRoomsAtDeadEnd(dradEnds, roomPositions);
    
        floorPositions.UnionWith(roomPositions);// 将房间位置添加到地板位置集合中
    
        tilemapVisualizer.PaintFloorTiles(floorPositions);  // 绘制地板瓦片
        WallGenerator.CreateWalls(floorPositions, tilemapVisualizer);  // 创建墙壁
    }
    
    // 在死胡同处创建房间的方法
    private void CreateRoomsAtDeadEnd(List<Vector2Int> deadEnds, HashSet<Vector2Int> roomFloors)
    {
        foreach (var position in deadEnds)
        {
            if (roomFloors.Contains(position) == false)
            {
                var room = RunRandomWalk(randomWalkParameters, position);  // 在选定的位置运行随机行走算法以生成房间地板
                roomFloors.UnionWith(room);  // 将房间地板位置添加到房间位置集合中
            }
        }
    }
    
    // 查找所有死胡同的方法
    private List<Vector2Int> FindAllDeadEnds(HashSet<Vector2Int> floorPositions)
    {
        // 创建一个空的死路位置列表
        List<Vector2Int> deadEnds = new List<Vector2Int>();
    
        // 对于每个位置,检查其周围的位置数量
        foreach (var position in floorPositions)
        {
            int neighboursCount = 0;
            // 遍历四个基本方向(上下左右),如果相邻位置在floorPositions中,则增加邻居计数
            foreach (var direction in Direction2D.cardinalDirectionsList)
            {
                if (floorPositions.Contains(position + direction))
                {
                    neighboursCount++;
                }
            }
            // 如果邻居计数为1,则将该位置添加到死路列表中
            if (neighboursCount == 1)
            {
                deadEnds.Add(position);
            }
        }
        // 返回所有死路的位置列表
        return deadEnds;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    效果,我们可以把走廊长度扩大,这样就实现了生成不同房间的功能
    在这里插入图片描述
    效果
    在这里插入图片描述

    增加走廊宽度

    获取走廊位置信息集合

    修改CorridorFirstDungeonGenerator

    List<List<Vector2Int>> corridors = CreateCorridors(floorPositions, potentialRoomPositions);
    
    private List<List<Vector2Int>> CreateCorridors(HashSet<Vector2Int> floorPositions, HashSet<Vector2Int> potentialRoomPositions)
    {
        var currentPosition = startPosition;  // 当前位置设为起始位置
    
        potentialRoomPositions.Add(currentPosition);// 将当前位置添加到潜在房间位置集合中
    
        List<List<Vector2Int>> corridors = new List<List<Vector2Int>>(); // 声明并初始化走廊列表
    
        for (int i = 0; i < corridorCount; i++)  // 循环生成指定数量的走廊
        {
            var corridor = ProceduralGenerationAlgorithms.RandomWalkCorridor(currentPosition, corridorLength);  // 生成随机走廊
            currentPosition = corridor[corridor.Count - 1];  // 更新当前位置为走廊的最后一个位置
    
            potentialRoomPositions.Add(currentPosition);// 将当前位置添加到潜在房间位置集合中
    
            floorPositions.UnionWith(corridor);  // 将走廊位置添加到地板位置集合中
            corridors.Add(corridor);
        }
        return corridors;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    下面IncreaseCorridorSizeByOne() 和 IncreaseCorridorBrush3by3()两种办法都可以增加走廊宽度走廊,但它们的实现方式不同。
    IncreaseCorridorSizeByOne()方法比较简单粗暴,适合用于简单的走廊扩展
    而 IncreaseCorridorBrush3by3()方法则更加考虑周围环境,生成的走廊形状更加自然
    但是,由于 IncreaseCorridorBrush3by3()方法的实现比较复杂,可能会增加代码的复杂度和运行时间,因此需要权衡使用场景。

    方法一

    修改CorridorFirstDungeonGenerator

    // 走廊优先生成方法
    private void CorridorFirstGeneration()
    {
    	//...
    	
    	// 对每条走廊进行遍历,增加走廊的大小并更新地板位置集合
    	for (int i = 0; i < corridors.Count; i++)
    	{
    	    // 增加走廊大小的方法 生成3x3走廊
    	    corridors[i] = IncreaseCorridorSizeByOne(corridors[i]);//方法1
    	    
    	    // 将更新后的走廊位置添加到地板位置集合中
    	    floorPositions.UnionWith(corridors[i]);
    	}
    }
    
    public List<Vector2Int> IncreaseCorridorSizeByOne(List<Vector2Int> corridor)
    {
        List<Vector2Int> newCorridor = new List<Vector2Int>(); // 新走廊坐标的列表
        Vector2Int previousDirection = Vector2Int.zero; // 上一个方向的单位向量
    
        for (int i = 1; i < corridor.Count; i++) // 遍历走廊坐标列表
        {
            Vector2Int directionFromCell = corridor[i] - corridor[i - 1]; // 获取当前坐标与上一个坐标之间的方向向量
    
            if (previousDirection != Vector2Int.zero && directionFromCell != previousDirection)
            {
                // 处理转角情况
                for (int x = -1; x < 2; x++)
                {
                    for (int y = -1; y < 2; y++)
                    {
                        newCorridor.Add(corridor[i - 1] + new Vector2Int(x, y)); // 将转角坐标添加到新走廊坐标列表中
                    }
                }
                previousDirection = directionFromCell; // 更新上一个方向的单位向量
            }
            else
            {
                Vector2Int newCorridorTileOffset = GetDirection90From(directionFromCell); // 获取当前方向的90度偏移向量
                newCorridor.Add(corridor[i - 1]); // 添加当前坐标到新走廊坐标列表中
                newCorridor.Add(corridor[i - 1] + newCorridorTileOffset); // 添加当前坐标及偏移向量到新走廊坐标列表中
            }
        }
        return newCorridor; // 返回新走廊坐标的列表
    }
    
    private Vector2Int GetDirection90From(Vector2Int direction)
    {
        if (direction == Vector2Int.up)
        {
            return Vector2Int.right; // 如果输入方向向量是向上的,返回向右的方向向量
        }
        if (direction == Vector2Int.right)
        {
            return Vector2Int.down; // 如果输入方向向量是向右的,返回向下的方向向量
        }
        if (direction == Vector2Int.down)
        {
            return Vector2Int.left; // 如果输入方向向量是向下的,返回向左的方向向量
        }
        if (direction == Vector2Int.left)
        {
            return Vector2Int.up; // 如果输入方向向量是向左的,返回向上的方向向量
        }
    
        return Vector2Int.zero; // 如果输入方向向量不是上述情况之一,则返回零向量
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    生成效果,可以看到对于复杂走廊增加宽度的效果不是很好,有些走廊只有两格隔宽度
    在这里插入图片描述

    方法二

    修改CorridorFirstDungeonGenerator

    // 走廊优先生成方法
    private void CorridorFirstGeneration()
    {
    	//...
    	
    	// 对每条走廊进行遍历,增加走廊的大小并更新地板位置集合
    	for (int i = 0; i < corridors.Count; i++)
    	{
    	    // 增加走廊大小的方法 生成3x3走廊
    	    // corridors[i] = IncreaseCorridorSizeByOne(corridors[i]);//方法1
    	    corridors[i] = IncreaseCorridorBrush3by3(corridors[i]);//方法2
    	    // 将更新后的走廊位置添加到地板位置集合中
    	    floorPositions.UnionWith(corridors[i]);
    	}
    }
    
    public List<Vector2Int> IncreaseCorridorBrush3by3(List<Vector2Int> corridor)
    {
        List<Vector2Int> newCorridor = new List<Vector2Int>(); // 新走廊坐标的列表
    
        for (int i = 1; i < corridor.Count; i++) // 遍历走廊坐标列表
        {
            for (int x = -1; x < 2; x++) // 在x轴方向上遍历-1到1的范围
            {
                for (int y = -1; y < 2; y++) // 在y轴方向上遍历-1到1的范围
                {
                    newCorridor.Add(corridor[i - 1] + new Vector2Int(x, y)); // 将当前坐标的周围九个坐标添加到新走廊坐标列表中
                }
            }
        }
    
        return newCorridor; // 返回新走廊坐标的列表
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    生成效果,稳定生成3格宽度的走廊
    在这里插入图片描述

    源码

    源码会放在本项目最后一篇

    完结

    赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

    好了,我是向宇https://xiangyu.blog.csdn.net

    一位在小公司默默奋斗的开发者,出于兴趣爱好,于是最近才开始自习unity。如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我可能也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~
    在这里插入图片描述

  • 相关阅读:
    连新手小白都知道的电子画册一键生成器,你还不知道吗?
    Wifi列表扫描和Wifi链接
    hive orc文件读取出错
    基于Springboot实现毕业生信息招聘平台管理系统演示【项目源码+论文说明】分享
    公允价值会计(fair-value accounting)
    java基础之继承[20]
    module “QtQuick.VirtualKeyboard.Plugins“ is not installed
    盘点JDK中基于CAS实现的原子类
    简记C语言清空输入残留内容
    2024年Nano编辑器最新使用教程
  • 原文地址:https://blog.csdn.net/qq_36303853/article/details/134536369