-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapGenerator.cs
257 lines (210 loc) · 7.74 KB
/
MapGenerator.cs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour
{
public Map[] maps;
public int mapIndex;
public Transform tilePrefab;
public Transform obstaclePrefab;
public Transform mapFloor;
public Transform navmeshFloor;
public Transform navmeshMaskPrefab;
public Vector2 maxMapSize;
[Range(0,1)]
public float outlinePercent;
public float tileSize;
List<Coord> allTileCoords;
Queue<Coord> shuffledTileCoords;
Queue<Coord> shuffledOpenTileCoords;
Transform[,] tileMap;
Map currentMap;
public Map CurrentMap { get { return currentMap; }}
void Awake()
{
FindObjectOfType<Spawner>().OnNewWave += OnNewWave;
}
void OnNewWave(int waveNumber)
{
mapIndex = waveNumber - 1;
GenerateMap();
}
public void GenerateMap()
{
currentMap = maps[mapIndex];
tileMap = new Transform[currentMap.mapSize.x, currentMap.mapSize.y];
var rand = new System.Random(currentMap.seed);
// Generating coords
allTileCoords = new List<Coord>();
for (var x = 0; x < currentMap.mapSize.x; x++)
{
for (var y = 0; y < currentMap.mapSize.y; y++)
{
allTileCoords.Add(new Coord(x, y));
}
}
shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), currentMap.seed));
// Create map holder object
var holderName = "Generated Map";
if (transform.FindChild(holderName))
{
DestroyImmediate(transform.FindChild(holderName).gameObject);
}
var mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
// Spawning tiles
for (var x = 0; x < currentMap.mapSize.x; x++)
{
for (var y = 0; y < currentMap.mapSize.y; y++)
{
var tilePosition = CoordToPosition(x, y);
var newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent) * tileSize;
newTile.parent = mapHolder;
tileMap[x, y] = newTile;
}
}
// Spawning obstacles
bool[,] obstacleMap = new bool[(int)currentMap.mapSize.x, (int)currentMap.mapSize.y];
var obstacleCount = (int)(currentMap.mapSize.x * currentMap.mapSize.y * currentMap.obstaclePercent);
var currentObstacleCount = 0;
var allOpenCoords = new List<Coord>(allTileCoords);
for (var i = 0; i < obstacleCount; i++)
{
var randomCoord = GetRandomCoord();
obstacleMap[randomCoord.x, randomCoord.y] = true;
currentObstacleCount++;
if (randomCoord != currentMap.mapCenter && MapIsFullyAccessible(obstacleMap, currentObstacleCount))
{
var obstacleHeight = Mathf.Lerp(currentMap.minObstacleHeight, currentMap.maxObstacleHeight, (float)rand.NextDouble());
var obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);
var newObstacle = Instantiate(obstaclePrefab, obstaclePosition + Vector3.up * obstacleHeight / 2, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
newObstacle.localScale = new Vector3((1 - outlinePercent) * tileSize, obstacleHeight, (1 - outlinePercent) * tileSize);
var obstacleRenderer = newObstacle.GetComponent<Renderer>();
var obstacleMaterial = new Material(obstacleRenderer.sharedMaterial);
float colorPercent = randomCoord.y / (float)currentMap.mapSize.y;
obstacleMaterial.color = Color.Lerp(currentMap.foregroundColor, currentMap.backgroundColor, colorPercent);
obstacleRenderer.sharedMaterial = obstacleMaterial;
allOpenCoords.Remove(randomCoord);
}
else
{
obstacleMap[randomCoord.x, randomCoord.y] = false;
currentObstacleCount--;
}
}
shuffledOpenTileCoords = new Queue<Coord>(Utility.ShuffleArray(allOpenCoords.ToArray(), currentMap.seed));
// Creating the navmesh mask
var maskLeft = Instantiate(navmeshMaskPrefab, Vector3.left * (currentMap.mapSize.x + maxMapSize.x) / 4f * tileSize, Quaternion.identity) as Transform;
maskLeft.parent = mapHolder;
maskLeft.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2f, 1, currentMap.mapSize.y) * tileSize;
var maskRight = Instantiate(navmeshMaskPrefab, Vector3.right * (currentMap.mapSize.x + maxMapSize.x) / 4f * tileSize, Quaternion.identity) as Transform;
maskRight.parent = mapHolder;
maskRight.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2f, 1, currentMap.mapSize.y) * tileSize;
var maskTop = Instantiate(navmeshMaskPrefab, Vector3.forward * (currentMap.mapSize.y + maxMapSize.y) / 4f * tileSize, Quaternion.identity) as Transform;
maskTop.parent = mapHolder;
maskTop.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y) / 2f) * tileSize;
var maskBottom = Instantiate(navmeshMaskPrefab, Vector3.back * (currentMap.mapSize.y + maxMapSize.y) / 4f * tileSize, Quaternion.identity) as Transform;
maskBottom.parent = mapHolder;
maskBottom.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y) / 2f) * tileSize;
navmeshFloor.localScale = new Vector3(maxMapSize.x, maxMapSize.y) * tileSize;
mapFloor.localScale = new Vector3(currentMap.mapSize.x * tileSize, currentMap.mapSize.y * tileSize);
}
bool MapIsFullyAccessible(bool[,] obstacleMap, int currentObstacleCount)
{
var mapFlags = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)];
var queue = new Queue<Coord>();
queue.Enqueue(currentMap.mapCenter);
mapFlags[currentMap.mapCenter.x, currentMap.mapCenter.y] = true;
var accessibleTileCount = 1;
while (queue.Count > 0)
{
var tile = queue.Dequeue();
for (var x = -1; x <= 1; x++)
{
for (var y = -1; y <= 1; y++)
{
var neighborX = tile.x + x;
var neighborY = tile.y + y;
if (x == 0 || y == 0)
{
if (neighborX >= 0 && neighborX < obstacleMap.GetLength(0) && neighborY >= 0 && neighborY < obstacleMap.GetLength(1))
{
if (!mapFlags[neighborX, neighborY] && !obstacleMap[neighborX, neighborY])
{
mapFlags[neighborX, neighborY] = true;
queue.Enqueue(new Coord(neighborX, neighborY));
accessibleTileCount++;
}
}
}
}
}
}
var targetAccessibleTileCount = (int)(currentMap.mapSize.x * currentMap.mapSize.y - currentObstacleCount);
return targetAccessibleTileCount == accessibleTileCount;
}
Vector3 CoordToPosition(int x, int y)
{
return new Vector3(-currentMap.mapSize.x / 2f + 0.5f + x, 0, -currentMap.mapSize.y / 2f + 0.5f + y) * tileSize;
}
public Transform GetTileFromPosition(Vector3 position)
{
var x = Mathf.RoundToInt(position.x / tileSize + (currentMap.mapSize.x - 1) / 2f);
var y = Mathf.RoundToInt(position.z / tileSize + (currentMap.mapSize.y - 1) / 2f);
x = Mathf.Clamp(x, 0, tileMap.GetLength(0) - 1);
y = Mathf.Clamp(y, 0, tileMap.GetLength(1) - 1);
return tileMap[x, y];
}
public Coord GetRandomCoord()
{
var randomCoord = shuffledTileCoords.Dequeue();
shuffledTileCoords.Enqueue(randomCoord);
return randomCoord;
}
public Transform GetRandomOpenTile()
{
var randomCoord = shuffledOpenTileCoords.Dequeue();
shuffledOpenTileCoords.Enqueue(randomCoord);
return tileMap[randomCoord.x, randomCoord.y];
}
[System.Serializable]
public struct Coord
{
public int x;
public int y;
public Coord(int _x, int _y)
{
x = _x;
y = _y;
}
public static bool operator ==(Coord c1, Coord c2)
{
return c1.x == c2.x && c1.y == c2.y;
}
public static bool operator !=(Coord c1, Coord c2)
{
return !(c1 == c2);
}
}
[System.Serializable]
public class Map
{
public Coord mapSize;
[Range(0, 1)]
public float obstaclePercent;
public int seed;
public float minObstacleHeight;
public float maxObstacleHeight;
public Color foregroundColor;
public Color backgroundColor;
public Coord mapCenter
{
get
{
return new Coord(mapSize.x / 2, mapSize.y / 2);
}
}
}
}