-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cs
648 lines (565 loc) · 25.1 KB
/
World.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Limestone.Utility;
using Limestone.Tiles;
using Limestone.Entities;
using Limestone.Entities.Enemies;
using Limestone.Items;
using Limestone.Generation;
using Limestone.Guis;
using Limestone.Serialization;
using Limestone.Entities.Collectables;
namespace Limestone
{
[JsonObject(MemberSerialization.OptIn)]
public class World
{
public static int worldTimer;
public Player2 player;
Effect testEffect;
[JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
public Tile[,] tiles;
public List<TileWall> wallTiles = new List<TileWall>();
private List<Tile> drawTiles = new List<Tile>();
private List<Tile> spawnCheckTiles = new List<Tile>();
public List<Projectile> projectiles = new List<Projectile>();
public List<Enemy> enemies = new List<Enemy>();
public List<EntitySpawner> spawners = new List<EntitySpawner>();
public List<Particle> particles = new List<Particle>();
public List<Entity> entities = new List<Entity>();
public List<Collectable> collectables = new List<Collectable>();
public List<Bomb> bombs = new List<Bomb>();
public List<NPC> npcs = new List<NPC>();
public List<Rectangle> islandRects = new List<Rectangle>();
private DungeonGenerator gd;
public Coordinate spawnLocation = Coordinate.Zero;
private int respawnTimer = -20;
private bool sorted = false;
private bool takeshot = false, takeshot1 = false;
private Texture2D minimap;
public Thread mapLoadThread;
private bool doneGen = false;
public World()
{
worldTimer = 0;
}
public void Update()
{
worldTimer++;
if (!mapLoadThread.IsAlive)
{
if (!Main.camera.activeGui.stopsWorldInput)
{
entities = entities.OrderBy(x =>
{
if (x.tType != EntityType.Spawner)
{
Vector2 yRot = Vector2.Transform(x.hitbox.center, Matrix.CreateRotationZ(Main.camera.Rotation));
return yRot.Y;
}
return 0;
}).ToList();
if (player.moving)
{
List<Tile> nearTiles = nearTiles = GetNearTiles(player, 16);
foreach (Tile t in nearTiles)
{
if (!t.revealed)
{
t.revealed = true;
t.OnReveal(this);
}
}
}
if (player != null)
{
Main.camera.clamp = new Rectangle(0, 0, tiles.GetLength(0) * Coordinate.coordSize, tiles.GetLength(1) * Coordinate.coordSize); //new Rectangle(0, 0, 3200, 600);
doneGen = true;
}
if (doneGen)
{
if (Main.keyboard.KeyPressed(Keys.F))
{
//CreateEnemy(new EnemyTest(player.center));
//CreateBomb(new Bomb(null, Color.White, 4, player.center, Vector2.Zero, 128, 1, 0, 0, 0, 4, 128, 2));
//player.GiveXp(90000);
}
if (Main.keyboard.KeyPressed(Keys.G))
takeshot = true;
foreach (Entity entity in entities.ToList())
{
if (entity.tType == EntityType.Player)
entity.Update(this);
else if (entity.tType == EntityType.Projectile)
entity.Update(this);
else if (entity.tType == EntityType.Bag)
entity.Update(this);
else if (entity.tType == EntityType.Spawner)
entity.Update(this);
else if (entity.tType == EntityType.Particle)
entity.Update(this);
else if (entity.tType == EntityType.Collectable)
entity.Update(this);
else if (entity.tType == EntityType.Bomb)
entity.Update(this);
else if (entity.tType == EntityType.NPC)
entity.Update(this);
else if (entity.tType == EntityType.Enemy)
{
Enemy e = (Enemy)entity;
float dist = Vector2.Distance(e.center, player.center);
if (dist < e.activeDistance)
e.active = true;
else e.active = false;
if (e.active)
e.Update(this);
}
#region If Projectile
if (entity.tType == EntityType.Projectile)
{
Projectile p = (Projectile)entity;
if (!p.friendly)
{
if (!player.untargetable)
{
if (player.hitbox.Intersects(p.hitbox))
{
if (!p.hitEntities.Contains(p))
player.TakeDamage((int)p.damage, p, this);
if (!p.piercing)
p.Die(this);
else
p.hitEntities.Add(p);
}
}
}
else
{
foreach (Enemy e in enemies)
{
if (!e.untargetable)
{
if (e.hitbox.Intersects(p.hitbox))
{
if (!p.hitEntities.Contains(e))
e.TakeDamage((int)p.damage, p, this);
if (!p.piercing)
entity.Die(this);
else
p.hitEntities.Add(e);
}
}
}
}
}
if (entity.tType == EntityType.Collectable)
{
Collectable collectable = (Collectable)entity;
if (player.hitbox.Intersects(entity.hitbox))
{
if (collectable.collectable)
collectable.OnPickup(this);
}
}
#endregion
if (entity.tileCollides)
{
List<Tile> nearTiles = GetNearTiles(entity);
foreach (Tile t in nearTiles)
{
if (t.collidable)
{
if (entity.hitbox.Intersects(t.bounds))
{
TileCollidable tC = (TileCollidable)t;
tC.OnEntityCollide(this, entity);
entity.OnTileCollide(this, t);
}
}
}
}
#region deletion
if (entity.dead)
entities.Remove(entity);
if (entity.tType == EntityType.Projectile)
{
for (int i2 = projectiles.Count - 1; i2 >= 0; i2--)
{
if (projectiles[i2].dead)
projectiles.RemoveAt(i2--);
}
}
else if (entity.tType == EntityType.Enemy)
{
for (int i2 = enemies.Count - 1; i2 >= 0; i2--)
{
if (enemies[i2].dead)
enemies.RemoveAt(i2--);
}
}
else if (entity.tType == EntityType.Particle)
{
for (int i2 = particles.Count - 1; i2 >= 0; i2--)
{
if (particles[i2].dead)
particles.RemoveAt(i2--);
}
}
else if (entity.tType == EntityType.Collectable)
{
for (int i2 = collectables.Count - 1; i2 >= 0; i2--)
{
if (collectables[i2].dead)
collectables.RemoveAt(i2--);
}
}
else if (entity.tType == EntityType.Bomb)
{
for (int i2 = bombs.Count - 1; i2 >= 0; i2--)
{
if (bombs[i2].dead)
bombs.RemoveAt(i2--);
}
}
else if (entity.tType == EntityType.NPC)
{
for (int i2 = npcs.Count - 1; i2 >= 0; i2--)
{
if (npcs[i2].dead)
npcs.RemoveAt(i2--);
}
}
}
#endregion
Main.camera.target = player.center;
if (enemies.FindAll(x => x.active).Count < 16)
{
foreach (EntitySpawner spawner in spawners)
{
float distance = (player.position - spawner.position).Length();
if (distance > 1024 && distance <= 1280)
{
if (Main.rand.Next(spawner.rate) == 0)
{
spawner.SpawnEnemies(this);
}
}
}
}
}
}
}
}
List<Vector2> points = new List<Vector2>();
public void Draw(GameCamera camera, SpriteBatch batch)
{
if (doneGen)
{
if (!Main.camera.activeGui.stopsWorldDraw)
{
if (drawTiles.Count == 0)
GetTilesToDraw(true);
else GetTilesToDraw();
foreach (Tile t in drawTiles)
{
if (!t.setCardinals)
t.SetCardinalTiles(this);
t.Draw(batch);
if (t is TileRock)
{
TileRock tC = (TileRock)t;
tC.DrawBeneath(batch);
}
}
foreach (Entity e in entities)
{
e.DrawOutline(batch);
e.Draw(batch);
}
foreach (Tile t in drawTiles)
{
if (t.collidable)
{
TileCollidable tC = (TileCollidable)t;
tC.DrawOutline(batch);
tC.Draw(batch);
}
}
foreach (EntitySpawner spawner in spawners)
spawner.Draw(batch);
Vector2 prev = Vector2.Zero;
foreach (Vector2 p in points)
{
if (prev != Vector2.Zero)
{
DrawGeometry.DrawLine(batch, prev, p, Microsoft.Xna.Framework.Color.White);
}
prev = p;
}
if (takeshot)
{
if (minimap == null)
minimap = new Texture2D(batch.GraphicsDevice, tiles.GetLength(0), tiles.GetLength(1));
Color[] color2 = new Color[tiles.GetLength(0) * tiles.GetLength(1)];
for (int x = 0; x < tiles.GetLength(0); x++)
{
for (int y = 0; y < tiles.GetLength(1); y++)
{
if (tiles[x, y] != null)
color2[x + (y * tiles.GetLength(0))] = tiles[x, y].MinimapColor();
else
color2[x + (y * tiles.GetLength(0))] = Color.Black;
}
}
Coordinate playerPos = player.position.ToCoordinate();
if ((playerPos.x < tiles.GetLength(0) && playerPos.y < tiles.GetLength(1)) && (playerPos.x >= 0 && playerPos.y >= 0))
color2[playerPos.x + (playerPos.y * tiles.GetLength(0))] = Color.Red;
minimap.SetData(color2);
Stream stream = File.OpenWrite("test1.jpg");
minimap.SaveAsPng(stream, tiles.GetLength(0), tiles.GetLength(1));
stream.Close();
takeshot = false;
}
}
}
else
{
//batch.GraphicsDevice.Clear(Color.Black);
//batch.DrawString(Assets.GetFont("munro12"), WorldGenerator.worldGenText, Vector2.Zero, Color.White, 0, Vector2.Zero, 2, SpriteEffects.None, 0);
}
}
#region Tile Getters
public List<Tile> GetTilesAlongLine(Vector2 pointA, Vector2 pointB, float width) //NOTE Width in pixels to test; not tiles
{
List<Tile> tiles = new List<Tile>();
Vector2 distance = pointB - pointA;
distance.Normalize();
List<Coordinate> used = new List<Coordinate>();
for (int i = 0; i < (pointA - pointB).Length(); i++)
{
Coordinate currentCoord = Coordinate.VectorToCoord(pointA + (distance * i));
if (!used.Contains(currentCoord)) //so it doesn't give multiple of the same tile
{
tiles.Add(GetTile(currentCoord));
if (width > 1)
{
Coordinate currentCoordLeft = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance) * width / 2));
Coordinate currentCoordRight = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance, true) * width / 2));
tiles.Add(GetTile(currentCoordLeft));
tiles.Add(GetTile(currentCoordRight));
}
used.Add(currentCoord);
}
}
return tiles;
}
public static List<Coordinate> GetCoordsAlongLine(Tile[,] tiles, Vector2 pointA, Vector2 pointB, float width) //NOTE Width in pixels to test; not tiles
{
List<Coordinate> coords = new List<Coordinate>();
Vector2 distance = pointB - pointA;
distance.Normalize();
List<Coordinate> used = new List<Coordinate>();
for (int i = 0; i < (pointA - pointB).Length(); i++)
{
Coordinate currentCoord = Coordinate.VectorToCoord(pointA + (distance * i));
coords.Add(currentCoord);
if (width > 1)
{
Coordinate currentCoordLeft = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance) * width / 2));
Coordinate currentCoordRight = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance, true) * width / 2));
if (!used.Contains(currentCoordLeft))
{
coords.Add(currentCoordLeft);
used.Add(currentCoordLeft);
}
if (!used.Contains(currentCoordRight))
{
coords.Add(currentCoordRight);
used.Add(currentCoordRight);
}
}
}
return coords;
}
public static List<Tile> GetTilesAlongLine(Tile[,] tiles, Vector2 pointA, Vector2 pointB, float width) //NOTE Width in pixels to test; not tiles
{
List<Tile> tiles2 = new List<Tile>();
Vector2 distance = pointB - pointA;
distance.Normalize();
List<Coordinate> used = new List<Coordinate>();
for (int i = 0; i < (pointA - pointB).Length(); i++)
{
Coordinate currentCoord = Coordinate.VectorToCoord(pointA + (distance * i));
tiles2.Add(GetTile(tiles, currentCoord));
if (width > 1)
{
Coordinate currentCoordLeft = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance) * width / 2));
Coordinate currentCoordRight = Coordinate.VectorToCoord(pointA + (distance * i) + (VectorHelper.GetPerp(distance, true) * width / 2));
if (!used.Contains(currentCoordLeft))
{
tiles2.Add(GetTile(tiles, currentCoordLeft));
used.Add(currentCoordLeft);
}
if (!used.Contains(currentCoordRight))
{
tiles2.Add(GetTile(tiles, currentCoordRight));
used.Add(currentCoordRight);
}
//tiles2.Add(GetTile(tiles, currentCoordLeft));
//tiles2.Add(GetTile(tiles, currentCoordRight));
}
}
return tiles2;
}
public List<Tile> GetTilesToDraw(bool moveOverride = false)
{
List<Tile> tiles = new List<Tile>();
if (player.moving || moveOverride)
{
Coordinate start = Coordinate.VectorToCoord(Main.camera.worldCenter);
for (int x = -15; x < 16; x++)
{
for (int y = -17; y < 16; y++)
{
Tile t = GetTile(new Coordinate(start.x + x, start.y + y));
if (t != null && t.texture != null)
tiles.Add(t);
}
}
drawTiles = tiles;
}
return drawTiles;
}
public List<Tile> GetNearTiles(Entity entity, int distance = 1)
{
List<Tile> tiles = new List<Tile>();
Coordinate start = Coordinate.VectorToCoord(entity.center);
for (int x = -distance; x <= distance; x++)
{
for (int y = -distance; y <= distance; y++)
{
Tile t = GetTile(new Coordinate(start.x + x, start.y + y));
if (t != null)
tiles.Add(t);
}
}
return tiles;
}
public Tile GetTile(Coordinate position)
{
if ((position.x >= 0 && position.y >= 0) && (position.x < tiles.GetLength(0) && position.y < tiles.GetLength(1)))
return tiles[position.x, position.y];
else return null;
}
public static Tile GetTile(Tile[,] tiles, Coordinate position)
{
if ((position.x >= 0 && position.y >= 0) && (position.x < tiles.GetLength(0) && position.y < tiles.GetLength(1)))
return tiles[position.x, position.y];
else return null;
}
#endregion
public void LoadWorld(Player2 player)
{
GuiLoading.DEBUGLOADINGINFO = "Loading save file...";
PlayerSave save = SerializeHelper.LoadSave(@"Saves/save.json");
GuiLoading.DEBUGLOADINGINFO = "Loading world file: " + save.map;
if (!Directory.Exists("Maps"))
Directory.CreateDirectory("Maps");
SerWorld w = SerializeHelper.LoadWorld("Maps/" + save.map);
tiles = new Tile[w.bounds.Width / 48, w.bounds.Height / 48];
for (int i = 0; i < w.serTiles.Count; i++)
{
GuiLoading.DEBUGLOADINGINFO = "Adding tiles " + w.serTiles.Count + " to world...\n" + i + "/" + w.serTiles.Count;
Tile t = w.serTiles[i];
if (t != null)
{
tiles[(int)(t.realPosition.X / 48), (int)(t.realPosition.Y / 48)] = t;
}
}
entities.AddRange(w.spawners);
spawners.AddRange(w.spawners);
this.player = new Player2(player.position);
foreach(EntitySpawner findPlayerSpawner in spawners)
{
if (findPlayerSpawner.type == 0)
player.position = findPlayerSpawner.position;
}
entities.Add(this.player);
}
public void UnloadWorld()
{
entities.Clear();
enemies.Clear();
projectiles.Clear();
//player = null;
}
#region creation
public Thread CreateWorld(Player2 player)
{
tiles = new Tile[256, 256];
return mapLoadThread;
}
public Tile CreateTile(Tile tile)
{
if (tile != null && (tile.position.x >= 0 && tile.position.y >= 0 && tile.position.x < tiles.GetLength(0) && tile.position.y < tiles.GetLength(1)))
{
tiles[tile.position.x, tile.position.y] = tile;
if (tile is TileWall)
wallTiles.Add((TileWall)tile);
}
//else
//Logger.Log("Could not create tile", false);
return tile;
}
public Particle CreateParticle(Particle p)
{
entities.Add(p);
particles.Add(p);
return p;
}
public Enemy CreateEnemy(Enemy e)
{
entities.Add(e);
enemies.Add(e);
return e;
}
public EntitySpawner CreateSpawner(EntitySpawner e)
{
entities.Add(e);
return e;
}
public Projectile CreateProjectile(Projectile p)
{
entities.Add(p);
projectiles.Add(p);
return p;
}
public Collectable CreateCollectable(Collectable c)
{
entities.Add(c);
collectables.Add(c);
return c;
}
public Bomb CreateBomb(Bomb b)
{
entities.Add(b);
bombs.Add(b);
return b;
}
public NPC CreateNPC(NPC n)
{
entities.Add(n);
npcs.Add(n);
return n;
}
#endregion
}
}