-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathLevelGenerator.cs
More file actions
149 lines (136 loc) · 4.48 KB
/
LevelGenerator.cs
File metadata and controls
149 lines (136 loc) · 4.48 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LevelGenerator : MonoBehaviour {
[Tooltip("The Tilemap to draw onto")]
public Tilemap tilemap;
[Tooltip("The Tile to draw (use a RuleTile for best results)")]
public TileBase tile;
[Tooltip("Width of our map")]
public int width;
[Tooltip("Height of our map")]
public int height;
[Tooltip("The settings of our map")]
public MapSettings mapSetting;
private void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
ClearMap();
GenerateMap();
}
}
[ExecuteInEditMode]
public void GenerateMap()
{
ClearMap();
int[,] map = new int[width, height];
float seed;
if (mapSetting.randomSeed)
{
seed = Time.time;
}
else
{
seed = mapSetting.seed;
}
//Generate the map depending omapSen the algorithm selected
switch (mapSetting.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, mapSetting.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, mapSetting.modifier, mapSetting.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, mapSetting.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, mapSetting.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, mapSetting.clearAmount);
break;
case Algorithm.CellularAutomataVonNeuman:
//First generate the cellular automata array
map = MapFunctions.GenerateCellularAutomata(width, height, seed, mapSetting.fillAmount, mapSetting.edgesAreWalls);
//Next smooth out the array using the von neumann rules
map = MapFunctions.SmoothVNCellularAutomata(map, mapSetting.edgesAreWalls, mapSetting.smoothAmount);
break;
case Algorithm.CellularAutomataMoore:
//First generate the cellular automata array
map = MapFunctions.GenerateCellularAutomata(width, height, seed, mapSetting.fillAmount, mapSetting.edgesAreWalls);
//Next smooth out the array using the Moore rules
map = MapFunctions.SmoothMooreCellularAutomata(map, mapSetting.edgesAreWalls, mapSetting.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, mapSetting.minPathWidth, mapSetting.maxPathWidth, mapSetting.maxPathChange, mapSetting.roughness, mapSetting.windyness);
break;
}
//Render the result
MapFunctions.RenderMap(map, tilemap, tile);
}
public void ClearMap()
{
tilemap.ClearAllTiles();
}
}
[CustomEditor(typeof(LevelGenerator))]
public class LevelGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
//Reference to our script
LevelGenerator levelGen = (LevelGenerator)target;
//Only show the mapsettings UI if we have a reference set up in the editor
if (levelGen.mapSetting != null)
{
Editor mapSettingEditor = CreateEditor(levelGen.mapSetting);
mapSettingEditor.OnInspectorGUI();
if (GUILayout.Button("Generate"))
{
levelGen.GenerateMap();
}
if (GUILayout.Button("Clear"))
{
levelGen.ClearMap();
}
}
}
}