-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameClasses.h
1525 lines (1232 loc) · 34.9 KB
/
gameClasses.h
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
#pragma once
#include "GamesEngineeringBase.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include "datastructs.h"
using namespace std;
class Vec2 {
public:
float x, y;
Vec2() {
x = y = 0;
}
Vec2(float _x) {
x = y = _x;
}
Vec2(float _x, float _y) {
x = _x;
y = _y;
}
Vec2 operator+(const Vec2 v) { //Elementwise addition
Vec2 newVec;
newVec.x = x + v.x;
newVec.y = y + v.y;
return newVec;
}
Vec2 operator-(const Vec2 v) { //Elementwise subtraction
Vec2 newVec;
newVec.x = x - v.x;
newVec.y = y - v.y;
return newVec;
}
Vec2 operator*(float k) { //Multiply each element by constant k
Vec2 newVec;
newVec.x = x * k;
newVec.y = y * k;
return newVec;
}
Vec2 operator*(const Vec2 v) { //Elementwise multiplication
Vec2 newVec;
newVec.x = x * v.x;
newVec.y = y * v.y;
return newVec;
}
Vec2 operator/(float k) { //Elementwise division by constant k
Vec2 newVec;
newVec.x = x / k;
newVec.y = y / k;
return newVec;
}
void setPos(float _x, float _y) {
x = _x;
y = _y;
}
float length() {
return sqrtf(x * x + y * y);
}
Vec2 normalize() { //Normalize vector to unit length
Vec2 newVec;
newVec.x = x / length();
newVec.y = y / length();
return newVec;
}
void display() {
std::cout << "X: " << x << " Y: " << y << endl;
}
};
class camera {
public:
int xPos, yPos, xDim, yDim;
camera() {
xPos = yPos = 0;
xDim = 1024;
yDim = 768;
}
camera(GamesEngineeringBase::Window& canvas, float _xPos, float _yPos) {
xDim = (int)canvas.getWidth();
yDim = (int)canvas.getHeight();
xPos = (int)(_xPos + 0.5f) - xDim / 2;
yPos = (int)(_yPos + 0.5f) - yDim / 2;
}
camera(float _xPos, float _yPos, int _xDim, int _yDim) {
xDim = _xDim;
yDim = _yDim;
xPos = (int)(_xPos + 0.5f) - xDim / 2;
yPos = (int)(_yPos + 0.5f) - yDim / 2;
}
void loadFromFile(string& fileLine) {
string val;
stringstream ss(fileLine);
ss >> val;
xDim = stoi(val);
ss >> val;
yDim = stoi(val);
ss.clear();
xPos = yPos = 0;
}
void save(ofstream& file) {
file << xDim << " " << yDim << endl;
}
void focus(int worldSizeX, int worldSizeY, float _xPos, float _yPos) { //Focus camera on coordinates (for finite map)
xPos = (int)(_xPos + 0.5f) - xDim / 2; //Round position coordinates to integers
yPos = (int)(_yPos + 0.5f) - yDim / 2; //And set as center of viewbox
if (xPos < 0) { //Limit viewbox to stop at map borders
xPos = 0;
} else if (xPos + xDim >= worldSizeX) {
xPos = worldSizeX - xDim;
}
if (yPos < 0) {
yPos = 0;
} else if (yPos + yDim >= worldSizeY) {
yPos = worldSizeY - yDim;
}
}
void focus(float _xPos, float _yPos) { //Focus camera on coordinates (for infinite map)
xPos = (int)(_xPos + 0.5f) - xDim / 2; //Round position coordinates to integers
yPos = (int)(_yPos + 0.5f) - yDim / 2; //And set as center of viewbox
}
};
class tileMap {
public:
int xDim, yDim, tileDimX, tileDimY;
int xTally, yTally;
bool isInf;
loopingTiles loop; //Data structure for map (2-D wrapping quadruply linked list?)
GamesEngineeringBase::Image tilePack; //Image with all map tiles
tileMap() {
tilePack.load("Resources/tiles.png");
xDim = yDim = 1500;
tileDimX = tileDimY = 1500 / 32;
xTally = yTally = 0;
isInf = false;
}
tileMap(GamesEngineeringBase::Window& canvas) {
tilePack.load("Resources/tiles.png");
xDim = canvas.getWidth();
yDim = canvas.getHeight();
tileDimX = xDim / 32;
tileDimY = yDim / 32;
xTally = yTally = 0;
isInf = false;
}
tileMap(int _tileDimX, int _tileDimY) {
tilePack.load("Resources/tiles.png");
tileDimX = _tileDimX;
tileDimY = _tileDimY;
xDim = tileDimX * 32;
yDim = tileDimY * 32;
xTally = yTally = 0;
isInf = false;
}
tileMap(int _tileDimX, int _tileDimY, bool _isInf) {
tilePack.load("Resources/tiles.png");
loop.generate("Resources/mapnospace.txt");
tileDimX = _tileDimX;
tileDimY = _tileDimY;
xDim = tileDimX * 32;
yDim = tileDimY * 32;
xTally = yTally = 0;
isInf = _isInf;
}
void loadFromFile(string& fileLine) {
tilePack.load("Resources/tiles.png");
loop.generate("Resources/mapnospace.txt");
string val;
stringstream ss(fileLine);
ss >> val;
isInf = stoi(val);
ss >> val;
xDim = stoi(val);
ss >> val;
yDim = stoi(val);
xTally = stoi(val);
ss >> val;
yTally = stoi(val);
ss.clear();
tileDimX = xDim / 32;
tileDimY = yDim / 32;
}
void save(ofstream& file) {
file << (int)isInf << " " << xDim << " " << yDim << " " << xTally << " " << yTally << endl;
}
void load(string filePath) { //Load file of map into the data structure for the map
loop.generate(filePath);
}
void setStructStart(camera& cam) { //Set start pointer so that it keeps original node start points to at origin ((0, 0) on plane)
if (cam.xPos > 0) { //If greater than zero, shift right by distance in tiles
for (int i = 0; i < cam.xPos / 32; i++) {
loop.start = loop.start->right;
}
} else {
for (int i = 0; i > cam.xPos / 32; i--) { //Otherwise shift left
loop.start = loop.start->left;
}
}
if (cam.yPos > 0) {
for (int i = 0; i < cam.yPos / 32; i++) { //Shift down
loop.start = loop.start->down;
}
} else {
for (int i = 0; i > cam.yPos / 32; i--) { //Otherwise shift up
loop.start = loop.start->up;
}
}
}
void updateStructStart(camera& cam) { //Move the start pointer to match the camera's positon
xTally += cam.xPos - loop.prevPosX;
yTally += cam.yPos - loop.prevPosY;
if (xTally > 32) { //When camera moved over 32 pixels, shift right to new tile
loop.start = loop.start->right;
xTally -= 32; //Maintain tally count of new position
} else if (xTally < 1) { //Similarly, shift left when pixel tally is 0 or less
loop.start = loop.start->left;
xTally += 32;
}
//Identical logic for y-direction
if (yTally > 32) {
loop.start = loop.start->down;
yTally -= 32;
} else if (yTally < 1) {
loop.start = loop.start->up;
yTally += 32;
}
loop.prevPosX = cam.xPos;
loop.prevPosY = cam.yPos;
}
void setPos(camera& cam) {
xTally = cam.xPos % 32 + 1;
yTally = cam.yPos % 32 + 1;
loop.prevPosX = cam.xPos;
loop.prevPosY = cam.yPos;
}
bool checkTile(node* n) { //Check if tile is impassablew (i.e. water)
if (n->type == 1) {
return true;
} else {
return false;
}
}
void draw(GamesEngineeringBase::Window& canvas, camera& cam) { //Draws all tiles in camera's view
loop.prev = loop.lineStart = loop.start;
for (int x = cam.xPos; x < cam.xPos + cam.xDim; x++) {
if (x % 32 == 0 && cam.xPos != x) {
loop.prev = loop.prev->right;
loop.lineStart = loop.lineStart->right;
}
for (int y = cam.yPos; y < cam.yPos + cam.yDim; y++) {
if (y % 32 == 0 && y != cam.yPos) {
loop.prev = loop.prev->down;
}
bool shiftX = 0;
bool shiftY = 0;
int xDraw = x;
int yDraw = y;
if (loop.prev->type & 1) {
shiftX = 1;
}
if ((loop.prev->type >> 1) & 1) {
shiftY = 1;
}
if (x < 0) {
xDraw = 32 + abs(x);
}
if (y < 0) {
yDraw = 32 + abs(y);
}
canvas.draw(x - cam.xPos, y - cam.yPos, tilePack.at(yDraw % 32 + (32 * shiftX), xDraw % 32 + (32 * shiftY)));
}
loop.prev = loop.lineStart;
}
}
};
class Bar { //Rectangular Object with adjustable width
public:
float xPos;
float yPos;
int width;
float percentage;
unsigned int r, g, b;
Bar() {
xPos = 0.f;
yPos = 0.f;
width = 0;
percentage = 1.f;
r = 200;
g = b = 0;
}
Bar(float _xPos, float _yPos) {
xPos = _xPos;
yPos = _yPos;
percentage = 1.f;
width = 0;
r = 200;
g = b = 0;
}
Bar(float _xPos, float _yPos, int _width) {
xPos = _xPos;
yPos = _yPos;
width = _width;
percentage = 1.f;
r = 200;
g = b = 0;
}
void update(float _xPos, float _yPos, float _percentage) {
xPos = _xPos;
yPos = _yPos;
percentage = _percentage;
}
void setColor(unsigned int _r, unsigned int _g, unsigned int _b) {
r = _r;
g = _g;
b = _b;
}
void draw(GamesEngineeringBase::Window& canvas, camera& cam) {
for (int x = 0; x < (int)(width * percentage); x++) { //Limit width by percentage
for (int y = 0; y < 7; y++) { //Fixed height of 7 pixels
if ((int)(xPos + 0.5f) + x > cam.xPos && (int)(xPos + 0.5f) + x < cam.xPos + cam.xDim) { //Check if pixels are off screen
if ((int)(yPos + 0.5f) + y > cam.yPos && (int)(yPos + 0.5f) + y < cam.yPos + cam.yDim) {
canvas.draw(((int)(xPos + 0.5f) - cam.xPos) + x, ((int)(yPos + 0.5f) - cam.yPos) + y, r, g, b);
}
}
}
}
}
};
class sprite {
public:
float xPos, yPos;
int xDim, yDim;
float maxHP;
float health;
float speed;
Bar healthBar;
Vec2 dir;
GamesEngineeringBase::Image* image = new GamesEngineeringBase::Image;
sprite() {
image->load("Resources/L.png");
xPos = 512.f;
yPos = 380.f;
xDim = image->width;
yDim = image->height;
maxHP = 0.f;
health = 0.f;
speed = 0.f;
dir.setPos(0, 0);
}
sprite(float _xPos, float _yPos) {
image->load("Resources/L.png");
xPos = _xPos;
yPos = _yPos;
xDim = healthBar.width = image->width;
yDim = image->height;
health = 100.f;
maxHP = 100.f;
speed = 5.f;
}
sprite(float _xPos, float _yPos, int _dim) {
image->load("Resources/L.png");
xPos = _xPos;
yPos = _yPos;
xDim = yDim = healthBar.width = _dim;
health = 100.f;
maxHP = 100.f;
speed = 5.f;
}
sprite(float _xPos, float _yPos, int _xDim, int _yDim) {
image->load("Resources/L.png");
xPos = _xPos;
yPos = _yPos;
xDim = healthBar.width = _xDim;
yDim = _yDim;
health = 100.f;
maxHP = 100.f;
speed = 2.f;
}
sprite(float _xPos, float _yPos, int _xDim, int _yDim, string filePath) {
image->load(filePath);
xPos = _xPos;
yPos = _yPos;
xDim = healthBar.width = _xDim;
yDim = _yDim;
health = 100.f;
maxHP = 100.f;
speed = 2.f;
}
/*~sprite() {
delete image;
}*/
virtual void save(ofstream& file) {}
void setPos(float _xPos, float _yPos) {
xPos = _xPos;
yPos = _yPos;
}
void setSpriteImg(string filePath) { //Load image from file
image->load(filePath);
xDim = healthBar.width = image->width; //Set health bar width to sprite width
yDim = image->height;
}
void setSpriteImg(string filePath, int _xDim, int _yDim) { //Load image from file, providing sprite dimensions (for images with multiple sprites_
image->load(filePath);
xDim = healthBar.width = _xDim; //Set health bar width to sprite width
yDim = _yDim;
}
float getCenterX() {
return xPos + (float)xDim / 2.f;
}
float getCenterY() {
return yPos + (float)yDim / 2.f;
}
void draw(GamesEngineeringBase::Window& canvas, camera& cam) { //Draw sprite
for (int x = 0; x < xDim; x++) {
for (int y = 0; y < yDim; y++) {
if ((int)(xPos + 0.5f) + x > cam.xPos && (int)(xPos + 0.5f) + x < cam.xPos + cam.xDim) { //Check if pixels are off screen
if ((int)(yPos + 0.5f) + y > cam.yPos && (int)(yPos + 0.5f) + y < cam.yPos + cam.yDim) {
if (image->alphaAt(x, y) > 200) {
canvas.draw(((int)(xPos + 0.5f) - cam.xPos) + x, ((int)(yPos + 0.5f) - cam.yPos) + y, image->at(x, y));
}
}
}
}
}
healthBar.update(xPos, yPos - 20.f, health / maxHP); //Update position and percentage of health bar to match sprite data
healthBar.draw(canvas, cam); //Draw health bar
}
bool touching(sprite& s) { //Checks if two sprites are touching (intersection of rectangular areas)
if (xPos + xDim > s.xPos && xPos < s.xPos + s.xDim) {
if (yPos + yDim > s.yPos && yPos < s.yPos + s.yDim) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
void resetVec() { //Resets movement vector
dir.setPos(0.f, 0.f);
}
void move(float dt) { //Moves sprite, uncoupled with framerate
if (dir.length() != 0.f) { //Only move if movement vector is non-zero
float k = (speed * dt) / dir.length();
dir = dir * k; //Normalize movement vector, cap movement at sprite's speed value
//Increment position in respective directions
if (dir.x > 0) {
xPos += dir.x;
} else {
xPos += dir.x;
}
if (dir.y > 0) {
yPos += dir.y;
} else {
yPos += dir.y;
}
}
}
};
class player : public sprite {
public:
unsigned int aoeLvl;
float atkLvl;
float tempPosX;
float tempPosY;
float damage;
float elapsed;
player() {
image->load("Resources/L.png");
xPos = tempPosX = 512.f;
yPos = tempPosY = 380.f;
xDim = healthBar.width = image->width;
yDim = image->height;
health = 100.f;
maxHP = 100.f;
speed = 0.f;
damage = 15.f;
elapsed = 0.f;
dir.setPos(0, 0);
aoeLvl = 2;
atkLvl = 3.f;
}
player(float _xPos, float _yPos) {
image->load("Resources/L.png");
xPos = tempPosX = _xPos;
yPos = tempPosY = _yPos;
xDim = healthBar.width = image->width;
yDim = image->height;
health = 100.f;
maxHP = 100.f;
speed = 200.f;
damage = 15.f;
elapsed = 0.f;
aoeLvl = 2;
atkLvl = 3.f;
}
void loadFromFile(string& fileLine) {
string val;
stringstream ss(fileLine);
ss >> val;
xPos = stof(val);
ss >> val;
yPos = stof(val);
ss >> val;
tempPosX = stof(val);
ss >> val;
tempPosY = stof(val);
ss >> val;
health = stof(val);
ss >> val;
atkLvl = stof(val);
ss >> val;
aoeLvl = stoi(val);
ss >> val;
elapsed = stof(val);
ss.clear();
maxHP = 100.f;
speed = 200.f;
damage = 15.f;
image->load("Resources/L.png");
xDim = healthBar.width = image->width;
yDim = image->height;
}
void save(ofstream& file) {
file << xPos << " " << yPos << " " << tempPosX << " " << tempPosY << " " << health << " " << atkLvl << " " << aoeLvl << " " << elapsed << endl;
}
void processInputs(GamesEngineeringBase::Window& canvas, float dt) {
resetVec(); //Reset movement vector
//Move player
if (canvas.keyPressed('W')) { //up
dir.y -= speed * dt;
}
if (canvas.keyPressed('S')) { //down
dir.y += speed * dt;
}
if (canvas.keyPressed('A')) { //left
dir.x -= speed * dt;
}
if (canvas.keyPressed('D')) { //right
dir.x += speed * dt;
}
}
void attack(sprite& mob) { //Direct attack, simply deducts health from targeted sprite (no projectile)
mob.health -= damage;
}
void move(float dt) { //moves player, only difference to sprite method is saving temp position values (for collisions)
if (dir.length() != 0.f) { //Only move if movement vector is non-zero
float k = (speed * dt) / dir.length();
dir = dir * k; //Cap movment vector at player speed
tempPosX = xPos; //save player position before movement (for collisions)
tempPosY = yPos;
if (dir.x > 0) {
xPos += dir.x;
} else {
xPos += dir.x;
}
if (dir.y > 0) {
yPos += dir.y;
} else {
yPos += dir.y;
}
}
}
void handleRestricted(tileMap& map, camera& cam) { //Reverts position for map borders and impassable terrain
if (!map.isInf) { //For finite map, prevent passing world boundary
if (xPos < 0.f || xPos + xDim > map.xDim) {
xPos = tempPosX;
}
if (yPos < 0.f || yPos + yDim > map.yDim) {
yPos = tempPosY;
}
}
int xDiff = (int)((xPos - cam.xPos + map.xTally) / 32.f); //Get difference from camera corner to player, in tiles
int yDiff = (int)((yPos - cam.yPos + map.yTally) / 32.f);
map.loop.prev = map.loop.start;
//Shift from start node to tile at top right of player
for (int i = 0; i < xDiff; i++) {
map.loop.prev = map.loop.prev->right;
}
for (int i = 0; i < yDiff; i++) {
map.loop.prev = map.loop.prev->down;
}
//Loop through area of player
map.loop.lineStart = map.loop.prev;
for (int x = (int)xPos; x < (int)(xPos + 0.5f) + xDim; x++) {
map.loop.prev = map.loop.lineStart;
if (x % 32 == 0 && (int)xPos != x) { //If passed tile width, shift to next tile
map.loop.lineStart = map.loop.lineStart->right;
}
for (int y = (int)yPos; y < (int)(yPos + 0.5f) + yDim; y++) {
if (y % 32 == 0 && (int)yPos != y) { //If passed tile width, shift to next tile
map.loop.prev = map.loop.prev->down;
}
if (map.checkTile(map.loop.prev)) { //If tile is impassible, revert to last acceptable position
xPos = tempPosX;
yPos = tempPosY;
break;
}
}
}
}
};
class mob : public sprite {
public:
short type;
short shiftX;
float farness;
float dps;
mob() {
type = 0;
//image->load("Resources/L.png");
xPos = 512.f;
yPos = 380.f;
xDim = healthBar.width = 32;
yDim = 32;
health = 10;
maxHP = 0;
speed = 60.f;
dps = 1.f;
farness = 0.f;
shiftX = 0;
dir.setPos(0, 0);
}
void initType(short _type) {
type = _type;
switch (type) { //Cases for each mob type
case 0:
xDim = 90;
yDim = 120;
health = maxHP = 80.f;
speed = 0.f;
dps = 1.f;
shiftX = 0;
break;
case 1:
xDim = 55;
yDim = 40;
health = maxHP = 15.f;
speed = 100.f;
dps = 5.f;
shiftX = 90;
break;
case 2:
xDim = 80;
yDim = 100;
health = maxHP = 50.f;
speed = 30.f;
dps = 30.f;
shiftX = 145;
break;
case 3:
xDim = 50;
yDim = 50;
health = maxHP = 30.f;
speed = 75.f;
dps = 12.f;
shiftX = 225;
break;
}
}
mob(short _type, GamesEngineeringBase::Image& img) {
image = &img;
xPos = 512.f;
yPos = 380.f;
dps = 1.f;
shiftX = 0;
initType(_type);
healthBar.width = xDim;
farness = 0.f;
dir.setPos(0, 0);
}
mob(string fileLine, GamesEngineeringBase::Image& img) {
image = &img;
string val;
stringstream ss(fileLine);
ss >> val;
initType((short)stoi(val));
ss >> val;
xPos = stof(val);
ss >> val;
yPos = stof(val);
ss >> val;
health = stof(val);
ss >> val;
farness = stof(val);
ss >> val;
dir.x = stof(val);
ss >> val;
dir.y = stof(val);
ss.clear();
healthBar.width = xDim;
}
void save(ofstream& file) {
file << type << " " << xPos << " " << yPos << " " << health << " " << farness << " " << dir.x << " " << dir.y << endl;
}
void distanceTo(sprite& player) { //Calculate and save distance to player
Vec2 vec = Vec2(getCenterX() - player.getCenterX(), getCenterY() - player.getCenterY());
farness = vec.length();
}
void target(sprite& player) { //Direct movement towards a sprite and save distance to same sprite
dir.setPos((float)(player.getCenterX() - getCenterX()), (float)(player.getCenterY() - getCenterY()));
distanceTo(player);
}
void draw(GamesEngineeringBase::Window& canvas, camera& cam) { //Draw mob
for (int x = 0; x < xDim; x++) {
for (int y = 0; y < yDim; y++) {
if ((int)(xPos + 0.5f) + x > cam.xPos && (int)(xPos + 0.5f) + x < cam.xPos + cam.xDim) { //Check if pixels are off screen
if ((int)(yPos + 0.5f) + y > cam.yPos && (int)(yPos + 0.5f) + y < cam.yPos + cam.yDim) {
if (image->alphaAt(x + shiftX, y) > 200) { //Do not draw transparent pixels
canvas.draw(((int)(xPos + 0.5f) - cam.xPos) + x, ((int)(yPos + 0.5f) - cam.yPos) + y, image->at(x + shiftX, y));
}
}
}
}
}
healthBar.update(xPos, yPos - 20.f, health / maxHP);
healthBar.draw(canvas, cam);
}
};
template <class T>
class nodeLL {
public:
nodeLL<T>* next = nullptr;
nodeLL<T>* prev = nullptr;
T sprite;
nodeLL(T b) {
sprite = b;
}
};
template <class T>
class linkedList {
public:
nodeLL<T>* head = nullptr;
nodeLL<T>* tail = nullptr;
linkedList() {
head = nullptr;
}
/*~linkedList() { //Destructor for list
for (nodeLL<T>* nptr = head; nptr != nullptr; nptr = nptr->next) {
nodeLL<T>* next = nptr->next;
delete nptr;
nptr = next;
}
}*/
virtual void save(ofstream& file) {}
void add(T b) { //Adds node
nodeLL<T>* n = new nodeLL<T>(b);
if (head == nullptr) { //If empty, add as head and tail
head = tail = n;
} else { //Otherwise, append to end
tail->next = n;
n->prev = tail;
tail = n; //Make node new tail
}
}
void swap(nodeLL<T>* n) { //Swap a node and the node previous to it
if (head != n) { //Don't swap if it is the head
nodeLL<T>* nextNode = n->next;
nodeLL<T>* prevNode = n->prev->prev;
if (nextNode == nullptr && prevNode == nullptr) { //Meaning node is tail and previous node is head
head = n;
tail = n->prev;
n->next = n->prev;
n->prev->prev = n;
n->prev->next = nullptr;
n->prev = nullptr;
} else if (nextNode == nullptr) { //Meaning node is tail
tail = n->prev;
prevNode->next = n;
n->next = tail;
tail->prev = n;
n->prev = prevNode;
tail->next = nullptr;
} else if (prevNode == nullptr) { //Meaning previous node is head
n->next = head;
head->next = nextNode;
nextNode->prev = head;
head->prev = n;
head = n;
head->prev = nullptr;
} else { //For all general cases
prevNode->next = n;
n->next = n->prev;
n->prev->next = nextNode;
nextNode->prev = n->prev;
n->prev->prev = n;
n->prev = prevNode;
}
}
}
void display() {
for (nodeLL<T>* n = head; n != nullptr; n = n->next) {
cout << n->sprite << "\t";
}
cout << endl;
}
virtual void movecleandraw(GamesEngineeringBase::Window& canvas, camera& cam, player& guy, float dt) {}
};
class swarmLL : public linkedList<mob> {