-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldController.cs
362 lines (315 loc) · 11.9 KB
/
WorldController.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System.Collections.Generic;
using UnityEngine;
using static TurnOrder;
public class WorldController : MonoBehaviour
{
//Blocks
private static GameObject block;
private static GameObject world;
private static GameObject player;
private static List<Enemy> enemies;
private static Dictionary<(int, int), GameObject> tiles;
private static Dictionary<(int, int), GameObject> floorTiles;
private static Dictionary<Vector2Int, int> distanceFromPlayer;
private static Queue<Vector2Int> nextTile;
private static List<Effect> effects;
//Monobehavior
private void Awake()
{
tiles = new Dictionary<(int, int), GameObject>();
floorTiles = new Dictionary<(int, int), GameObject>();
distanceFromPlayer = new Dictionary<Vector2Int, int>();
nextTile = new Queue<Vector2Int>();
player = GameObject.Find("Player");
world = GameObject.Find("World");
block = (GameObject)Resources.Load("Prefab/Other/Block", typeof(GameObject));
enemies = new List<Enemy>();
effects = new List<Effect>();
enemies.Add(Resources.Load<GameObject>("Prefab/Slime").GetComponent<Enemy>());
enemies.Add(Resources.Load<GameObject>("Prefab/Spirit").GetComponent<Enemy>());
}
private void Start()
{
GetComponent<WorldGen>().GenerateWorld();
}
//Effects
public static void Add(Effect effect)
{
effects.Add(effect);
}
public static void Remove(Effect effect)
{
effects.Remove(effect);
}
public static void EffectTurn()
{
foreach(Effect effect in new List<Effect>(effects))
{
effect.Turn();
}
}
//Distance From Player
public static void SetDistanceFromPlayer()
{
nextTile.Clear();
distanceFromPlayer.Clear();
Vector2Int playerPosition = player.GetComponent<Entity>().PlayerPosition();
nextTile.Enqueue(playerPosition);
distanceFromPlayer.Add(playerPosition, 0);
while(nextTile.Count > 0)
{
Vector2Int currTile = nextTile.Dequeue();
int dist = distanceFromPlayer[currTile];
Vector2Int[] adjacent = GetAdjacent(currTile);
foreach(Vector2Int tile in adjacent)
{
if (distanceFromPlayer.ContainsKey(tile)) continue;
if (GetTile(tile) != null && GetTile(tile).GetComponent<Block>() != null) continue;
//if (tiles.ContainsKey((tile.x, tile.y))) continue;
if (!floorTiles.ContainsKey((tile.x, tile.y))) continue;
distanceFromPlayer[tile] = dist + 1;
nextTile.Enqueue(tile);
}
}
}
public static int GetDistanceFromPlayer(Vector2Int currTile)
{
if (distanceFromPlayer.ContainsKey(currTile) == false) return -1;
return distanceFromPlayer[currTile];
}
public static Vector2Int FarthestTileFromPlayer(Vector2Int currTile)
{
Vector2Int[] adjacent = GetAdjacent(currTile);
Vector2Int farthest = currTile;
int farthestDist = distanceFromPlayer[currTile];
foreach (Vector2Int tile in adjacent)
{
if (!distanceFromPlayer.ContainsKey(tile)) continue;
if (tiles.ContainsKey((tile.x, tile.y))) continue;
if (!floorTiles.ContainsKey((tile.x, tile.y))) continue;
if (farthestDist < distanceFromPlayer[tile])
{
farthest = tile;
farthestDist = distanceFromPlayer[tile];
}
if (farthestDist == distanceFromPlayer[tile] && farthest != currTile)
{
if (PlayerPosition().x == tile.x || PlayerPosition().y == tile.y) farthest = tile;
}
}
return farthest;
}
public static Vector2Int GetClosestTileToPlayer(Vector2Int currTile)
{
Vector2Int[] adjacent = GetAdjacent(currTile);
Vector2Int closest = currTile;
if (!distanceFromPlayer.ContainsKey(currTile)) return currTile;
int closestDist = distanceFromPlayer[currTile];
foreach(Vector2Int tile in adjacent)
{
if (!distanceFromPlayer.ContainsKey(tile)) continue;
if (tiles.ContainsKey((tile.x, tile.y))) continue;
if (!floorTiles.ContainsKey((tile.x, tile.y))) continue;
if (closestDist > distanceFromPlayer[tile])
{
closest = tile;
closestDist = distanceFromPlayer[tile];
}
if (closestDist == distanceFromPlayer[tile] && closest != currTile)
{
if (Vector2.Distance(tile, PlayerPosition()) < Vector2.Distance(closest, PlayerPosition())) closest = tile;
}
}
return closest;
}
protected static Vector2Int PlayerPosition()
{
return new Vector2Int((int)player.transform.position.x, (int)player.transform.position.y);
}
public static Vector2Int[] GetAdjacent(Vector2Int currTile)
{
Vector2Int[] adjacent = new Vector2Int[8];
adjacent[0] = new Vector2Int(currTile.x - 1, currTile.y + 1);
adjacent[1] = new Vector2Int(currTile.x + 0, currTile.y + 1);
adjacent[2] = new Vector2Int(currTile.x + 1, currTile.y + 1);
adjacent[3] = new Vector2Int(currTile.x - 1, currTile.y + 0);
adjacent[4] = new Vector2Int(currTile.x + 1, currTile.y + 0);
adjacent[5] = new Vector2Int(currTile.x - 1, currTile.y - 1);
adjacent[6] = new Vector2Int(currTile.x + 0, currTile.y - 1);
adjacent[7] = new Vector2Int(currTile.x + 1, currTile.y - 1);
return adjacent;
}
//Enemy Spawn
public static void SpawnEnemy(int x, int y)
{
int index = Random.Range(0, enemies.Count);
index = 0;
Instantiate(enemies[index].gameObject, new Vector2(x, y), Quaternion.identity);
}
//Block methods
public static void Kill(GameObject curr)
{
RemoveFromTurn(curr);
tiles.Remove(((int)curr.transform.position.x, (int)curr.transform.position.y));
Destroy(curr);
}
public static void MoveWorldLocation(Transform curr, int xMove, int yMove)
{
int x = (int)curr.position.x;
int y = (int)curr.position.y;
if (!Empty(x + xMove, y + yMove)) return;
curr.position = new Vector2(x + xMove, y + yMove);
tiles[(x + xMove, y + yMove)] = curr.gameObject;
tiles.Remove((x, y));
}
public static void MoveToWorldPoint(Transform curr, Vector2Int newLocation)
{
if (!Empty(newLocation.x, newLocation.y)) return;
tiles.Remove(((int)curr.transform.position.x, (int)curr.transform.position.y));
curr.position = (Vector2)newLocation;
AddTile(curr.gameObject, newLocation);
}
public static void MoveToWorldPoint(Transform curr, Vector2Int prevPosition, Vector2Int newLocation)
{
if (!Empty(newLocation.x, newLocation.y)) return;
tiles.Remove((prevPosition.x, prevPosition.y));
curr.position = (Vector2)newLocation;
AddTile(curr.gameObject, newLocation);
}
public static void AddTile(GameObject curr, Vector2Int position)
{
if (!tiles.ContainsKey((position.x, position.y)))
{
tiles[(position.x, position.y)] = curr;
Step(position, curr);
}
}
public static void AddToWorld(GameObject curr, int x, int y)
{
if (!tiles.ContainsKey((x, y)))
{
tiles[(x, y)] = curr;
}
}
public static void Step(Vector2Int position, GameObject target)
{
if (floorTiles.ContainsKey((position.x, position.y)))
{
floorTiles[(position.x, position.y)].GetComponent<Block>().SteppedOn(target);
}
}
public static void Enable(int x, int y)
{
if (tiles.ContainsKey((x, y)))
{
tiles[(x, y)].SetActive(true);
}
if (floorTiles.ContainsKey((x,y)))
{
floorTiles[(x, y)].SetActive(true);
}
}
public static void Disable(int x, int y)
{
if (tiles.ContainsKey((x, y)))
{
tiles[(x, y)].SetActive(false);
}
if (floorTiles.ContainsKey((x, y)))
{
floorTiles[(x, y)].SetActive(false);
}
}
public static bool Create(int x, int y, Sprite blockSprite, Vector3? rotation = null, int sortingOrder=0)
{
if (!tiles.ContainsKey((x, y)))
{
tiles[(x, y)] = block.GetComponent<Block>().Create(x, y, world.transform, blockSprite, sortingOrder);
if (rotation != null)
{
Vector3 blockRotation = tiles[(x, y)].transform.rotation.eulerAngles;
blockRotation.z = rotation.Value.z;
tiles[(x, y)].transform.rotation = Quaternion.Euler((Vector3)rotation);
}
//if(!level) blocks[(x, y)].SetActive(false);
return true;
}
else return false;
}
public static bool CreateBackground(int x, int y, Sprite blockSprite, int sortingOrder = 0)
{
if (!floorTiles.ContainsKey((x, y)))
{
floorTiles[(x, y)] = block.GetComponent<Block>().Create(x, y, world.transform, blockSprite, sortingOrder);
floorTiles[(x, y)].GetComponent<Block>().background = true;
floorTiles[(x, y)].GetComponent<BoxCollider2D>().isTrigger = true;
floorTiles[(x, y)].GetComponent<SpriteRenderer>().color = new Color32(120, 120, 120, 255);
floorTiles[(x, y)].GetComponent<SpriteRenderer>().sortingOrder = -y - 1;
//if (!level) backBlocks[(x, y)].SetActive(false);
return true;
}
else return false;
}
public static GameObject GetGround(int x, int y)
{
if (floorTiles.ContainsKey((x, y)))
{
return floorTiles[(x, y)];
}
else return null;
}
public static bool Place(int x, int y, GameObject b)
{
if (!tiles.ContainsKey((x, y)))
{
tiles[(x, y)] = b.GetComponent<Block>().Place(x, y);
tiles[(x, y)].SetActive(true);
return true;
}
else return false;
}
public static GameObject PickUp(int x, int y)
{
if (tiles.ContainsKey((x, y)))
{
GameObject b = tiles[(x, y)];
tiles.Remove((x, y));
b.GetComponent<Block>().PickUp();
return b;
}
else return null;
}
public static GameObject Get(int x, int y)
{
if (tiles.ContainsKey((x, y)))
{
return tiles[(x, y)];
}
else return null;
}
public static bool Empty(int x, int y)
{
if (tiles.ContainsKey((x, y)))
{
return false;
}
else return true;
}
public static GameObject GetTile(Vector2Int position)
{
if (tiles.ContainsKey((position.x, position.y))) return tiles[(position.x, position.y)];
return null;
}
public static GameObject GetGround(Vector2Int position)
{
if (floorTiles.ContainsKey((position.x, position.y))) return floorTiles[(position.x, position.y)];
return null;
}
public static List<GameObject> GetAll(Vector2Int position)
{
List<GameObject> result = new List<GameObject>();
if (tiles.ContainsKey((position.x, position.y))) result.Add(tiles[(position.x, position.y)]);
if (floorTiles.ContainsKey((position.x, position.y))) result.Add(floorTiles[(position.x, position.y)]);
return result;
}
}