-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldGen.cs
More file actions
221 lines (191 loc) · 7.93 KB
/
WorldGen.cs
File metadata and controls
221 lines (191 loc) · 7.93 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using static WorldController;
using static Settings;
public class WorldGen : MonoBehaviour
{
private Dictionary<(int, int), string> chunks;
private Grid[] allsides;
private Grid[] leftright;
private Grid[] leftrighttop;
private Grid[] leftrightbottom;
private Grid[] special;
private Grid filled;
private Grid empty;
//Start
public void GenerateWorld()
{
chunks = new Dictionary<(int, int), string>();
allsides = GetGrid(Resources.LoadAll<GameObject>("Prefab/Dungeon/AllSides"));
leftright = GetGrid(Resources.LoadAll<GameObject>("Prefab/Dungeon/LeftRight"));
leftrighttop = GetGrid(Resources.LoadAll<GameObject>("Prefab/Dungeon/LeftRightTop"));
leftrightbottom = GetGrid(Resources.LoadAll<GameObject>("Prefab/Dungeon/RightLeftBottom"));
special = GetGrid(Resources.LoadAll<GameObject>("Prefab/Dungeon/Special"));
filled = Resources.Load<GameObject>("Prefab/Dungeon/Filled").GetComponent<Grid>();
empty = Resources.Load<GameObject>("Prefab/Dungeon/Empty").GetComponent<Grid>();
ChunkGeneration();
FillEdge();
//CreateBackgrounds();
}
//Chunk Generation
private void FillEdge()
{
for (int chunkY = 2; chunkY >= -numChunks; chunkY--)
{
for (int chunkX = -numChunks-1; chunkX <= numChunks; chunkX++)
{
if (chunkY == 2 || chunkY == -numChunks || chunkX == -numChunks - 1 || chunkX == numChunks)
{
PlaceChunk(chunkX, chunkY, filled);
}
}
}
}
private void ChunkGeneration()
{
for (int chunkY = 1; chunkY > -numChunks; chunkY--)
{
for (int chunkX = -numChunks; chunkX < numChunks; chunkX++)
{
Grid chunkGrid = PickChunk(chunkX, chunkY);
PlaceChunk(chunkX, chunkY, chunkGrid);
if (chunkX == 1 && chunkY == 1) PlaceChunkPath(chunkX, chunkY - 1, "down");
}
}
}
private void PlaceChunk(int chunkX, int chunkY, Grid chunkGrid)
{
if (chunks.ContainsKey((chunkX, chunkY))) return;
//Tilemap chunk = chunkGrid.transform.GetChild(0).gameObject.GetComponent<Tilemap>();
chunks[(chunkX, chunkY)] = chunkGrid.name;
for (int x = -chunksize / 2; x < chunksize / 2; x++)
{
for (int y = 0; y > -chunksize; y--)
{
PlaceTile(x, y, chunkX, chunkY, chunkGrid);
}
}
}
private void PlaceTile(int x, int y, int chunkX, int chunkY, Grid chunkGrid)
{
Tilemap[] chunkLayers = chunkGrid.GetComponentsInChildren<Tilemap>();
int currX = x + (chunkX * chunksize);
int currY = y + (chunkY * chunksize);
foreach(Tilemap chunk in chunkLayers)
{
TileBase currTile = chunk.GetTile(new Vector3Int(x, y, 0));
Sprite tileSprite = chunk.GetSprite(new Vector3Int(x, y, 0));
Vector3 rotation = chunk.GetTransformMatrix(new Vector3Int(x, y, 0)).rotation.eulerAngles;
int sortingOrder = chunk.gameObject.GetComponent<TilemapRenderer>().sortingOrder;
if (currTile)
{
if (currTile.name == "Background" || currTile.name.Contains("Floor"))
{
CreateBackground(currX, currY, tileSprite, sortingOrder);
}
else
{
Create(currX, currY, tileSprite, rotation, sortingOrder);
}
}
}
}
private void PlaceChunkPath(int chunkx, int chunky, string prevDirection)
{
Grid chunkGrid = GetChunkType(chunkx, chunky, prevDirection);
PlaceChunk(chunkx, chunky, chunkGrid);
string newDirection = GetDirection(chunkx, chunky, chunkGrid);
if (newDirection == "down") chunky -= 1;
if (newDirection == "left") chunkx -= 1;
if (newDirection == "right") chunkx += 1;
if (newDirection != null) PlaceChunkPath(chunkx, chunky, newDirection);
}
private string GetDirection(int chunkx, int chunky, Grid chunkType)
{
bool leftChunk = chunks.ContainsKey((chunkx - 1, chunky));
bool rightChunk = chunks.ContainsKey((chunkx + 1, chunky));
int direction = -1; //null=-1, down=0, left=1, right=2, down=3
//if chunk blocking
bool canLeft = !leftChunk;
bool canRight = !rightChunk;
bool canDown = true;
//if reached end of map
if (Contains(leftrighttop, chunkType) || Contains(leftright, chunkType)) canDown = false;
//if (chunkType == leftrighttop[0] || chunkType == leftright[0]) canDown = false;
if (chunkx == -numChunks) canLeft = false;
if (chunkx == numChunks - 1) canRight = false;
if (chunky == -numChunks + 1) canDown = false;
//get direction
if (canLeft && canRight && canDown) direction = Random.Range(0, 3);
if (!canLeft && canRight && canDown) direction = Random.Range(2, 4);
if (canLeft && !canRight && canDown) direction = Random.Range(0, 2);
if (canLeft && canRight && !canDown) direction = Random.Range(1, 3);
if (canLeft && !canRight && !canDown) direction = 1;
if (!canLeft && canRight && !canDown) direction = 2;
if (!canLeft && !canRight && canDown) direction = 0;
//return direction
if (direction == 0) return "down";
if (direction == 1) return "left";
if (direction == 2) return "right";
if (direction == 3) return "down";
else return null;
}
private bool Contains(Grid[] rooms, Grid item)
{
foreach (Grid room in rooms)
{
if (room == item) return true;
}
return false;
}
private Grid GetChunkType(int chunkx, int chunky, string prevDirection)
{
bool canLeft = !chunks.ContainsKey((chunkx - 1, chunky));
bool canRight = !chunks.ContainsKey((chunkx + 1, chunky));
if (chunkx == -numChunks) canLeft = false;
if (chunkx == numChunks - 1) canRight = false;
int chunkType = 0; //all=0; LRT=1, LRB=2, LR=3
if (prevDirection == "down") chunkType = Random.Range(0, 2);
else if (!canLeft && !canRight) chunkType = Random.Range(0, 3);
else chunkType = Random.Range(0, 4);
if (!canLeft && !canRight && chunkType == 1) chunkType = 2;
if (chunkType == 0) return RandomRoom(allsides);
if (chunkType == 1) return RandomRoom(leftrighttop);
if (chunkType == 2) return RandomRoom(leftrightbottom);
if (chunkType == 3) return RandomRoom(leftright);
else return null;
}
private Grid RandomRoom(Grid[] rooms)
{
int rand = Random.Range(0, rooms.Length);
return rooms[rand];
}
private Grid PickChunk(int chunkX, int chunkY)
{
int chunkType = Random.Range(0, 5);
if (chunkX == 0 && chunkY == 1) return empty;
if (chunkType == 0) return RandomRoom(allsides);
if (chunkType == 1) return RandomRoom(leftrighttop);
if (chunkType == 2) return RandomRoom(leftrightbottom);
if (chunkType == 3) return RandomRoom(leftright);
if (chunkType == 4) return RandomRoom(special);
return null;
}
private Grid[] GetGrid(GameObject[] input)
{
//Debug.Log(input.Length);
Grid[] output = new Grid[input.Length];
for (int i = 0; i < input.Length; i++)
{
output[i] = input[i].GetComponent<Grid>();
}
return output;
}
//Getters and Setters
public static int GetMapLength()
{
return numChunks * chunksize;
}
}