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
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public struct TileCoord
{
public int tileX;
public int tileY;
public TileCoord(int _x, int _y)
{
tileX = _x;
tileY = _y;
}
public static bool operator ==(TileCoord a, TileCoord b)
{
if ((a.tileX == b.tileX) && (a.tileY == b.tileY)) return true;
else return false;
}
public static bool operator !=(TileCoord a, TileCoord b)
{
if ((a.tileX == b.tileX) && (a.tileY == b.tileY)) return false;
else return true;
}
}
public class MapGenerator : MonoBehaviour {
[SerializeField]
private Transform mapTilePrefab;
private Transform mapTileGroup;
[SerializeField]
private Transform obstaclePrefab;
private Transform obstacleGroup;
//인스펙터 조절 변수들.
public int tileNumX;
public int tileNumY;
[Range(0, 1)]
public float outLinePercent;
[Range(0, 1)]
public float obstaclesPercent;
public int randSeed = 1;
private List<TileCoord> list_tileCoord;
private Queue<TileCoord> queue_randTileCoord;
// player spwan position.
private TileCoord mapCentre;
void Start () {
GenerateMap();
}
public void GenerateMap()
{
mapCentre.tileX = tileNumX / 2;
mapCentre.tileY = tileNumY / 2;
string groupName = "mapTileGroup";
if (transform.FindChild(groupName))
{
DestroyImmediate(transform.FindChild(groupName).gameObject);
}
mapTileGroup = new GameObject(groupName).transform;
mapTileGroup.parent = transform;
groupName = "obstacleGroup";
if (transform.FindChild(groupName))
{
DestroyImmediate(transform.FindChild(groupName).gameObject);
}
obstacleGroup = new GameObject(groupName).transform;
obstacleGroup.parent = transform;
GenTileCoords();
GenRandTileCoords();
GenTiles();
GenObstacles();
}
private void GenTiles()
{
for (int x = 0; x < tileNumX; x++)
for (int y = 0; y < tileNumY; y++)
{
Transform obj = Instantiate(mapTilePrefab,
new Vector3(x, 0, y),
Quaternion.Euler(90, 0, 0)) as Transform;
obj.parent = mapTileGroup;
obj.localScale = Vector3.one * (1 - outLinePercent);
}
}
private void GenTileCoords()
{
list_tileCoord = new List<TileCoord>();
for(int x = 0; x < tileNumX; ++x)
for(int y = 0; y < tileNumY; ++y)
{
TileCoord tileCoord;
tileCoord.tileX = x;
tileCoord.tileY = y;
list_tileCoord.Add(tileCoord);
}
}
private void GenRandTileCoords()
{
queue_randTileCoord = new Queue<TileCoord>(Utility.ShuffleArray(list_tileCoord.ToArray(),
randSeed));
}
private TileCoord GetRandomCoord()
{
TileCoord tileCoord = queue_randTileCoord.Dequeue();
queue_randTileCoord.Enqueue(tileCoord);
return tileCoord;
}
private Vector3 CoordToPosition(TileCoord _coord)
{
return new Vector3(_coord.tileX, 0, _coord.tileY);
}
/// <summary>
/// Flood fill 알고리즘을 이용한 중복제거.
/// </summary>
/// <param name="obstacleMap"></param>
/// <param name="currentObstacleNum"></param>
/// <returns></returns>
private bool IsMapFullyAccessible(bool [,] obstacleMap, int currentObstacleNum)
{
bool[,] mapFlags = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)];
Queue<TileCoord> queue_nodes = new Queue<TileCoord>();
queue_nodes.Enqueue(mapCentre);
mapFlags[mapCentre.tileX, mapCentre.tileY] = true;
int accessibleTileCount = 1;
while(queue_nodes.Count > 0)
{
TileCoord tileCoord = queue_nodes.Dequeue();
for(int x = -1; x <=1; x++)
for (int y = -1; y <= 1; ++y){
int neighborX = tileCoord.tileX + x;
int neighborY = tileCoord.tileY + 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_nodes.Enqueue(new TileCoord(neighborX, neighborY));
accessibleTileCount++;
}
}
}
}
}
int targetAccessibleTileCount = (tileNumX * tileNumY) - currentObstacleNum;
return accessibleTileCount == targetAccessibleTileCount;
}
private void GenObstacles()
{
bool[,] obstacleMap = new bool[tileNumX, tileNumY];
int curObstacleNum = 0;
int obstacleNum = (int)(tileNumX * tileNumY * obstaclesPercent);
for (int idx = 0; idx < obstacleNum; ++idx)
{
TileCoord tileRandCoord = GetRandomCoord();
obstacleMap[tileRandCoord.tileX, tileRandCoord.tileY] = true;
curObstacleNum++;
if ((tileRandCoord != mapCentre) &&
IsMapFullyAccessible(obstacleMap, curObstacleNum))
{
Vector3 obstaclePos = CoordToPosition(tileRandCoord);
obstaclePos.y += 0.5f;
Transform newObstacle = Instantiate(obstaclePrefab,
obstaclePos,
Quaternion.identity) as Transform;
newObstacle.parent = obstacleGroup;
}
else
{
obstacleMap[tileRandCoord.tileX, tileRandCoord.tileY] = false;
curObstacleNum--;
}
}
}
}
|
댓글 없음:
댓글 쓰기