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

简介: 【用unity实现100个游戏之16】Unity中程序化生成的2D地牢4(附项目源码)

最终效果

前言

本期紧跟着上期内容,主要实现在地牢中生成物品、放置玩家和敌人。

素材

物品素材:

https://itch.io/c/1597630/super-retro-world

按程序放置物品

新增ItemData,定义物品数据

// 放置类型枚举
public enum PlacementType
{
    OpenSpace, // 空地
    NearWall // 靠近墙壁
}
[CreateAssetMenu(menuName = "Dungeon/ItemData")]
public class ItemData : ScriptableObject {
    [Header("物品图片")]
    public Sprite sprite;
    [Header("物品大小")]
    public Vector2Int size;
    [Header("放置类型枚举")]
    public PlacementType placementType;
    [Header("是否添加偏移量,可以有效防止物品阻塞路径")]
    public bool addOffset;
}
[Serializable]
public class ItemDataInfo {
    public ItemData itemData;
    public int minQuantity;//最小数量
    public int maxQuantity;//最大数量
}

添加各种物品配置

新增Graph,实现一个简单的图数据结构,其中包含了获取邻居节点的功能

public class Graph
{
    // 四个方向邻居偏移量列表
    private static List<Vector2Int> neighbours4Directions = new List<Vector2Int>
    {
        new Vector2Int(0, 1), // 上
        new Vector2Int(1, 0), // 右
        new Vector2Int(0, -1), // 下
        new Vector2Int(-1, 0) // 左
    };
    // 八个方向邻居偏移量列表
    private static List<Vector2Int> neighbours8Directions = new List<Vector2Int>
    {
        new Vector2Int(0, 1), // 上
        new Vector2Int(1, 0), // 右
        new Vector2Int(0, -1), // 下
        new Vector2Int(-1, 0), // 左
        new Vector2Int(1, 1), // 右上
        new Vector2Int(1, -1), // 右下
        new Vector2Int(-1, 1), // 左上
        new Vector2Int(-1, -1) // 左下
    };
    private List<Vector2Int> graph; // 图的节点列表
    public Graph(IEnumerable<Vector2Int> vertices)
    {
        graph = new List<Vector2Int>(vertices);
    }
    // 获取起始位置的四个方向邻居节点
    public List<Vector2Int> GetNeighbours4Directions(Vector2Int startPosition)
    {
        return GetNeighbours(startPosition, neighbours4Directions);
    }
    // 获取起始位置的八个方向邻居节点
    public List<Vector2Int> GetNeighbours8Directions(Vector2Int startPosition)
    {
        return GetNeighbours(startPosition, neighbours8Directions);
    }
    // 获取指定位置的邻居节点
    private List<Vector2Int> GetNeighbours(Vector2Int startPosition, List<Vector2Int> neighboursOffsetList)
    {
        List<Vector2Int> neighbours = new List<Vector2Int>();
        foreach (var neighbourDirection in neighboursOffsetList)
        {
            Vector2Int potentialNeighbour = startPosition + neighbourDirection;
            if (graph.Contains(potentialNeighbour))
            {
                neighbours.Add(potentialNeighbour);
            }
        }
        return neighbours;
    }
}

新增ItemPlacementHelper,辅助物品放置的帮助类,根据房间地板信息和不包括走廊的房间地板信息进行初始化,提供了根据放置类型、最大迭代次数和物品区域大小获取物品放置位置的功能。

public class ItemPlacementHelper
{
    // 存储每种放置类型的所有可用位置
    Dictionary<PlacementType, HashSet<Vector2Int>> tileByType = new Dictionary<PlacementType, HashSet<Vector2Int>>();
    // 房间内不包含走廊的地板格子集合
    HashSet<Vector2Int> roomFloorNoCorridor;
    /// <summary>
    /// 用于辅助物品放置的帮助类,根据房间地板信息和不包括走廊的房间地板信息进行初始化。
    /// </summary>
    /// <param name="roomFloor">包括走廊在内的房间地板位置集合</param>
    /// <param name="roomFloorNoCorridor">不包括走廊的房间地板位置集合</param>
    public ItemPlacementHelper(HashSet<Vector2Int> roomFloor, HashSet<Vector2Int> roomFloorNoCorridor)
    {
        // 根据房间地板位置集合构建图
        Graph graph = new Graph(roomFloor);
        // 初始化不包括走廊的房间地板位置集合
        this.roomFloorNoCorridor = roomFloorNoCorridor;
        // 遍历每个房间地板位置
        foreach (var position in roomFloorNoCorridor)
        {
            // 获取当前位置的8个方向的邻居数量
            int neighboursCount8Dir = graph.GetNeighbours8Directions(position).Count;
            // 根据邻居数量判断放置类型
            PlacementType type = neighboursCount8Dir < 8 ? PlacementType.NearWall : PlacementType.OpenSpace;
            // 如果该放置类型不在字典中,则添加一个新的放置类型
            if (tileByType.ContainsKey(type) == false)
            {
                tileByType[type] = new HashSet<Vector2Int>();
            }
            // 对于靠墙的位置,如果有4个方向的邻居,则跳过该位置
            if (type == PlacementType.NearWall && graph.GetNeighbours4Directions(position).Count == 4)
            {
                continue;
            }
            // 将位置添加到对应放置类型的集合中
            tileByType[type].Add(position);
        }
    }
    /// <summary>
    /// 根据放置类型、最大迭代次数和物品区域大小获取物品放置位置。
    /// </summary>
    /// <param name="placementType">放置类型</param>
    /// <param name="iterationsMax">最大迭代次数</param>
    /// <param name="itemAreaSize">物品区域大小</param>
    /// <returns>物品放置位置的二维向量,如果找不到合适位置则返回null</returns>
    public Vector2? GetItemPlacementPosition(PlacementType placementType, int iterationsMax, Vector2Int itemAreaSize, bool addOffset)
    {
        int itemArea = itemAreaSize.x * itemAreaSize.y;
        // 如果指定放置类型的可用位置数量小于物品区域的大小,则无法放置,返回null
        if (tileByType[placementType].Count < itemArea)
        {
            return null;
        }
        int iteration = 0;
        while (iteration < iterationsMax)
        {
            iteration++;
                
            // 随机选择一个位置
            int index = UnityEngine.Random.Range(0, tileByType[placementType].Count);
            // if(tileByType[placementType] == null) return null; 
            var position = tileByType[placementType].ElementAtOrDefault(index);
            if (position == null)
            {
                continue; // 集合中没有指定索引的位置
            }
            // Vector2Int position = tileByType[placementType].ElementAt(index);
            // 如果物品区域大小大于1,则尝试放置较大的物品
            if (itemArea > 1)
            {
                var (result, placementPositions) = PlaceBigItem(position, itemAreaSize, addOffset);
                if (result == false)
                {
                    continue; // 放置失败,进行下一次迭代
                }
                // 从放置类型和邻近墙壁的位置集合中排除已放置的位置
                tileByType[placementType].ExceptWith(placementPositions);
                tileByType[PlacementType.NearWall].ExceptWith(placementPositions);
            }
            else
            {
                // 移除单个位置
                tileByType[placementType].Remove(position);
            }
            return position; // 返回成功放置的位置
        }
        return null; // 达到最大迭代次数仍未找到合适位置,返回null
    }
    /// <summary>
    /// 放置较大物品,返回放置是否成功以及放置的位置列表。
    /// </summary>
    /// <param name="originPosition">起始位置</param>
    /// <param name="size">物品尺寸</param>
    /// <param name="addOffset">是否添加偏移量</param>
    /// <returns>放置是否成功以及放置的位置列表</returns>
    private (bool, List<Vector2Int>) PlaceBigItem(Vector2Int originPosition, Vector2Int size, bool addOffset)
    {
        // 初始化放置的位置列表,并加入起始位置
        List<Vector2Int> positions = new List<Vector2Int>() { originPosition };
        // 计算边界值
        int maxX = addOffset ? size.x + 1 : size.x;
        int maxY = addOffset ? size.y + 1 : size.y;
        int minX = addOffset ? -1 : 0;
        int minY = addOffset ? -1 : 0;
        // 遍历每个位置
        for (int row = minX; row <= maxX; row++)
        {
            for (int col = minY; col <= maxY; col++)
            {
                // 跳过起始位置
                if (col == 0 && row == 0)
                {
                    continue;
                }
                // 计算新位置
                Vector2Int newPosToCheck = new Vector2Int(originPosition.x + row, originPosition.y + col);
                // 检查新位置是否可用
                if (roomFloorNoCorridor.Contains(newPosToCheck) == false)
                {
                    return (false, positions); // 放置失败,返回失败状态和已放置的位置列表
                }
                positions.Add(newPosToCheck); // 将新位置加入已放置的位置列表
            }
        }
        return (true, positions); // 放置成功,返回成功状态和已放置的位置列表
    }
}

新增PropPlacementManager,定义放置功能

public class PropPlacementManager : MonoBehaviour
{
    [SerializeField, Header("道具的预制体")]
    private GameObject propPrefab;
    [SerializeField]
    [Header("需要放置的道具列表")]
    private List<ItemDataInfo> itemDataInfos;
    private GameObject itemDataParent;//物品父类
    // 放置物品
    public void SetData(ItemPlacementHelper itemPlacementHelper)
    {
        ClearData();
        foreach (var itemDataInfo in itemDataInfos)
        {
            int count = UnityEngine.Random.Range(itemDataInfo.minQuantity, itemDataInfo.maxQuantity + 1);
            for (int i = 0; i < count; i++)
            {
                var position = itemPlacementHelper.GetItemPlacementPosition(itemDataInfo.itemData.placementType, 10, itemDataInfo.itemData.size, itemDataInfo.itemData.addOffset);
                if (position != null)
                {
                    SetIteamData((Vector3)position, itemDataInfo);
                }
            }
        }
    }
    //清空物品
    private void ClearData()
    {
        itemDataParent = GameObject.Find("ItemDataParent");
        //清空物品
        if (itemDataParent) DestroyImmediate(itemDataParent);
        itemDataParent = new GameObject("ItemDataParent");
    }
    //放置物品
    private void SetIteamData(Vector3 position, ItemDataInfo itemDataInfo)
    {
        // 实例化道具对象
        GameObject prop = Instantiate(propPrefab, position, Quaternion.identity);
        //绑定父级
        prop.transform.SetParent(itemDataParent.transform);
        //修改名称
        prop.name = itemDataInfo.itemData.name;
        SpriteRenderer propSpriteRenderer = prop.GetComponentInChildren<SpriteRenderer>();
        // 添加碰撞体
        CapsuleCollider2D collider = propSpriteRenderer.gameObject.AddComponent<CapsuleCollider2D>();
        collider.offset = Vector2.zero;
        // 根据道具大小设置碰撞体方向
        if (itemDataInfo.itemData.size.x > itemDataInfo.itemData.size.y)
        {
            collider.direction = CapsuleDirection2D.Horizontal;
        }
        // 根据道具大小设置碰撞体大小
        Vector2 size = new Vector2(itemDataInfo.itemData.size.x * 0.8f, itemDataInfo.itemData.size.y * 0.8f);
        collider.size = size;
        // 设置道具的精灵图片
        propSpriteRenderer.sprite = itemDataInfo.itemData.sprite;
        //调整精灵图片的位置
        propSpriteRenderer.transform.localPosition = new Vector2(1, 1) * 0.5f;
    }
}

修改CorridorFirstDungeonGenerator,调用放置物品功能

// 走廊优先生成方法
private void CorridorFirstGeneration()
{
    //。。。
    //放置物品
    ItemPlacementHelper itemPlacementHelper = new ItemPlacementHelper(floorPositions, roomPositions);
    PropPlacementManager propPlacementManager = FindObjectOfType<PropPlacementManager>();
    propPlacementManager.SetData(itemPlacementHelper);
}

添加道具预制体

挂载放置功能脚本并配置参数

效果

放置玩家和敌人

修改PropPlacementManager

[SerializeField, Header("敌人和玩家预制体")]
private GameObject enemyPrefab, playerPrefab;
[SerializeField, Header("虚拟相机")]
private CinemachineVirtualCamera vCamera;
//放置主角
public void SetPlayer(ItemPlacementHelper itemPlacementHelper)
{
    ClearPlayer();
    var position = itemPlacementHelper.GetItemPlacementPosition(PlacementType.OpenSpace, 10, new Vector2Int(1, 1), false);
    if (position == null) return;
    GameObject player = Instantiate(playerPrefab, (Vector3)position, Quaternion.identity);
    player.transform.localPosition = new Vector2(1, 1) * 0.5f;
    //使相机跟随玩家
    vCamera.Follow = player.transform;
    vCamera.LookAt = player.transform;
}
//清空主角
private void ClearPlayer()
{
    GameObject player = GameObject.FindGameObjectWithTag("Player");
    if (player) DestroyImmediate(player);
}
//放置敌人
public void SetEnemy(ItemPlacementHelper itemPlacementHelper)
{
    ClearEnemy();
    
    //测试放置10个敌人
    for (int i = 0; i < 10; i++)
    {
        var position = itemPlacementHelper.GetItemPlacementPosition(PlacementType.OpenSpace, 10, new Vector2Int(1, 1), false);
        if (position == null) return;
        GameObject enemy= Instantiate(enemyPrefab, (Vector3)position, Quaternion.identity);
        enemy.transform.localPosition = new Vector2(1, 1) * 0.5f;
    }
}
//清空敌人
private void ClearEnemy()
{
    GameObject[] enemys = GameObject.FindGameObjectsWithTag("Enemy");
    foreach (GameObject enemy in enemys)
    {
        DestroyImmediate(enemy);
    }
}

修改CorridorFirstDungeonGenerator,调用

private void CorridorFirstGeneration()
{
  //。。。
  //放置主角
    propPlacementManager.SetPlayer(itemPlacementHelper);
    //放置敌人
    propPlacementManager.SetEnemy(itemPlacementHelper);
}

配置主角和敌人预制体,记得配置对应的标签

添加虚拟相机,并配置参数

效果

控制主角移动

新增代码,简单实现人物移动

public class Player : MonoBehaviour
{
    [Header("移动速度")]
    public float speed;
    Vector3 movement;
    void Update()
    {
        //移动
        movement = new Vector3(Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed, Input.GetAxisRaw("Vertical") * Time.deltaTime * speed, transform.position.z);
        transform.Translate(movement);
        //翻面
        if (movement.x > 0)
        {
            transform.GetChild(0).localScale = new Vector3(1, 1, 1);
        }
        if (movement.x < 0)
        {
            transform.GetChild(0).localScale = new Vector3(-1, 1, 1);
        }
    }
}

效果

参考

【视频】https://www.youtube.com/watch?app=desktop&v=-QOCX6SVFsk&list=PLcRSafycjWFenI87z7uZHFv6cUG2Tzu9v&index=1

源码

源码整理好我会放上来

目录
相关文章
|
3天前
|
存储 JSON 关系型数据库
【unity实战】制作unity数据保存和加载系统——大型游戏存储的最优解
【unity实战】制作unity数据保存和加载系统——大型游戏存储的最优解
12 2
|
3天前
|
图形学
【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)(上)
【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)
10 2
|
3天前
|
存储 JSON 图形学
【unity实战】制作unity数据保存和加载系统——小型游戏存储的最优解
【unity实战】制作unity数据保存和加载系统——小型游戏存储的最优解
6 0
|
3天前
|
图形学
【推荐100个unity插件之19】武器拖尾特效插件——Pocket RPG Weapon Trails(2d 3d通用)
【推荐100个unity插件之19】武器拖尾特效插件——Pocket RPG Weapon Trails(2d 3d通用)
7 0
|
3天前
|
图形学
【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)(下)
【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)(下)
8 0
|
3天前
|
图形学
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版3(附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版3(附带项目源码)
14 2
|
3天前
|
图形学
【制作100个unity游戏之28】花半天时间用unity复刻童年4399经典小游戏《黄金矿工》(附带项目源码)
【制作100个unity游戏之28】花半天时间用unity复刻童年4399经典小游戏《黄金矿工》(附带项目源码)
13 0
|
3天前
|
图形学
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版2(附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版2(附带项目源码)
8 1
|
3天前
|
存储 JSON 关系型数据库
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版13(完结,附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版13(完结,附带项目源码)
9 0
|
3天前
|
图形学
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版12(附带项目源码)
【制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版12(附带项目源码)
9 0