-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathLevelGeneratorStack.cs
More file actions
182 lines (162 loc) · 6.08 KB
/
LevelGeneratorStack.cs
File metadata and controls
182 lines (162 loc) · 6.08 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LevelGeneratorStack : MonoBehaviour
{
public Tilemap tilemap;
public TileBase tile;
[Tooltip("The width of each layer of the stack")]
public int width;
[Tooltip("The height of each layer of the stack")]
public int height;
[SerializeField]
public List<MapSettings> mapSettings = new List<MapSettings>();
List<int[,]> mapList = new List<int[,]>();
private void Update()
{
if(Input.GetKeyDown(KeyCode.N))
{
tilemap.GetComponent<Tilemap>().ClearAllTiles();
GenerateMap();
}
}
[ExecuteInEditMode]
public void GenerateMap()
{
ClearMap();
mapList = new List<int[,]>();
//Work through the List of mapSettings
for (int i = 0; i < mapSettings.Count; i++)
{
int[,] map = new int[width,height];
float seed;
if(mapSettings[i].randomSeed)
{
seed = Time.time.GetHashCode();
}
else
{
seed = mapSettings[i].seed.GetHashCode();
}
//Generate the map depending on the algorithm selected
switch(mapSettings[i].algorithm)
{
case Algorithm.Perlin:
//First generate our array
map = MapFunctions.GenerateArray(width, height, true);
//Next generate the perlin noise onto the array
map = MapFunctions.PerlinNoise(map, seed);
break;
case Algorithm.PerlinSmoothed:
//First generate our array
map = MapFunctions.GenerateArray(width, height, true);
//Next generate the perlin noise onto the array
map = MapFunctions.PerlinNoiseSmooth(map, seed, mapSettings[i].interval);
break;
case Algorithm.PerlinCave:
//First generate our array
map = MapFunctions.GenerateArray(width, height, true);
//Next generate the perlin noise onto the array
map = MapFunctions.PerlinNoiseCave(map, mapSettings[i].modifier, mapSettings[i].edgesAreWalls);
break;
case Algorithm.RandomWalkTop:
//First generate our array
map = MapFunctions.GenerateArray(width, height, true);
//Next generater the random top
map = MapFunctions.RandomWalkTop(map, seed);
break;
case Algorithm.RandomWalkTopSmoothed:
//First generate our array
map = MapFunctions.GenerateArray(width, height, true);
//Next generate the smoothed random top
map = MapFunctions.RandomWalkTopSmoothed(map, seed, mapSettings[i].interval);
break;
case Algorithm.RandomWalkCave:
//First generate our array
map = MapFunctions.GenerateArray(width, height, false);
//Next generate the random walk cave
map = MapFunctions.RandomWalkCave(map, seed, mapSettings[i].clearAmount);
break;
case Algorithm.RandomWalkCaveCustom:
//First generate our array
map = MapFunctions.GenerateArray(width, height, false);
//Next generate the custom random walk cave
map = MapFunctions.RandomWalkCaveCustom(map, seed, mapSettings[i].clearAmount);
break;
case Algorithm.CellularAutomataVonNeuman:
//First generate the cellular automata array
map = MapFunctions.GenerateCellularAutomata(width, height, seed, mapSettings[i].fillAmount, mapSettings[i].edgesAreWalls);
//Next smooth out the array using the von neumann rules
map = MapFunctions.SmoothVNCellularAutomata(map, mapSettings[i].edgesAreWalls, mapSettings[i].smoothAmount);
break;
case Algorithm.CellularAutomataMoore:
//First generate the cellular automata array
map = MapFunctions.GenerateCellularAutomata(width, height, seed, mapSettings[i].fillAmount, mapSettings[i].edgesAreWalls);
//Next smooth out the array using the Moore rules
map = MapFunctions.SmoothMooreCellularAutomata(map, mapSettings[i].edgesAreWalls, mapSettings[i].smoothAmount);
break;
case Algorithm.DirectionalTunnel:
//First generate our array
map = MapFunctions.GenerateArray(width, height, false);
//Next generate the tunnel through the array
map = MapFunctions.DirectionalTunnel(map, mapSettings[i].minPathWidth, mapSettings[i].maxPathWidth, mapSettings[i].maxPathChange, mapSettings[i].roughness, mapSettings[i].windyness);
break;
}
//Add the map to the list
mapList.Add(map);
}
//Allows for all of the maps to be on the same tilemap without overlaying
Vector2Int offset = new Vector2Int(-width / 2, (-height / 2) - 1);
//Work through the list to generate all maps
foreach(int[,] map in mapList)
{
MapFunctions.RenderMapWithOffset(map, tilemap, tile, offset);
offset.y += -height + 1;
}
}
public void ClearMap()
{
tilemap.ClearAllTiles();
}
}
[CustomEditor(typeof(LevelGeneratorStack))]
public class LevelGeneratorStackEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
//Create a reference to our script
LevelGeneratorStack levelGen = (LevelGeneratorStack)target;
//List of editors to only show if we have elements in the map settings list
List <Editor> mapEditors = new List<Editor>();
for(int i = 0; i < levelGen.mapSettings.Count; i++)
{
if (levelGen.mapSettings[i] != null)
{
Editor mapLayerEditor = CreateEditor(levelGen.mapSettings[i]);
mapEditors.Add(mapLayerEditor);
}
}
//If we have more than one editor in our editor list, draw them out. Also draw the buttons
if (mapEditors.Count > 0)
{
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
for (int i = 0; i < mapEditors.Count; i++)
{
mapEditors[i].OnInspectorGUI();
}
if (GUILayout.Button("Generate"))
{
levelGen.GenerateMap();
}
if (GUILayout.Button("Clear"))
{
levelGen.ClearMap();
}
}
}
}