程序生成用于增加游戏的可变性。
著名的项目包括
Minecraft ,
Enter the Gungeon和
Descenders 。 在本文中,我将解释与
Tilemap系统一起使用时可以使用的一些算法,该算法在Unity 2017.2和
RuleTile中以2D函数的形式出现。
随着地图的程序创建,每个传递的游戏将是唯一的。 您甚至可以在组装游戏后,使用各种输入数据(例如,时间或玩家的当前级别)来动态更改内容。
这个帖子是关于什么的?
我们将介绍一些创建过程世界的最常见方法,以及我创建的几种变体。 这是阅读本文后可以创建的示例。 三种算法共同使用
Tilemap和
RuleTile创建地图:
在使用任何算法生成地图的过程中,我们都会获得一个包含所有新数据的
int
数组。 您可以继续修改此数据或将其渲染到图块地图。
在进一步阅读之前,很高兴了解以下内容:
- 我们区分什么是图块,哪些不使用二进制值。 1是图块,0是不存在。
- 我们将所有卡存储在每个函数末尾返回给用户的二维整数数组中(执行渲染的函数除外)。
- 我将使用GetUpperBound()数组函数获取每个地图的高度和宽度,以便该函数接收较少的变量,并且代码更简洁 。
- 我经常使用Mathf.FloorToInt() ,因为Tilemap坐标系从左下角开始,而Mathf.FloorToInt()允许您将数字四舍五入为整数。
- 这篇文章中的所有代码都是用C#编写的。
数组生成
GenerateArray创建给定大小的新
int
数组。 我们还可以指示该数组是应填充还是为空(1或0)。 这是代码:
public static int[,] GenerateArray(int width, int height, bool empty) { int[,] map = new int[width, height]; for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { if (empty) { map[x, y] = 0; } else { map[x, y] = 1; } } } return map; }
地图渲染
此功能用于在平铺地图上渲染地图。 我们围绕地图的宽度和高度循环,仅在测试点处的数组的值为1时才放置图块。
public static void RenderMap(int[,] map, Tilemap tilemap, TileBase tile) {
地图更新
此功能仅用于更新地图,而不用于重新渲染。 因此,我们可以使用更少的资源,而无需重新绘制每个图块及其图块数据。
public static void UpdateMap(int[,] map, Tilemap tilemap)
噪音珀林
佩林噪声可用于各种目的。 首先,我们可以使用它来创建地图的顶层。 为此,只需使用当前位置x和种子获得一个新点。
简单的解决方案
这种生成方法使用了电平生成中最简单的实现Perlin噪声的形式。 我们可以将Unity函数用于消除Perlin噪声,因此我们不必自己编写代码。 使用
Mathf.FloorToInt()函数,我们还将仅使用整数作为图块映射。
public static int[,] PerlinNoise(int[,] map, float seed) { int newPoint;
呈现为图块地图后的外观如下:
平滑处理
您也可以使用此功能并使其平滑。 设置固定Perlin高度的时间间隔,然后在这些点之间执行平滑处理。 事实证明,此函数会稍微复杂一点,因为对于间隔,您需要考虑整数值列表。
public static int[,] PerlinNoiseSmooth(int[,] map, float seed, int interval) {
在此功能的第一部分中,我们首先检查间隔是否大于一个。 如果是这样,则产生噪音。 生成间隔执行,以便可以应用平滑。 函数的下一部分是平滑点。
平滑执行如下:
- 我们得到当前和最后的位置
- 我们得到两点之间的差,我们需要的最重要的信息是沿y轴的差
- 然后,我们确定需要多少变化才能达到目标,这是通过将y的差除以区间变量来完成的。
- 接下来,我们开始设置位置,一直到零
- 当我们在y轴上达到0时,将高度变化添加到当前高度,然后对下一个x位置重复该过程
- 在最后位置和当前位置之间的每个位置结束时,我们移至下一个点
如果间隔小于一个,那么我们仅使用前一个函数,它将为我们完成所有工作。
else {
让我们看一下渲染:
随机漫步
随机步行上衣
该算法执行硬币翻转。 我们可以得到两个结果之一。 如果结果是“ eagle”,那么我们将块向上移动;如果结果是“ tails”,那么我们将块向下移动。 通过不断向上或向下移动来创建高度。 这种算法的唯一缺点是其非常明显的块状性。 让我们看看它是如何工作的。
public static int[,] RandomWalkTop(int[,] map, float seed) {
带有抗锯齿的随机步行上衣与Perlin噪声生成相比,这种生成使我们的高度更平滑。
与以前的版本相比,Random Walk的这种变化提供了更加平滑的结果。 我们可以通过在函数中再添加两个变量来实现它:
- 第一个变量用于确定维持当前高度需要多长时间。 它是整数,并在高度变化时重置
- 第二个变量输入到函数中,并用作高度的最小截面宽度。 当我们看功能时,它将变得更加清晰。
现在我们知道要添加什么。 让我们看一下函数:
public static int[,] RandomWalkTopSmoothed(int[,] map, float seed, int minSectionWidth) {
如下图所示,通过平滑随机游走算法,您可以在该水平上获得漂亮的平坦段。
结论
我希望本文能激发您在项目中使用过程生成。 如果您想了解有关过程生成地图的更多信息,请浏览
过程生成Wiki或
Roguebasin.com的优秀资源。
在本文的第二部分,我们将使用过程生成来创建洞穴系统。
第二部分
我们在本部分中讨论的所有内容都可以在
该项目中找到。 您可以下载资产并尝试自己的过程算法。
噪音珀林
在上一部分中,我们研究了应用
Perlin噪声创建顶层的方法。 幸运的是,Perlin的噪音也可以用来创建洞穴。 这是通过计算新的Perlin噪声值来实现的,该值接收当前位置的参数乘以修饰符。 修改器的值介于0到1之间。修改器值越高,Perlin生成越混乱。 然后,我们将此值舍入为整数(0或1),并将其存储在map数组中。 查看如何实现:
public static int[,] PerlinNoiseCave(int[,] map, float modifier, bool edgesAreWalls) { int newPoint; for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { if (edgesAreWalls && (x == 0 || y == 0 || x == map.GetUpperBound(0) - 1 || y == map.GetUpperBound(1) - 1)) { map[x, y] = 1;
我们使用修饰符而不是种子,因为当乘以0到0.5的数字时,Perlin生成的结果看起来更好。 值越低,结果将越块状。 看一下样本结果。 Gif以0.01的修改器值开始,并逐渐达到0.25的值。
从此gif可以看出,每次增加的Perlin生成只会增加模式。
随机漫步
在上一部分中,我们看到您可以使用抛硬币来确定平台将在哪个位置向上或向下移动。 在这一部分中,我们将使用相同的想法,但是
左移和右移两个附加选项。 随机游走算法的这种变化使我们能够创建洞穴。 为此,我们选择一个随机方向,然后移动我们的位置并删除图块。 我们继续进行此过程,直到达到需要销毁的所需瓷砖数量。 到目前为止,我们仅使用4个方向:上,下,左,右。
public static int[,] RandomWalkCave(int[,] map, float seed, int requiredFloorPercent) {
该函数从以下内容开始:
- 找到起始位置
- 计算要删除的地砖数量。
- 在起始位置删除磁贴
- 将图块数加一。
然后我们进入
while
。 他将创建一个洞穴:
while (floorCount < reqFloorAmount) {
我们在这里做什么?
好吧,首先,借助随机数,我们选择要移动的方向。 然后,使用
switch case
检查新方向。 在此语句中,我们检查位置是否为墙。 如果不是,则从数组中删除带有图块的元素。 我们会继续这样做,直到达到所需的地板面积为止。 结果如下所示:
我还创建了该函数的自己的版本,其中还包括对角线方向。 该功能代码很长,因此,如果要查看它,请从本文此部分开头的链接下载该项目。
定向隧道
定向隧道始于地图的一个边缘,然后到达相对的边缘。 我们可以通过将隧道的曲率和粗糙度传递给输入函数来控制它们。 我们还可以设置隧道各部分的最小和最大长度。 让我们看一下实现:
public static int[,] DirectionalTunnel(int[,] map, int minPathWidth, int maxPathWidth, int maxPathChange, int roughness, int curvyness) {
这是怎么回事?
首先,我们设置宽度值。 宽度值将从负数变为正数。 因此,我们将获得所需的尺寸。 在这种情况下,我们使用值1,这将使我们的总宽度为3,因为我们使用值-1、0、1。
接下来,我们在x中设置初始位置,为此,我们采用了地图宽度的中间位置。 之后,我们可以在地图的第一部分中铺设一条隧道。
现在,让我们进入地图的其余部分。
我们生成一个随机数与粗糙度值进行比较,如果粗糙度值大于该值,则可以更改路径宽度。 我们还检查该值,以免宽度太小。 在代码的下一部分,我们将逐步浏览地图。 在每个阶段,发生以下情况:
- 与曲率值相比,我们生成了一个新的随机数。 与之前的测试一样,如果该值大于该值,则我们更改路径的中心点。 我们还会进行检查,以免超出地图范围。
- 最后,我们在新创建的零件中铺设了一条隧道。
该实现的结果如下所示:
细胞自动机
元胞自动机使用相邻单元格来确定当前单元格是打开(1)还是关闭(0)。 用于确定相邻小区的基础是基于随机生成的小区网格创建的。 我们将使用C#
Random.Next函数生成此源网格。
由于我们有几种细胞自动机的不同实现,因此我编写了一个单独的函数来生成此基本网格。 该函数如下所示:
public static int[,] GenerateCellularAutomata(int width, int height, float seed, int fillPercent, bool edgesAreWalls) {
在此功能中,您还可以设置我们的网格是否需要墙。 在所有其他方面,这非常简单。 我们检查一个具有百分比填充的随机数,以确定是否启用了当前单元格。 看一下结果:
摩尔附近
摩尔邻域用于平滑细胞自动机的初始生成。 摩尔附近看起来像这样:
以下规则适用于邻居:
- 我们在每个方向上检查邻居。
- 如果邻居是活动图块,则将其添加到周围图块的数量中。
- 如果邻居是不活动的图块,那么我们什么也不做。
- 如果一个单元具有4个以上的周围图块,则使该单元处于活动状态。
- 如果该单元格恰好有4个周围的图块,那么我们将对其不执行任何操作。
- 重复进行,直到我们检查每个地图图块。
摩尔的邻域检查功能如下:
static int GetMooreSurroundingTiles(int[,] map, int x, int y, bool edgesAreWalls) { int tileCount = 0; for(int neighbourX = x - 1; neighbourX <= x + 1; neighbourX++) { for(int neighbourY = y - 1; neighbourY <= y + 1; neighbourY++) { if (neighbourX >= 0 && neighbourX < map.GetUpperBound(0) && neighbourY >= 0 && neighbourY < map.GetUpperBound(1)) {
检查图块后,我们在平滑功能中使用此信息。 在这里,就像生成细胞自动机一样,人们可以指出地图的边缘是否应该是墙壁。
public static int[,] SmoothMooreCellularAutomata(int[,] map, bool edgesAreWalls, int smoothCount) { for (int i = 0; i < smoothCount; i++) { for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { int surroundingTiles = GetMooreSurroundingTiles(map, x, y, edgesAreWalls); if (edgesAreWalls && (x == 0 || x == (map.GetUpperBound(0) - 1) || y == 0 || y == (map.GetUpperBound(1) - 1))) {
在此必须注意,该函数具有
for
循环,该循环执行平滑指定的次数。 因此,可以获得更漂亮的卡片。
例如,如果它们之间只有两个块,我们总是可以通过连接房间来修改此算法。
冯·诺依曼社区
冯·诺伊曼附近是实现细胞自动机的另一种流行方法。 对于这样的一代,我们使用比摩尔一代更简单的邻域。 邻居看起来像这样:
以下规则适用于邻居:
- 我们检查图块的直接邻居,而不考虑对角线的邻居。
- 如果单元格处于活动状态,则将数量加一。
- 如果该单元处于非活动状态,则什么也不做。
- 如果该单元具有两个以上的邻居,则我们使当前单元处于活动状态。
- 如果该单元具有少于2个邻居,则我们使当前单元处于非活动状态。
- 如果恰好有2个邻居,则不要更改当前单元格。
第二个结果使用与第一个相同的原理,但是扩大了邻域的面积。
:
static int GetVNSurroundingTiles(int[,] map, int x, int y, bool edgesAreWalls) { int tileCount = 0;
, . ,
for
.
public static int[,] SmoothVNCellularAutomata(int[,] map, bool edgesAreWalls, int smoothCount) { for (int i = 0; i < smoothCount; i++) { for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) {
, , :
, , .
结论
, - . ,
.