-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapClasses.ixx
1080 lines (886 loc) · 32.3 KB
/
MapClasses.ixx
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* __ __
* | \/ | __ _ _ __
* | |\/| |/ _` | '_ \
* | | | | (_| | |_) |
* |_|__|_|\__,_| .__/
* / ___| | __ |_|__ ___ ___ ___
* | | | |/ _` / __/ __|/ _ \/ __|
* | |___| | (_| \__ \__ \ __/\__ \
* \____|_|\__,_|___/___/\___||___/
*
*
* MAP CLASSES.
*
*/
module;
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
export module MapClasses;
import <stdio.h>;
import <string>;
import <iostream>;
import <vector>;
import <cstdlib>;
import <time.h>;
import <unordered_map>;
import FormFactory;
import TypeStorage;
import GameState;
import Resources;
import CharacterClasses;
import LimbFormMasterList;
import UI;
using namespace std;
export enum MapDirection { Up, Down, Left, Right, Total }; /* NOT a CLASS because we want to use it as int. */
export struct AcquiredLimb {
SDL_Texture* texture;
int countdown;
int rotationAngle;
int rotationAngleIncrement;
SDL_Rect diffRect;
string name;
AcquiredLimb(SDL_Texture* texture, int countdown, int rotationAngle, SDL_Rect diffRect, int rotationIncrement, string name) :
texture(texture), countdown(countdown), rotationAngle(rotationAngle),
diffRect(diffRect), rotationAngleIncrement(rotationIncrement), name(name) { }
};
/* This is what a Landmark's checkCollision function will return. */
export struct LandmarkCollisionInfo {
bool hasCollided;
int subject;
LandmarkType type;
/* constructor */
LandmarkCollisionInfo(bool iHasCollided, LandmarkType iType, int iSubject) :
hasCollided(iHasCollided), subject(iSubject), type(iType) {
}
};
/*
* SubPath helps draw paths of floors through the blockmap.
* Create smaller sub-paths so we aren't moving around totally randomly.
*/
export struct SubPath {
int seed; /* The length of the subpath (seeding a new floor). */
MapDirection direction;
int radius; /* The size of the area to be cleared around the path. */
SubPath(int iSeed, MapDirection iDirection, int iRadius) {
direction = iDirection;
seed = iSeed;
radius = iRadius;
}
};
/*
* Character will eventually go in its own module, along with Limb, for use in Character Creation and Battle screens.
* Some of the attributes are specific to the Map Screen. So we will have a Character Decorator in the Map Screen
* which will add the map-related members and functions to the extended object.
* Battle and Character Creation screens will have their own decorators, adding their own stuff.
*/
export class MapCharacter : public Character {
public:
/* Null character with no avatar. */
MapCharacter(CharacterType characterType = CharacterType::None) : Character(characterType) {
texture = NULL;
/* Set default texture. */
newNpc = false;
}
/* There will be no texture. The character will be drawn from their limbs. */
MapCharacter(CharacterType characterType, int x, int y) :
Character(characterType, x, y)
{
texture = NULL;
/* Set default texture. */
UI& ui = UI::getInstance();
SDL_Surface* characterSurface = IMG_Load("assets/player_character.png");
SDL_Texture* characterTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), characterSurface);
SDL_FreeSurface(characterSurface);
setTexture(characterTexture);
newNpc = false;
}
/* Hostile NPC when loaded from DB. */
MapCharacter(int id, string name, int anchorLimbId, Point position, vector<Limb> limbs) :
Character(id, name, anchorLimbId, position, limbs), homePosition(position)
{
texture = NULL;
/* Set default texture. */
UI& ui = UI::getInstance();
SDL_Surface* characterSurface = IMG_Load("assets/player_character.png");
SDL_Texture* characterTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), characterSurface);
SDL_FreeSurface(characterSurface);
setTexture(characterTexture);
newNpc = false;
}
~MapCharacter() {} /* destroy texture. */
Point getHomePosition() { return homePosition; }
vector<AcquiredLimb>& getAcquiredLimbStructs() { return acquiredLimbStructs; }
bool isNewNpc() { return newNpc; }
int getNewNpcCountup() { return newNpcCountup; }
void clearAcquiredLimbStructs() { acquiredLimbStructs = {}; }
void setHomePosition(Point position) { homePosition = position; }
/* We count UP so we can multiply the count to get a rotation.
* When an NPC is created we want to spin their avatar as an animation.
*/
void startNewNpcCountup() {
newNpcCountup = 0;
newNpc = true;
}
/* Get the countup and increment it in the same function so I can do both in a ternary statement. */
int tickNewNpcCountup() {
if (newNpcCountup >= 90) {
newNpc = false;
}
else {
++newNpcCountup;
}
return newNpcCountup;
}
bool move(MapDirection direction, int distance = 1) {
bool moved = false;
/* This will become more complicated when we do animations. */
/* Checking for obstacles must be done by MapScreen object.
* When this is called, we follow blindly. */
switch (direction) {
case MapDirection::Up:
updateLastBlock();
blockPosition.y -= distance;
moved = true;
break;
case MapDirection::Down:
updateLastBlock();
blockPosition.y += distance;
moved = true;
break;
case MapDirection::Left:
updateLastBlock();
blockPosition.x -= distance;
moved = true;
break;
case MapDirection::Right:
updateLastBlock();
blockPosition.x += distance;
moved = true;
break;
}
// cout << blockPosition.x << ", " << blockPosition.y << "\n";
return moved;
}
private:
Point homePosition;
vector<AcquiredLimb> acquiredLimbStructs;
bool newNpc;
int newNpcCountup;
};
/*
* Entrance, Exit, and Shrines are landmarks.
* In sequels this will include buildings (portals to maps with friendly NPCs) and portals to dungeons (maps with more unfriendly NPCs).
*/
export class Landmark {
public:
/* constructor (primarily for entrance and exit.) */
Landmark(
Point position,
SDL_Texture* texture,
LandmarkType landmarkType,
int characterId,
SuitType suitType = SuitType::NoSuit
) :
texture(texture),
landmarkType(landmarkType),
characterId(characterId),
position(position),
suitType(suitType)
{ }
/* destructor */
~Landmark() { /* Texture is managed by MapScreen and will be destroyed at the end of the run() function. */ }
int getDrawX() { return position.x; }
int getDrawY() { return position.y; }
int getCharacterId() { return characterId; }
int getId() { return id; }
Point getPosition() { return position; }
LandmarkType getType() { return landmarkType; }
void setId(int id) { this->id = id; }
void setCharacterId(int characterId) { this->characterId = characterId; }
void setPosition(Point position) { this->position = position; }
void clearAcquiredLimbStructs() { acquiredLimbStructs = {}; }
SDL_Texture* getTexture() { return texture; }
SuitType getSuitType() { return suitType; }
vector<AcquiredLimb>& getAcquiredLimbStructs() { return acquiredLimbStructs; }
LandmarkCollisionInfo checkCollision(Point pos) { return checkCollision(pos.x, pos.y); }
LandmarkCollisionInfo checkCollision(int x, int y) {
if (x == position.x && y == position.y) {
return { true, landmarkType, characterId };
}
return { false, landmarkType, -1 }; }
private:
/* Point refers to the block grid, not the pixels */
int id;
string slug;
Point position;
SDL_Texture* texture;
LandmarkType landmarkType;
int characterId; /* This can be either the MAP id or the SUIT slug??? Needs re-thinking! */
SuitType suitType;
vector<AcquiredLimb> acquiredLimbStructs;
};
export Landmark getExitLandmark(Point position) {
UI& ui = UI::getInstance();
SDL_Surface* gateSurface = IMG_Load("assets/ENTRANCE.png");
SDL_Texture* gateTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), gateSurface);
SDL_FreeSurface(gateSurface);
return Landmark(position, gateTexture, LandmarkType::Exit, -1);
}
export Landmark getEntranceLandmark(Point position) {
UI& ui = UI::getInstance();
SDL_Surface* gateSurface = IMG_Load("assets/ENTRANCE.png");
SDL_Texture* gateTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), gateSurface);
SDL_FreeSurface(gateSurface);
return Landmark(position, gateTexture, LandmarkType::Entrance, -1);
}
export class Block {
public:
/* constructor */
Block(bool isFloor = true)
: isFloor(isFloor), floorTextureIndex(0), isPath(false), pathFlipOption(0),
pathRotateAngle(0), wallTextureIndex(0), pathTextureIndex(0), isLandmarkArea(false){
loaded = false;
}
/* Rebuilding block from DB. */
Block(int id, bool isFloor, bool isPath, bool isLandmarkArea) :
id(id), isFloor(isFloor), isPath(isPath), isLandmarkArea(isLandmarkArea)
{
floorTextureIndex = 0;
pathFlipOption = 0;
wallTextureIndex = 0;
pathTextureIndex = 0;
pathRotateAngle = 0;
loaded = true;
}
/* getters */
bool getIsFloor() { return isFloor; }
bool getIsLoaded() { return loaded; }
int getFloorTextureIndex() { return floorTextureIndex; }
int getWallTextureIndex() { return wallTextureIndex; }
bool getWallIsFlipped() { return wallIsFlipped; }
bool getIsPath() { return isPath; }
bool getIsLandmarkArea() { return isLandmarkArea; }
int getPathFlipOption() { return pathFlipOption; }
int getPathRotateAngle() { return pathRotateAngle; }
int getPathTextureIndex() { return pathTextureIndex; }
int getId() { return id; }
/* setters */
void setPathRotateAngle(int angle = 0) { pathRotateAngle = angle; }
void setPathFlipOption(int option = 0) { pathFlipOption = option; }
void setIsFloor(bool incomingIsFloor) { isFloor = incomingIsFloor; }
void setIsPath(bool incomingIsPath = true) { isPath = incomingIsPath; }
void setWallTextureIndex(int index) { wallTextureIndex = index; }
void setWallIsFlipped(bool flipWall) { wallIsFlipped = flipWall; }
void setPathTextureIndex(int index) { pathTextureIndex = index; }
void setId(int id) { this->id = id; }
void setFloorTextureIndex(int index) { floorTextureIndex = index; }
void setIsLandmarkArea(bool isLandmarkArea) {this->isLandmarkArea = isLandmarkArea; }
private:
bool isFloor;
bool isPath;
bool isLandmarkArea;
int pathFlipOption;
int pathRotateAngle;
int floorTextureIndex;
int wallTextureIndex;
int pathTextureIndex;
bool wallIsFlipped;
int id;
bool loaded = false;
};
/* The Map object contains all the blocks from the DB. */
export class Map {
public:
/* constructors */
Map() {};
Map(MapForm mapForm);
Map(MapForm mapForm, vector<Limb> roamingLimbs, vector<vector<Block>> rows, Point characterPosition, vector<MapCharacter> hostileNpcs);
vector<vector<Block>>& getRows() { return rows; }
vector<Landmark>& getLandmarks() { return landmarks; }
MapCharacter& getPlayerCharacter() { return playerCharacter; }
SDL_Texture* getFloorTexture(int index) { return mapForm.floorTextures[index]; }
SDL_Texture* getWallTexture(int index) { return mapForm.wallTextures[index]; }
SDL_Texture* getPathTexture(int index) { return mapForm.pathTextures[index]; }
vector<Limb>& getRoamingLimbs() { return roamingLimbs; }
string getName() { return mapForm.name; }
string getSlug() { return mapForm.slug; }
MapLevel getMapLevel() { return mapForm.mapLevel; }
vector<MapCharacter>& getNPCs() { return NPCs; }
vector<Character>& getSuits() { return mapForm.suits; }
void setLandmarks(vector<Landmark> landmarks) { this->landmarks = landmarks; }
void setPlayerCharacter(MapCharacter playerCharacter) { this->playerCharacter = playerCharacter; }
void addNPC(MapCharacter npc) { NPCs.push_back(npc); }
MapForm& getForm() { return mapForm; }
void randomizePathOptions(Block& block);
private:
MapForm mapForm;
vector<vector<Block>> rows;
void floorize(int x, int y, int radius, vector<Point>& floorPositions);
vector<Point> buildMap();
void spaceOutLandmarks();
void removeRoamingLimbsFromLandmarks();
vector<MapCharacter> NPCs;
MapCharacter playerCharacter;
/* stuff sent in from MapData struct */
vector<Landmark> landmarks;
vector<LimbForm> nativeLimbForms;
vector<Limb> roamingLimbs;
};
/*
*
*
*
*
*
*
*
* Map Functions
*
*
*
*
*
*
*
*/
/*
* Map class constructor for brand new map.
* For the first time loading the map, which then gets saved to the database.
*/
Map::Map(MapForm mapForm) : mapForm(mapForm) {
UI& ui = UI::getInstance();
/*
* On the first draw, we must build the GRID based on the number of SUITS (therefore shrines) and landmarks.
* But we must scatter the LIMBS across the available FLOOR tiles, which are only known after creating the grid.
* So SUITS must be calculated before buildMap, and Limbs must be created and distributed AFTER buildMap.
*
* After incorporating the database, we will need to differentiate between FIRST TIME (create map) vs. rebuilding
* from the DB.
*/
/* Create SHRINE landmarks. One for each Suit in the MapForm.
* They don't have IDs yet, but we will pass these into the database function
* to save the map and its members, and then we'll populate the IDs.
* The suit type acts as a placeholder for the character ID for now.
* The map building function will also add the Point location.
*/
for (Character& suit : mapForm.suits) {
SDL_Surface* shrineSurface = IMG_Load("assets/shrine.png");
SDL_Texture* shrineTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), shrineSurface);
SDL_FreeSurface(shrineSurface);
/* create a landmark */
Point shrinePoint = Point(0, 0);
landmarks.emplace_back(
shrinePoint,
shrineTexture,
LandmarkType::Shrine,
suit.getId(),
suit.getSuitType()
);
}
/* Build the actual grid for the first time. Receive a list of floor coordinates. */
vector<Point> floorPositions = buildMap();
/* populate characters and limbs after building the map(and its landmarks). */
nativeLimbForms = getMapLimbs(mapForm.mapLevel);
/* Populate the map with RoamingLimbs. */
for (LimbForm& limbForm : nativeLimbForms) {
int numberOfThisLimb = 4;
for (int n = 0; n < numberOfThisLimb; ++n) {
Limb& newLimb = roamingLimbs.emplace_back(limbForm);
Point newPosition = floorPositions[rand() % floorPositions.size()];
newLimb.setPosition(newPosition);
newLimb.setLastPosition(newPosition);
}
}
}
/* Map class constructor to rebuild map from DB data. */
Map::Map(MapForm mapForm, vector<Limb> roamingLimbs, vector<vector<Block>> rows, Point characterPosition, vector<MapCharacter> hostileNpcs) :
mapForm(mapForm), roamingLimbs(roamingLimbs), rows(rows), NPCs(hostileNpcs) {
/* populate characters and limbs after building the map(and its landmarks). */
nativeLimbForms = getMapLimbs(mapForm.mapLevel);
UI& ui = UI::getInstance();
/* create Player Character */
/* get character texture NO LONGER NECESSARY...
* DELETE (since this is done in the constructor.... and must ALWAYS be done in constructor...
* texture must never be NULL
*/
SDL_Surface* characterSurface = IMG_Load("assets/player_character.png");
SDL_Texture* characterTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), characterSurface);
SDL_FreeSurface(characterSurface);
/*
* PROBLEM
*
* How can I load from the database when the database imports this module?
*
* SOLUTION:
* Map constructor must take pre-built Character object in constructor.
* The Screen will create the character object (or maybe the Database will).
*
*/
playerCharacter = MapCharacter(CharacterType::Player, characterPosition.x, characterPosition.y);
playerCharacter.setTexture(characterTexture); /* Placeholder. */
/* Set wall and floor texture indexes. */
for (int i = 0; i < this->rows.size(); ++i) {
vector<Block>& blocks = this->rows[i];
for (int k = 0; k < blocks.size(); ++k) {
Block& thisBlock = blocks[k];
thisBlock.setFloorTextureIndex(rand() % mapForm.floorTextures.size());
/* set texture values for Wall blocks (defaults if they're not wall blocks). */
if (thisBlock.getIsFloor()) {
thisBlock.setWallTextureIndex(rand() % mapForm.floorTextures.size());
thisBlock.setWallIsFlipped(rand() % 2 == 0);
if (thisBlock.getIsPath()) {
randomizePathOptions(thisBlock);
}
}
else {
thisBlock.setWallTextureIndex(rand() % mapForm.wallTextures.size());
thisBlock.setWallIsFlipped(rand() % 2 == 0);
}
}
}
}
/* For each NPC, if they're on a landmark, */
void Map::removeRoamingLimbsFromLandmarks() {
}
/* For each landmark, compare its position with each other landmark. */
void Map::spaceOutLandmarks() {
int minimumBlocksDistance = 6;
bool landmarksAreSpacedOut = true;
for (int i = 0; i < landmarks.size() - 1; ++i) {
Landmark& thisLandmark = landmarks[i];
/* Skip entrance and exit. */
if (thisLandmark.getType() == LandmarkType::Exit || thisLandmark.getType() == LandmarkType::Entrance) {
continue;
}
Point thisPosition = thisLandmark.getPosition();
for (int k = 0; k < landmarks.size(); ++k) {
if (k == i) { continue; }
Landmark& thatLandmark = landmarks[k];
Point thatPosition = thatLandmark.getPosition();
/* get the x and y distances. */
int xDistance = thisPosition.x - thatPosition.x;
int yDistance = thisPosition.y - thatPosition.y;
if (xDistance < 0) {
xDistance *= -1;
}
if (yDistance < 0) {
yDistance *= -1;
}
if (xDistance < minimumBlocksDistance && yDistance < minimumBlocksDistance) {
landmarksAreSpacedOut = false;
/* Give thisPoint new coordinates. */
int randX = (rand() % (mapForm.blocksWidth - 10)) + 5;
int randY = (rand() % (mapForm.blocksHeight - 10)) + 5;
thisLandmark.setPosition(Point(randX, randY));
break;
}
}
if (!landmarksAreSpacedOut) { break; }
}
/* Recursively run this function with updated coordinates. */
if (!landmarksAreSpacedOut) { spaceOutLandmarks(); }
}
/*
* Build the actual grid of Block objects.
* Returns a vector of Points which are the coordinates for all Floor objects.
* The Floor Points are used to populate the map with Roaming Limbs.
*
* This is used the first time a particular Map is loaded.
* It's based on Landmarks.
* We scatter Entrance, Exit, and Shrines.
* We draw a path from Entrance, to each Shrine (one by one),
* and finally to the Exit.
* We also draw random paths to make it a maze.
*/
vector<Point> Map::buildMap() {
UI& ui = UI::getInstance();
/* Create a vector of rows (of blocks) of the specified size. */
rows = vector<vector<Block>>(mapForm.blocksHeight);
for (int i = 0; i < rows.size(); ++i) {
/* Create a vector of blocks of the specified size. All are walls. */
vector<Block> blocks(mapForm.blocksWidth);
for (int k = 0; k < blocks.size(); ++k) {
blocks[k] = Block(false);
}
rows[i] = blocks;
}
/* Now make the PATH */
/* get a random x starting point, but the y will be map's height - 2 */
int pathX = (rand() % (mapForm.blocksWidth - 10)) + 5;
int pathY = static_cast<int>(rows.size()) - 5;
int endBlockX = (rand() % (mapForm.blocksWidth - 10)) + 5;
int playerX = pathX;
int playerY = pathY;
Block& startingBlock = rows[pathY][pathX];
Block& endingBlock = rows[1][endBlockX];
startingBlock.setIsFloor(true);
endingBlock.setIsFloor(true);
/* make the first subpath (to lead us up from the entrance). */
SubPath subPath = SubPath(
(rand() % 5) + 2, /* seed (length) */
MapDirection::Up,
(rand() % 3) + 2); /* radius. */
/* Entrance & Exit landmarks. */
landmarks.emplace_back(getExitLandmark(Point(endBlockX, 3)));
landmarks.emplace_back(getEntranceLandmark(Point(pathX, pathY)));
/*
* Give the Shrines each a location.
* Fill out SubPaths designed to work their way to each shrine, and then the exit.
* How?
* Create a vector of points, with the landmarks's locations excluding the Entrance.
* Make sure the Exit is the first one.
* The while loop runs while there are still locations to reach.
* Keep drawing subPaths in the direction of the top (last) location.
* When we reach that location, delete it from the vector and restart the subPath.
*/
vector<Point> pointsToReach;
/* First add the exit as the final one to find. */
for (Landmark& landmark : landmarks) {
if (landmark.getType() == LandmarkType::Exit) {
pointsToReach.push_back(landmark.getPosition());
break;
}
}
/* Now give the shrines positions, and add those positions to pointsToReach. */
for (Landmark& landmark : landmarks) {
if (landmark.getType() == LandmarkType::Shrine) {
int randX = (rand() % (mapForm.blocksWidth - 10)) + 5;
int randY = (rand() % (mapForm.blocksHeight - 10)) + 5;
landmark.setPosition(Point(randX, randY));
}
}
/* Make sure the shrines are far enough apart with a recursive function. */
spaceOutLandmarks();
/* Now add the spaced out Shrine positions to the pointsToReach list. */
for (Landmark& landmark : landmarks) {
if (landmark.getType() == LandmarkType::Shrine) {
pointsToReach.push_back(landmark.getPosition());
}
}
vector<Point> floorPositions;
/* while loop makes the path.
* Keep drawing subPaths toward each pointsToReach Point until we reach (and delete) them all.
*/
while (pointsToReach.size() > 0) {
Point currentPoint = pointsToReach[pointsToReach.size() - 1];
Point pointToReach = Point(currentPoint.x, currentPoint.y);
/* refresh seed if needed. */
if (subPath.seed < 1) {
/*
* Most times the direction should be toward the pointToReach.
* and most of those should be in the most distant direction.
*
* Find out which directions are TOWARD the pointToReach,
* and from them, which is MOST distant.
*/
bool nextPointIsAbove = pathY > pointToReach.y;
bool nextPointIsLeft = pathX > pointToReach.x;
int horizontalDistance = nextPointIsLeft ? pathX - pointToReach.x : pointToReach.x - pathX;
int verticalDistance = nextPointIsAbove ? pathY - pointToReach.y : pointToReach.y - pathY;
vector<MapDirection> directionsLottery;
int mostNeededDirectionCount = 42;
int lessNeededDirectionCount = 3;
int wrongDirectionsEach = 1;
MapDirection favoriteDirection = MapDirection::Up;
MapDirection secondFavoriteDirection = MapDirection::Right;
MapDirection wrongDirection1 = MapDirection::Down;
MapDirection wrongDirection2 = MapDirection::Left;
if (verticalDistance > horizontalDistance) {
/* Add extra vertical options. */
if (nextPointIsAbove) {
favoriteDirection = MapDirection::Up;
wrongDirection1 = MapDirection::Down;
}
else {
favoriteDirection = MapDirection::Down;
wrongDirection1 = MapDirection::Up;
}
if (nextPointIsLeft) {
secondFavoriteDirection = MapDirection::Left;
wrongDirection2 = MapDirection::Right;
}
else {
secondFavoriteDirection = MapDirection::Right;
wrongDirection2 = MapDirection::Left;
}
}
else {
/* Add extra horizontal options. */
if (nextPointIsLeft) {
favoriteDirection = MapDirection::Left;
wrongDirection1 = MapDirection::Right;
}
else {
favoriteDirection = MapDirection::Right;
wrongDirection1 = MapDirection::Left;
}
if (nextPointIsAbove) {
secondFavoriteDirection = MapDirection::Up;
wrongDirection2 = MapDirection::Down;
}
else {
secondFavoriteDirection = MapDirection::Down;
wrongDirection2 = MapDirection::Up;
}
}
for (int i = 0; i < mostNeededDirectionCount; ++i) {
directionsLottery.push_back(favoriteDirection); }
for (int i = 0; i < lessNeededDirectionCount; ++i) {
directionsLottery.push_back(secondFavoriteDirection); }
for (int i = 0; i < wrongDirectionsEach; ++i) {
directionsLottery.push_back(wrongDirection1); }
for (int i = 0; i < wrongDirectionsEach; ++i) {
directionsLottery.push_back(wrongDirection2); }
int directionLotteryIndex = rand() % directionsLottery.size();
subPath.direction = directionsLottery[directionLotteryIndex];
subPath.seed = (rand() % 12) + 1;
subPath.radius = rand() % 4;
}
/* SubPaths get created at the top of the loop when the old one dies. Then blindly follow the one that exists. */
/* choose the next block to floorize */
switch (subPath.direction) {
case MapDirection::Up:
if (pathY > 1) { /* Do not hit the ceiling. */
--pathY;
}
else {
++pathY;
subPath.seed = 0;
}
break;
case MapDirection::Down:
if (pathY < rows.size() - 3) { /* Do not hit the bottom. */
++pathY;
}
else {
--pathY;
subPath.seed = 0;
}
break;
case MapDirection::Left:
if (pathX > 3) { /* Do not hit the left wall. */
--pathX;
}
else {
++pathX;
subPath.seed = 0;
}
break;
case MapDirection::Right:
if (pathX < rows[pathY].size() - 3) { /* Do not hit the right wall. */
++pathX;
}
else {
--pathX;
subPath.seed = 0;
}
break;
}
floorize(pathX, pathY, subPath.radius, floorPositions);
floorPositions.push_back(Point(pathX, pathY));
--subPath.seed;
if (pointToReach.x == pathX && pointToReach.y == pathY) {
/*
* Path has reached the current (top) pointToReach.
* Delete the pointToReach.
* Next iteration will seek the next (new top) pointToReach.
*/
pointsToReach.erase(pointsToReach.begin() + pointsToReach.size() - 1);
subPath.seed = 0;
}
}
/* Set wall and floor texture indexes. */
for (int i = 0; i < rows.size(); ++i) {
vector<Block>& blocks = rows[i];
for (int k = 0; k < blocks.size(); ++k) {
Block& thisBlock = blocks[k];
thisBlock.setFloorTextureIndex(rand() % mapForm.floorTextures.size());
/* set texture values for Wall blocks (defaults if they're not wall blocks). */
if (thisBlock.getIsFloor()) {
thisBlock.setWallTextureIndex(0);
thisBlock.setWallIsFlipped(false);
}
else {
thisBlock.setWallTextureIndex(rand() % mapForm.wallTextures.size());
thisBlock.setWallIsFlipped(rand() % 2 == 0);
}
}
}
/* clear the area around each shrine. */
for (Landmark& landmark : landmarks) {
Point lPoint = landmark.getPosition();
rows[lPoint.y][lPoint.x].setIsPath(false);
rows[lPoint.y][lPoint.x].setIsFloor(true);
rows[lPoint.y][lPoint.x].setIsLandmarkArea(true);
rows[lPoint.y][lPoint.x - 1].setIsFloor(true);
rows[lPoint.y][lPoint.x - 1].setIsPath(false);
rows[lPoint.y][lPoint.x - 1].setIsLandmarkArea(true);
rows[lPoint.y][lPoint.x + 1].setIsFloor(true);
rows[lPoint.y][lPoint.x + 1].setIsPath(false);
rows[lPoint.y][lPoint.x + 1].setIsLandmarkArea(true);
rows[lPoint.y - 1][lPoint.x - 1].setIsFloor(true);
rows[lPoint.y - 1][lPoint.x - 1].setIsPath(false);
rows[lPoint.y - 1][lPoint.x - 1].setIsLandmarkArea(true);
rows[lPoint.y - 1][lPoint.x].setIsFloor(true);
rows[lPoint.y - 1][lPoint.x].setIsPath(false);
rows[lPoint.y - 1][lPoint.x].setIsLandmarkArea(true);
rows[lPoint.y - 1][lPoint.x + 1].setIsFloor(true);
rows[lPoint.y - 1][lPoint.x + 1].setIsPath(false);
rows[lPoint.y - 1][lPoint.x + 1].setIsLandmarkArea(true);
rows[lPoint.y + 1][lPoint.x - 1].setIsFloor(true);
rows[lPoint.y + 1][lPoint.x - 1].setIsPath(false);
rows[lPoint.y + 1][lPoint.x - 1].setIsLandmarkArea(true);
rows[lPoint.y + 1][lPoint.x].setIsFloor(true);
rows[lPoint.y + 1][lPoint.x].setIsPath(false);
rows[lPoint.y + 1][lPoint.x].setIsLandmarkArea(true);
rows[lPoint.y + 1][lPoint.x + 1].setIsFloor(true);
rows[lPoint.y + 1][lPoint.x + 1].setIsPath(false);
rows[lPoint.y + 1][lPoint.x + 1].setIsLandmarkArea(true);
/* Surround the cleared area with a path. */
rows[lPoint.y][lPoint.x - 2].setIsFloor(true);
rows[lPoint.y][lPoint.x - 2].setIsPath(true);
rows[lPoint.y][lPoint.x + 2].setIsFloor(true);
rows[lPoint.y][lPoint.x + 2].setIsPath(true);
rows[lPoint.y - 1][lPoint.x - 2].setIsFloor(true);
rows[lPoint.y - 1][lPoint.x - 2].setIsPath(true);
rows[lPoint.y - 1][lPoint.x + 2].setIsFloor(true);
rows[lPoint.y - 1][lPoint.x + 2].setIsPath(true);
rows[lPoint.y + 1][lPoint.x - 2].setIsFloor(true);
rows[lPoint.y + 1][lPoint.x - 2].setIsPath(true);
rows[lPoint.y + 1][lPoint.x + 2].setIsFloor(true);
rows[lPoint.y + 1][lPoint.x + 2].setIsPath(true);
rows[lPoint.y - 2][lPoint.x - 2].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x - 2].setIsPath(true);
rows[lPoint.y - 2][lPoint.x + 2].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x + 2].setIsPath(true);
rows[lPoint.y + 2][lPoint.x - 2].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x - 2].setIsPath(true);
rows[lPoint.y + 2][lPoint.x + 2].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x + 2].setIsPath(true);
rows[lPoint.y + 2][lPoint.x - 1].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x - 1].setIsPath(true);
rows[lPoint.y + 2][lPoint.x + 1].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x + 1].setIsPath(true);
rows[lPoint.y - 2][lPoint.x - 1].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x - 1].setIsPath(true);
rows[lPoint.y - 2][lPoint.x + 1].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x + 1].setIsPath(true);
rows[lPoint.y + 2][lPoint.x].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x].setIsPath(true);
rows[lPoint.y + 2][lPoint.x].setIsFloor(true);
rows[lPoint.y + 2][lPoint.x].setIsPath(true);
rows[lPoint.y - 2][lPoint.x].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x].setIsPath(true);
rows[lPoint.y - 2][lPoint.x].setIsFloor(true);
rows[lPoint.y - 2][lPoint.x].setIsPath(true);
}
/* create Player Character */
/* get character texture (didn't we already do this?) */
SDL_Surface* characterSurface = IMG_Load("assets/player_character.png");
SDL_Texture* characterTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), characterSurface);
SDL_FreeSurface(characterSurface);
playerCharacter = MapCharacter(CharacterType::Player, playerX, playerY);
playerCharacter.setTexture(characterTexture);
vector<Point> removedPoints;
/* Remove landmark areas from Floor Positions. */
for (int i = floorPositions.size() - 1; i >= 0; --i) {
Point floorPosition = floorPositions[i];
Block& floorBlock = rows[floorPosition.y][floorPosition.x];
if (floorBlock.getIsLandmarkArea()) {
removedPoints.push_back(floorPosition);
floorPositions.erase(floorPositions.begin() + i);
}
}
return floorPositions;
}
void Map::randomizePathOptions(Block& block) {
/* Set up PATH BLOCK info. The path can be flipped vertical, horizontal, or not at all. */
block.setPathTextureIndex(rand() % mapForm.pathTextures.size());
block.setPathFlipOption(rand() % 3);
/* The path can also be rotated 90 degrees, 270 degrees, or not at all. */
int pathRotateOption = rand() % 3;
block.setPathRotateAngle(pathRotateOption == 2 ? 270 : pathRotateOption == 1 ? 90 : 0);
}
/*
* When we create a path we want to clear a radius around each block of the central path.
* There's the opportunity here to draw an actual "path" block (different than a floor block... maybe non-diggable?).
* But only certain paths are *actual* paths... many are just normal floor blocks.
*/
void Map::floorize(int x, int y, int radius, vector<Point>& floorPositions) {
/* Incoming coordinates are always a path */
Block& thisBlock = rows[y][x];
if (thisBlock.getIsPath()) { return; }
randomizePathOptions(thisBlock);
thisBlock.setIsPath(true);
/* If the block was already a floor, don't bother clearing the surrounding blocks. */
if (thisBlock.getIsFloor()) { return; }
thisBlock.setIsFloor(true);
if (y < (radius + 2)) { return; } /* don't clear top blocks (except exit block, which is already cleared) */
/* increment counters (to help reach radius) */