unity2d随机地图

本文详细展示了如何在Unity项目中使用C#编写代码生成随机地图,涉及地图生成逻辑、滑动概率设置和基础地图元素的应用。
摘要由CSDN通过智能技术生成

这是我之前写一个小游戏demo写的随机地图生成

初始值

[Tooltip("地图生成位置")]
    public Tilemap tilemap;

    int[,] map;

    [Tooltip("显示滑条")]
    [Range(0, 100)]
    public int probability;

    [Tooltip("地图宽")]
    public int width;

    [Tooltip("地图高")]
    public int height;

    [Tooltip("种子")]
    public string seed;

    [Tooltip("是否使用随机种子")]
    public bool useRandomSeed;


    [Tooltip("保存草地的素材")]
    public TileBase grasslandTile;

    [Tooltip("保存旱地的素材")]
    public TileBase grassUnfoldTile;

    [Tooltip("空气墙的素材")]
    public TileBase wallTile;

    [Tooltip("噪点")]
    public int NoiseRemoval = 4;

代码使用了的是噪声算法

关键代码

/// <summary>
    /// 画出地图
    /// </summary>
    void DrawMap()
    {
        if (map != null)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Vector3Int NowPoint = new Vector3Int(i, j, 0);
                    if (map[i, j] == 0)
                    {
                        tilemap.SetTile(NowPoint, grasslandTile);
                    }
                    else if (map[i,j]==1)
                    {
                        tilemap.SetTile(NowPoint, grassUnfoldTile);
                    }

                    //画墙
                    if ((i == width - 1 || i == width + 1) || (j == height - 1 || j == height + 1)
                        || (i == 0 || i == width - 1) || (j == 0 || j == height - 1))
                    {
                        tilemap.SetTile(NowPoint, wallTile);
                    }
                }
            }
            
        }
    }

    /// <summary>
    /// 生成地图(是否随机种子,随机种子则获取当前时间进行随机)
    /// </summary>
    void RandomillMap()
    {
        if (useRandomSeed)
            seed = DateTime.Now.ToString();

        System.Random pasudoRandom = new System.Random(seed.GetHashCode());

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (i == 0 || i == width - 1 || j == 0 || j == height - 1)
                    map[i, j] = 1;
                else
                    map[i, j] = (pasudoRandom.Next(0, 100) < probability) ? 1 : 0;
            }
        }
    }

    /// <summary>
    /// 地图规则,数量大于4为障碍,否则为道路
    /// </summary>
    void SmoothMap()
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                int surroundingTiles = GetSurroundingWalls(i, j);

                if (surroundingTiles > 4)
                    map[i, j] = 1;
                else if (surroundingTiles < 4)
                    map[i, j] = 0;
            }
        }
    }

    /// <summary>
    /// 生成墙
    /// </summary>
    /// <param name="posX"></param>
    /// <param name="posY"></param>
    /// <returns></returns>
    int GetSurroundingWalls(int posX, int posY)
    {
        int wallCount = 0;

        for (int i = posX - 1; i <= posX + 1; i++)
        {
            for (int j = posY - 1; j <= posY + 1; j++)
            {
                if (i >= 0 && i < width && j >= 0 && j < height)
                {
                    if (i != posX || j != posY)
                        wallCount += map[i, j];
                }
                else
                {
                    wallCount++;
                }
            }
        }
        return wallCount;
    }

没记错的话这个我记得是借鉴了一个博主的,但是找不到了,再此做分享与记录

完整代码:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;

public class GroundTileMapYawp : MonoBehaviour
{
    [Tooltip("地图生成位置")]
    public Tilemap tilemap;

    int[,] map;

    [Tooltip("显示滑条")]
    [Range(0, 100)]
    public int probability;

    [Tooltip("地图宽")]
    public int width;

    [Tooltip("地图高")]
    public int height;

    [Tooltip("种子")]
    public string seed;

    [Tooltip("是否使用随机种子")]
    public bool useRandomSeed;


    [Tooltip("保存草地的素材")]
    public TileBase grasslandTile;

    [Tooltip("保存旱地的素材")]
    public TileBase grassUnfoldTile;

    [Tooltip("空气墙的素材")]
    public TileBase wallTile;

    //[Tooltip("保存边界的素材")]
    //public TileBase Boundary;

    [Tooltip("噪点")]
    public int NoiseRemoval = 4;

    //private static GroundTileMapYawp _instance;

    //public static GroundTileMapYawp Instance
    //{
    //    get
    //    {
    //        if (_instance == null)
    //        {
    //            _instance = new GroundTileMapYawp();
    //        }
    //        return _instance;
    //    }
    //}

    public void BuildGroundMap()
    {
        BuilldMap();
    }

    /// <summary>
    /// 地图制作
    /// </summary>
    void BuilldMap()
    {
        //清空地图格子
        tilemap.ClearAllTiles();
        //创建地图大小
        map = new int[width, height];
        RandomillMap();
        for(int i = 0; i < NoiseRemoval;i++)
        {
            SmoothMap();
        }
        DrawMap();
        //Wall();
    }
    
    void Wall()
    {

    }


    /// <summary>
    /// 画出地图
    /// </summary>
    void DrawMap()
    {
        if (map != null)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Vector3Int NowPoint = new Vector3Int(i, j, 0);
                    if (map[i, j] == 0)
                    {
                        tilemap.SetTile(NowPoint, grasslandTile);
                    }
                    else if (map[i,j]==1)
                    {
                        tilemap.SetTile(NowPoint, grassUnfoldTile);
                    }

                    //画墙
                    if ((i == width - 1 || i == width + 1) || (j == height - 1 || j == height + 1)
                        || (i == 0 || i == width - 1) || (j == 0 || j == height - 1))
                    {
                        tilemap.SetTile(NowPoint, wallTile);
                    }
                }
            }
            
        }
    }

    /// <summary>
    /// 生成地图(是否随机种子,随机种子则获取当前时间进行随机)
    /// </summary>
    void RandomillMap()
    {
        if (useRandomSeed)
            seed = DateTime.Now.ToString();

        System.Random pasudoRandom = new System.Random(seed.GetHashCode());

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (i == 0 || i == width - 1 || j == 0 || j == height - 1)
                    map[i, j] = 1;
                else
                    map[i, j] = (pasudoRandom.Next(0, 100) < probability) ? 1 : 0;
            }
        }
    }

    /// <summary>
    /// 地图规则,数量大于4为障碍,否则为道路
    /// </summary>
    void SmoothMap()
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                int surroundingTiles = GetSurroundingWalls(i, j);

                if (surroundingTiles > 4)
                    map[i, j] = 1;
                else if (surroundingTiles < 4)
                    map[i, j] = 0;
            }
        }
    }

    /// <summary>
    /// 生成墙
    /// </summary>
    /// <param name="posX"></param>
    /// <param name="posY"></param>
    /// <returns></returns>
    int GetSurroundingWalls(int posX, int posY)
    {
        int wallCount = 0;

        for (int i = posX - 1; i <= posX + 1; i++)
        {
            for (int j = posY - 1; j <= posY + 1; j++)
            {
                if (i >= 0 && i < width && j >= 0 && j < height)
                {
                    if (i != posX || j != posY)
                        wallCount += map[i, j];
                }
                else
                {
                    wallCount++;
                }
            }
        }
        return wallCount;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值