-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.h
1049 lines (944 loc) · 33.4 KB
/
Game.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 <SFML/Graphics.hpp>
static const float SQRT_2 = std::sqrt(2.f);
static const unsigned short inventoryX = 10;
static const unsigned short inventoryY = 10;
// ITEM TYPES
//WEAPONS ( 0 - 12999 )
//Axes ( 0 - 999 )
//Bows ( 1000 - 1999 )
//Crossbows ( 2000 - 2999 )
//Daggers ( 3000 - 3999 )
const unsigned short Dagger = 3000;
//Javelins ( 4000 - 4999 )
//Maces ( 5000 - 5999 )
//Polearms ( 6000 - 6999 )
//Scepters ( 7000 - 7999 )
//Spears ( 8000 - 8999 )
//Staves ( 9000 - 9999 )
//Swords ( 10000 - 10999 )
const unsigned short Short_Sword = 9000;
//Throwing Weapons ( 11000 - 11999 )
//Wands ( 12000 - 12999 )
//ARMORS ( 13000 - 20999 )
//Amulets ( 13000 - 13999 )
const unsigned short Amulet_1 = 12000;
const unsigned short Amulet_2 = 12001;
const unsigned short Amulet_3 = 12002;
//Armours ( 14000 - 14999 )
const unsigned short Quilted_Armor= 14000;
//Belts ( 15000 - 15999 )
const unsigned short Sash = 15000;
//Boots ( 16000 - 16999 )
const unsigned short Boots = 16000;
//Circlets ( 17000 - 17999 )
const unsigned short Circlet = 17000;
//Gloves ( 18000 - 18999 )
const unsigned short Leather_Gloves = 18000;
//Helms ( 19000 - 19999 )
const unsigned short Cap = 19000;
//Rings ( 20000 - 20999 )
const unsigned short Ring_1 = 20000;
const unsigned short Ring_2 = 20001;
const unsigned short Ring_3 = 20002;
const unsigned short Ring_4 = 20003;
const unsigned short Ring_5 = 20004;
unsigned const short possible_items_size = 16;
unsigned short possible_items[possible_items_size] = {
Dagger,
Short_Sword,
Amulet_1,
Amulet_2,
Amulet_3,
Quilted_Armor,
Sash,
Boots,
Circlet,
Leather_Gloves,
Cap,
Ring_1,
Ring_2,
Ring_3,
Ring_4,
Ring_5
};
//ID auto incrementers
unsigned int ClientID = 0;
unsigned int WallID = 1;
unsigned int SafezoneID = 1;
unsigned int RegeneratorID = 1;
unsigned int ItemID = 1;
unsigned int DaemonID = 1;
struct Solid {
unsigned int id;
unsigned short type;
sf::RectangleShape &collisionRect;
Solid(unsigned int id, unsigned short type, sf::RectangleShape &_collisionRect)
: id(id), type(type), collisionRect(_collisionRect)
{}
};
std::vector<Solid> solids;
struct Target {
void *targetRef;
sf::RectangleShape *collisionRect;
bool *dead;
bool *targetable;
//1 - player
//2 - daemon
unsigned int type;
Target(void *targetRef, sf::RectangleShape &collisionRect, bool &dead, bool &targetable, unsigned int type)
: targetRef(targetRef), collisionRect(&collisionRect), dead(&dead), targetable(&targetable), type(type)
{}
Target() :targetRef(nullptr), collisionRect(nullptr), dead(nullptr), targetable(nullptr), type(0)
{}
bool operator==(const Target &t) {
return t.targetRef == targetRef;
}
};
struct Attack_Normal {
sf::RectangleShape collisionRect;
bool active;
bool ready;
float dmg;
float speed;
float currentTime;
bool check;
float width;
float height;
std::vector<Target> targets;
Attack_Normal(float x, float y, float width, float height, float dmg, float speed)
: active(false), ready(true), dmg(dmg), speed(speed), currentTime(0), check(true), width(width), height(height)
{
collisionRect.setPosition(x, y);
collisionRect.setSize(sf::Vector2f(width, height));
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2, collisionRect.getLocalBounds().height / 2);
}
void addTarget(Target &t) {
targets.push_back(t);
}
void removeTarget(Target &t) {
targets.erase(std::remove(targets.begin(), targets.end(), t), targets.end());
}
void setTargets(std::vector<Target> _targets) {
targets = _targets;
}
Target update(float x, float y, sf::Uint8 direction, float deltaTime) {
if (targets.size() < 1)
return Target();
//Set new Position and angle:
//Right
if (direction == 0) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width, height));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2, collisionRect.getLocalBounds().height / 2);
//Set new position
collisionRect.setPosition(x + collisionRect.getLocalBounds().width * 0.75f, y);
//Set new angle
collisionRect.setRotation(180);
globalMutex.unlock();
}
//Left
else if (direction == 7) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width, height));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2, collisionRect.getLocalBounds().height / 2);
//Set new position
collisionRect.setPosition(x - collisionRect.getLocalBounds().width * 0.75f, y);
//Set new angle
collisionRect.setRotation(0);
globalMutex.unlock();
}
//Up
else if (direction == 1) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width, height));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2, collisionRect.getLocalBounds().height / 2);
//Set new position
collisionRect.setPosition(x, y - collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(90);
globalMutex.unlock();
}
//Down
else if (direction == 4) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width, height));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2, collisionRect.getLocalBounds().height / 2);
//Set new position
collisionRect.setPosition(x, y + collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(270);
globalMutex.unlock();
}
//Up Right
else if (direction == 2) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width / std::sqrt(2.f), height / std::sqrt(2.f)));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2 / std::sqrt(2.f), collisionRect.getLocalBounds().height / 2 / std::sqrt(2.f));
//Set new position
collisionRect.setPosition(x + collisionRect.getLocalBounds().width * 0.75f, y - collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(135);
globalMutex.unlock();
}
//Up Left
else if (direction == 3) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width / std::sqrt(2.f), height / std::sqrt(2.f)));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2 / std::sqrt(2.f), collisionRect.getLocalBounds().height / 2 / std::sqrt(2.f));
//Set new position
collisionRect.setPosition(x - collisionRect.getLocalBounds().width * 0.75f, y - collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(45);
globalMutex.unlock();
}
//Down Right
else if (direction == 5) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width / std::sqrt(2.f), height / std::sqrt(2.f)));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2 / std::sqrt(2.f), collisionRect.getLocalBounds().height / 2 / std::sqrt(2.f));
//Set new position
collisionRect.setPosition(x + collisionRect.getLocalBounds().width * 0.75f, y + collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(225);
globalMutex.unlock();
}
//Down Left
else if (direction == 6) {
globalMutex.lock();
//Set size
collisionRect.setSize(sf::Vector2f(width / std::sqrt(2.f), height / std::sqrt(2.f)));
//Set Origin
collisionRect.setOrigin(collisionRect.getLocalBounds().width / 2 / std::sqrt(2.f), collisionRect.getLocalBounds().height / 2 / std::sqrt(2.f));
//Set new position
collisionRect.setPosition(x - collisionRect.getLocalBounds().width * 0.75f, y + collisionRect.getLocalBounds().height * 0.75f);
//Set new angle
collisionRect.setRotation(315);
globalMutex.unlock();
}
currentTime += deltaTime;
if (currentTime >= speed)
ready = true;
if (currentTime < speed)
return Target();
if (!active)
return Target();
if (!check)
return Target();
check = false;
//Check collisions
for (Target t : targets) {
if (collisionRect.getGlobalBounds().intersects(t.collisionRect->getGlobalBounds())) {
//Hit happend
return t;
}
}
return Target();
}
};
struct Item {
unsigned int id;
//Player id
int owner;
sf::Vector2i inventorySize;
sf::Vector2i inventoryPosition;
sf::RectangleShape collisionRect;
char *name;
unsigned short type;
bool inInventory;
bool grabbed;
bool equipped;
bool visible;
Item()
{}
Item(unsigned int _id, unsigned short _type, float x, float y)
: id(_id), inventoryPosition({ 0, 0 }), type(_type), inInventory(false), grabbed(false), equipped(false), visible(true), owner(-1)
{
if (type == Dagger) {
inventorySize = { 1, 2 };
name = "Dagger";
}
else if (type == Short_Sword) {
inventorySize = { 1, 3 };
name = "Short Sword";
}
else if (type == Amulet_1) {
inventorySize = { 1, 1 };
name = "Amulet_1";
}
else if (type == Amulet_2) {
inventorySize = { 1, 1 };
name = "Amulet_2";
}
else if (type == Amulet_3) {
inventorySize = { 1, 1 };
name = "Amulet_3";
}
else if (type == Quilted_Armor) {
inventorySize = { 2, 3 };
name = "Quilted_Armor";
}
else if (type == Sash) {
inventorySize = { 2, 1 };
name = "Sash";
}
else if (type == Boots) {
inventorySize = { 2, 2 };
name = "Boots";
}
else if (type == Circlet) {
inventorySize = { 2, 2 };
name = "Circlet";
}
else if (type == Leather_Gloves) {
inventorySize = { 2, 2 };
name = "Leather_Gloves";
}
else if (type == Cap) {
inventorySize = { 2, 2 };
name = "Cap";
}
else if (type == Ring_1) {
inventorySize = { 1, 1 };
name = "Ring_1";
}
else if (type == Ring_2) {
inventorySize = { 1, 1 };
name = "Ring_2";
}
else if (type == Ring_3) {
inventorySize = { 1, 1 };
name = "Ring_3";
}
else if (type == Ring_4) {
inventorySize = { 1, 1 };
name = "Ring_4";
}
else if (type == Ring_5) {
inventorySize = { 1, 1 };
name = "Ring_5";
}
collisionRect.setSize(sf::Vector2f(inventorySize.x * 24, inventorySize.y * 24));
collisionRect.setPosition(x, y);
}
};
struct Player {
unsigned int id;
bool logged_in;
sf::TcpSocket *socket;
float time;
char username[20];
float maxHealth;
float health;
float maxMana;
float mana;
bool dead;
bool running;
float speed;
float dmg;
float attackSpeed;
bool targetable;
sf::Vector2f respawn;
//0 - RIGHT
//1 - UP
//2 - UP RIGHT
//3 - UP LEFT
//4 - DOWN
//5 - DOWN RIGHT
//6 - DOWN LEFT
//7 - LEFT
sf::Uint8 direction;
sf::RectangleShape collisionRect;
bool inventoryFreeSpace[10][10];
std::vector<Item *>Inventory;
Attack_Normal normalAttack;
Player(unsigned int id, float x, float y)
:id(id), time(0), username(""), dead(false), running(false), maxHealth(10), health(10), maxMana(10), mana(10), speed(100), dmg(1), attackSpeed(0.84f), direction(4), logged_in(false), normalAttack(x, y, 22, 20, dmg, attackSpeed), respawn(0, 0), targetable(false)
{
for (unsigned char y = 0; y < inventoryY; ++y)
for (unsigned char x = 0; x < inventoryX; ++x)
inventoryFreeSpace[x][y] = true;
collisionRect.setPosition(x, y);
collisionRect.setSize(sf::Vector2f{ 26, 44 });
collisionRect.setOrigin(13, 22);
socket = new sf::TcpSocket;
}
void attack() {
if (normalAttack.ready) {
normalAttack.active = true;
normalAttack.ready = false;
normalAttack.check = true;
normalAttack.currentTime = 0;
}
}
Target updateAttack(float deltaTime) {
return normalAttack.update(collisionRect.getPosition().x, collisionRect.getPosition().y, direction, deltaTime);
}
};
std::vector<Player> players;
std::vector<sf::Vector2f> playerBeforeWall;
std::vector<sf::Vector2f> playerBehindWall;
struct Wall {
unsigned int id;
sf::RectangleShape collisionRect;
//0 - RIGHT
//1 - UP
//2 - UP RIGHT
//3 - UP LEFT
//4 - DOWN
//5 - DOWN RIGHT
//6 - DOWN LEFT
//7 - LEFT
sf::Uint8 direction;
Wall(unsigned int id, float x, float y, sf::Uint8 direction)
: id(id), direction(direction)
{
collisionRect.setPosition(x, y);
if (direction == 1 || direction == 4) {
collisionRect.setSize(sf::Vector2f{ 62.f, 40.f });
collisionRect.setOrigin(31.f, 30.f);
}
else if (direction == 0 || direction == 7) {
collisionRect.setSize(sf::Vector2f{ 20.f, 64.f });
collisionRect.setOrigin(22.f, 40.f);
}
else {
collisionRect.setSize(sf::Vector2f{ 30.f, 20.f });
collisionRect.setOrigin(12.f, 20.f);
collisionRect.rotate(-35);
}
}
};
std::vector<Wall> walls;
struct Safezone {
unsigned int id;
float active;
sf::RectangleShape rc;
Safezone(unsigned int id, float x, float y, float w, float h, bool active)
: id(id), active(active)
{
rc.setPosition(x, y);
rc.setSize(sf::Vector2f(w, h));
rc.setOrigin(x / 2, y / 2);
}
};
std::vector<Safezone> safezones;
struct Regenerator {
unsigned int id;
float active;
sf::RectangleShape rc;
sf::Uint8 type;
float time;
Regenerator(unsigned int id, float x, float y, float w, float h, bool active, sf::Uint8 type)
: id(id), active(active), type(type), time(0)
{
rc.setPosition(x, y);
rc.setSize(sf::Vector2f(w, h));
rc.setOrigin(x / 2, y / 2);
}
};
std::vector<Regenerator> regenerators;
bool collisionHappend(sf::RectangleShape &rc) {
sf::RectangleShape s;
unsigned int j = 0;
for (unsigned int i = 0; i < solids.size(); ++i) {
s = solids[i].collisionRect;
//If s is a wall
if (solids[i].type == 0) {
//Put the collision rectangle up or down
if (rc.getPosition().y < s.getPosition().y + 30)
s.setPosition(playerBeforeWall[j]);
if (rc.getPosition().y > s.getPosition().y - 30)
s.setPosition(playerBehindWall[j]);
++j;
}
//Checking for collision
if (s.getGlobalBounds().intersects(rc.getGlobalBounds())) {
return true;
}
}
return false;
}
struct Daemon {
unsigned int id;
sf::RectangleShape collisionRect;
sf::RectangleShape listenerRect;
float maxHealth;
float health;
float maxMana;
float mana;
bool dead;
float dead_Time;
bool running;
float speed;
float dmg;
float attackSpeed;
float targetUnseen_Time;
bool doBeenHit;
float beenHitTime;
bool targetable;
std::vector<sf::Vector2f> path;
Target target;
//0 - RIGHT
//1 - UP
//2 - UP RIGHT
//3 - UP LEFT
//4 - DOWN
//5 - DOWN RIGHT
//6 - DOWN LEFT
//7 - LEFT
sf::Uint8 direction;
Attack_Normal normalAttack;
bool dropped;
Daemon(unsigned int id, float x, float y, sf::Uint8 direction, float maxHealth, float health, float maxMana, float mana, bool running, float speed, float dmg, float attackSpeed)
: id(id), maxHealth(maxHealth), health(health), maxMana(maxMana), mana(mana), dead(false), dead_Time(0), running(running), speed(speed), dmg(dmg), attackSpeed(attackSpeed), direction(direction), normalAttack(x, y, 22, 20, dmg, attackSpeed), dropped(false), targetUnseen_Time(0), doBeenHit(false), beenHitTime(0), targetable(true)
{
collisionRect.setPosition(x, y);
collisionRect.setSize(sf::Vector2f{ 26, 44 });
collisionRect.setOrigin(13, 22);
listenerRect.setPosition(x, y);
listenerRect.setSize(sf::Vector2f{ 200, 44 });
listenerRect.setOrigin(200, 22);
std::vector<Target> targets;
targets.reserve(sizeof(Target) * players.size());
for (Player &p : players) {
targets.emplace_back(Target(&p, p.collisionRect, p.dead, p.targetable, 1));
}
normalAttack.setTargets(targets);
}
bool attack() {
if (normalAttack.ready && !doBeenHit) {
normalAttack.active = true;
normalAttack.ready = false;
normalAttack.check = true;
normalAttack.currentTime = 0;
return true;
}
return false;
}
bool move(float deltaTime) {
if (path.size() < 1)
return false;
long meX = collisionRect.getPosition().x;
long meY = collisionRect.getPosition().y;
sf::Vector2f goal = path.back();
sf::Vector2f movement(0.f, 0.f);
//Up Right
if (goal.x > meX && goal.y < meY) {
movement.x += speed * deltaTime;
movement.y -= speed * deltaTime;
movement /= SQRT_2;
direction = 2;
}
//Up Left
else if (goal.x < meX && goal.y < meY) {
movement.x -= speed * deltaTime;
movement.y -= speed * deltaTime;
movement /= SQRT_2;
direction = 3;
}
//Down Right
else if (goal.x > meX && goal.y > meY) {
movement.x += speed * deltaTime;
movement.y += speed * deltaTime;
movement /= SQRT_2;
direction = 5;
}
//Down Left
else if (goal.x < meX && goal.y > meY) {
movement.x -= speed * deltaTime;
movement.y += speed * deltaTime;
movement /= SQRT_2;
direction = 6;
}
//Right
else if (goal.x > meX) {
movement.x += speed * deltaTime;
direction = 0;
}
//Left
else if (goal.x < meX) {
movement.x -= speed * deltaTime;
direction = 7;
}
//Up
else if (goal.y < meY) {
movement.y -= speed * deltaTime;
direction = 1;
}
//Down
else if (goal.y > meY) {
movement.y += speed * deltaTime;
direction = 4;
}
collisionRect.move(movement);
//If we are close enough to the goal, then delete it
if (std::abs(collisionRect.getPosition().x - goal.x) < 10 && std::abs(collisionRect.getPosition().y - goal.y) < 10)
path.erase(path.begin() + path.size() - 1);
if (movement.x == 0 && movement.y == 0)
return false;
return true;
}
bool move(sf::Vector2f pos, float deltaTime) {
long posX = pos.x;
long posY = pos.y;
long meX = collisionRect.getPosition().x;
long meY = collisionRect.getPosition().y;
if (posX == meX && posY == meY)
return false;
sf::Vector2f movement(0.f, 0.f);
//Up Right
if (posX > meX && posY < meY) {
movement.x += speed * deltaTime;
movement.y -= speed * deltaTime;
movement /= SQRT_2;
direction = 2;
}
//Up Left
else if (posX < meX && posY < meY) {
movement.x -= speed * deltaTime;
movement.y -= speed * deltaTime;
movement /= SQRT_2;
direction = 3;
}
//Down Right
else if (posX > meX && posY > meY) {
movement.x += speed * deltaTime;
movement.y += speed * deltaTime;
movement /= SQRT_2;
direction = 5;
}
//Down Left
else if (posX < meX && posY > meY) {
movement.x -= speed * deltaTime;
movement.y += speed * deltaTime;
movement /= SQRT_2;
direction = 6;
}
//Right
else if (posX > meX) {
movement.x += speed * deltaTime;
direction = 0;
}
//Left
else if (posX < meX) {
movement.x -= speed * deltaTime;
direction = 7;
}
//Up
else if (posY < meY) {
movement.y -= speed * deltaTime;
direction = 1;
}
//Down
else if (posY > meY) {
movement.y += speed * deltaTime;
direction = 4;
}
sf::Vector2f oldPos = collisionRect.getPosition();
collisionRect.move(movement);
return !collisionHappend(collisionRect);
}
Target* listen() {
float angle, direction_angle;
bool pastZero = false;
std::vector<Target *> possibleTargets;
for (Target &t : normalAttack.targets) {
if (*t.dead || !*t.targetable)
continue;
//Get the angle between the this and the target
angle = atan2(collisionRect.getPosition().y - t.collisionRect->getPosition().y, collisionRect.getPosition().x - t.collisionRect->getPosition().x);
angle = angle * 180 / 3.1415926f;
if (angle < 0)
angle += 360;
//Get the looking angle
//Right
if (direction == 0) {
direction_angle = 180;
pastZero = angle > 85 && angle < 275;
}
//Left
else if (direction == 7) {
direction_angle = 0;
pastZero = angle > 265 || angle < 95;
}
//Up
else if (direction == 1) {
direction_angle = 90;
pastZero = angle > 355 || angle < 185;
}
//Down
else if (direction == 4) {
direction_angle = 270;
pastZero = angle > 175 || angle < 5;
}
//Up Right
else if (direction == 2) {
direction_angle = 135;
pastZero = angle > 40 && angle < 230;
}
//Up Left
else if (direction == 3) {
direction_angle = 45;
pastZero = angle > 310 || angle < 140;
}
//Down Right
else if (direction == 5) {
direction_angle = 225;
pastZero = angle > 130 && angle < 320;
}
//Down Left
else if (direction == 6) {
direction_angle = 315;
pastZero = angle > 220 || angle < 50;
}
bool obstacle = false;
//Set listener rectangle
listenerRect.setSize(sf::Vector2f{ 200, 44 });
listenerRect.setOrigin(200, 22);
listenerRect.setPosition(collisionRect.getPosition().x, collisionRect.getPosition().y);
listenerRect.setRotation(angle);
//If the target is behind this
if (!pastZero) {
listenerRect.setSize(sf::Vector2f{ 120, 44 });
listenerRect.setOrigin(120, 22);
}
//The target is close enough?
if (listenerRect.getGlobalBounds().intersects(t.collisionRect->getGlobalBounds())) {
//There is a obstacle in the listener Rect?
for (Wall &w : walls) {
float distance_obstacle = std::sqrt(std::pow(std::abs(collisionRect.getPosition().x - w.collisionRect.getPosition().x), 2) + std::pow(std::abs(collisionRect.getPosition().y - w.collisionRect.getPosition().y), 2));
float distance_target = std::sqrt(std::pow(std::abs(collisionRect.getPosition().x - t.collisionRect->getPosition().x), 2) + std::pow(std::abs(collisionRect.getPosition().y - t.collisionRect->getPosition().y), 2));
//The obstacle is closer?
if (distance_obstacle < distance_target) {
//The obstacle is at the right angle?
if (listenerRect.getGlobalBounds().intersects(w.collisionRect.getGlobalBounds())) {
obstacle = true;
break;
}
}
}
if (!obstacle) {
//Target found
listenerRect.setSize(sf::Vector2f{ 320, 44 });
listenerRect.setOrigin(320, 22);
possibleTargets.push_back(&t);
}
}
}
if (possibleTargets.size() == 1)
return possibleTargets[0];
else if (possibleTargets.size() > 1) {
//Which target is the closest?
unsigned int min = 0;
float xd, yd, xdM, ydM;
for (unsigned int i = 1; i < possibleTargets.size(); ++i) {
xd = collisionRect.getPosition().x - possibleTargets[i]->collisionRect->getPosition().x;
yd = collisionRect.getPosition().y - possibleTargets[i]->collisionRect->getPosition().y;
xdM = collisionRect.getPosition().x - possibleTargets[min]->collisionRect->getPosition().x;
ydM = collisionRect.getPosition().y - possibleTargets[min]->collisionRect->getPosition().y;
if (std::abs(xd * xd + yd * yd) < std::abs(xdM * xdM + ydM * ydM))
min = i;
}
return possibleTargets[min];
}
return nullptr;
}
Target updateAttack(float deltaTime) {
return normalAttack.update(collisionRect.getPosition().x, collisionRect.getPosition().y, direction, deltaTime);
}
};
std::vector<Item> items;
std::vector<Daemon> daemons;
void CreateMap(void) {
//Down
walls.push_back(Wall(WallID++, -300, 200, 4));//63 40 || 20 64
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -247, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -184, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -121, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -58, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 5, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 68, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 131, 200, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Left
walls.push_back(Wall(WallID++, -308, 156, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -308, 96, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -308, 36, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -308, -24, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -308, -84, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Right
walls.push_back(Wall(WallID++, 164, 156, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 164, 100, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 164, -84, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Up
walls.push_back(Wall(WallID++, -300, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -247, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -184, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -121, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, -58, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 5, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 68, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 131, -120, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Test
//UP
walls.push_back(Wall(WallID++, 280, -60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 343, -60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 406, -60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Right
walls.push_back(Wall(WallID++, 520, 0, 7));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
//Down
walls.push_back(Wall(WallID++, 280, 60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 343, 60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
walls.push_back(Wall(WallID++, 406, 60, 4));
playerBeforeWall.push_back(sf::Vector2f(walls.back().collisionRect.getPosition().x, walls.back().collisionRect.getPosition().y + 50));
playerBehindWall.push_back(walls.back().collisionRect.getPosition());
daemons.push_back(Daemon(DaemonID++, 400, 500, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 300, 500, 0, 100, 100, 10, 10, false, 100, 5, 0.4f));
daemons.push_back(Daemon(DaemonID++, 700, 600, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 550, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 500, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 450, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 400, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 350, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 300, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 250, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 200, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 150, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 100, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 50, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, 0, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, -50, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, -100, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, -150, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
daemons.push_back(Daemon(DaemonID++, 700, -200, 0, 3, 3, 3, 3, false, 100, 1, 0.84f));
items.push_back(Item(ItemID++, Short_Sword, -100, 50));
items.push_back(Item(ItemID++, Amulet_1, 60, 100));
items.push_back(Item(ItemID++, Quilted_Armor, -50, 0));
safezones.push_back(Safezone(SafezoneID++, -630, -130, 476, 320, true));
regenerators.push_back(Regenerator(RegeneratorID++, -100, -44, 100, 100, true, 0));
//Add solids
//Safezones
solids.reserve(sizeof(Solid) * safezones.size());
for (Safezone &s : safezones)
solids.emplace_back(Solid(s.id, 1, s.rc));
//Walls
solids.reserve(sizeof(Solid) * walls.size());
for (Wall &w : walls)
solids.emplace_back(Solid(w.id, 0, w.collisionRect));
}