/
Box.cpp
1144 lines (984 loc) · 31.8 KB
/
Box.cpp
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
#include "Box.h"
Box::Box(std::map<std::string, Box*>* groundBoxes, std::vector<Box*>* boxes)
{
this->groundBoxes = groundBoxes;
this->boxes = boxes;
init();
}
Box::~Box()
{
//dtor
}
void Box::init()
{
isActive = false;
isDead = false;
isAttacking = false;
isNotified = false;
isFlaggedForConversion = false;
isConverted = false;
isInfo = false;
isSticky = false;
team = TEAM_NEUTRAL;
hasActiveTargetArea = false;
target.position = sf::Vector2f(0.0f, 0.0f);
target.radius = 0.0f;
speed = 5.0f;
stopSpeed = 1.5f;
jumpHeight = 6.0f;
isJumping = false;
lastImpulseReceived = 0.0f;
health = 100;
type = "box";
preJumpPosition = b2Vec2(0.0f, 0.0f);
tagCharges = 1;
stompJumpForce = 15000.0f;
stompForce = 60000.0f;
attackStarted = false;
attackTimer.setThreshold(1.0f);
attackTimer.endTimer();
attackDamage = 30;
damageResistance = 1.0f;
hitTimer.setThreshold(0.05f);
hitTimer.endTimer();
hitNumberTimer.setThreshold(1.5f);
hitNumberTimer.endTimer();
aiTarget = NULL;
attackRange = 300;
aiAttackTimer.setThreshold(2.0f);
aiAttackTimer.endTimer();
contactSoundTimer.setThreshold(0.5f);
contactSoundTimer.endTimer();
stickyMaster = NULL;
floatMaster = NULL;
// Damage taken
text_damageTaken = sf::Text("", font_default, DEFAULT_TEXT_SIZE);
text_damageTaken.setColor(sf::Color::Red);
text_damageTaken.setStyle(sf::Text::Bold);
// Team, Type, Charges
addBoxInfoLine();
addBoxInfoLine();
addBoxInfoLine();
addBoxInfoLine();
}
void Box::updateTexture()
{
if(body->IsActive())
{
setTexture(textures[type + "_ground"]);
switch(team)
{
case TEAM_NEUTRAL:
overlaySprite.setColor(sf::Color(255, 255, 255, 0));
break;
case TEAM_ALLY:
setOverlayTexture(textures["overlay_allied"]);
overlaySprite.setColor(sf::Color(255, 255, 255, 255));
break;
case TEAM_ENEMY:
setOverlayTexture(textures["overlay_enemy"]);
overlaySprite.setColor(sf::Color(255, 255, 255, 255));
break;
}
}
else
{
setTexture(textures["box_ground"]);
}
if(isActive)
{
setOverlayTexture(textures["overlay_selected"]);
overlaySprite.setColor(sf::Color(255, 255, 255, 255));
}
}
void Box::createBox(b2World& physicsWorld, b2Vec2 position, b2Vec2 size, float density, float friction, bool isDynamic, uint16 categoryBits, uint16 maskBits)
{
// Physics body initialization
b2BodyDef bodyDef;
bodyDef.position = b2Vec2(position.x/SCALE, position.y/SCALE);
if(isDynamic)
{
bodyDef.type = b2_dynamicBody;
}
else
{
bodyDef.type = b2_staticBody;
}
body = physicsWorld.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox((size.x/2)/SCALE, (size.y/2)/SCALE);
b2FixtureDef fixtureDef;
fixtureDef.density = density;
fixtureDef.friction = friction;
//fixtureDef.restitution = 0.05f;
fixtureDef.filter.categoryBits = categoryBits;
fixtureDef.filter.maskBits = maskBits;
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);
if(body->GetType() == b2_staticBody)
{
setBodyActive(false);
}
// Sprite initilization
sprite.setOrigin(size.x/2.0f, size.y/2.0f);
sprite.setPosition(position.x, position.y);
overlaySprite.setOrigin(size.x/2.0f, size.y/2.0f);
overlaySprite.setPosition(position.x, position.y);
}
void Box::setTexture(sf::Texture &tex)
{
sprite.setTexture(tex);
}
void Box::setOverlayTexture(sf::Texture &tex)
{
overlaySprite.setTexture(tex);
}
bool Box::groundBoxExists(const std::string id)
{
if(groundBoxes->find(id) == groundBoxes->end())
{
return false;
}
else
{
return true;
}
}
void Box::update(Events gameEvents)
{
// ------------------------------- ACTIVE -------------------------------
if(body->IsActive())
{
// Update UserData
body->SetUserData(this);
// ------------------------------- Dynamic only -------------------------------
if(body->GetType() == b2_dynamicBody)
{
sprite.setPosition(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
sprite.setRotation(180/b2_pi * body->GetAngle());
overlaySprite.setPosition(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
overlaySprite.setRotation(180/b2_pi * body->GetAngle());
lastMouse.x = gameEvents.lastMouse.x;
lastMouse.y = gameEvents.lastMouse.y;
// Movement
moveToTargetArea();
// AI
ai();
// Atack
attack();
}
else // ------------------------------- Static only -------------------------------
{
ai();
attack();
checkNotifications();
checkForConversion();
}
// ------------------------------- Both -------------------------------
processImpulse();
// Deletes boxes that have fallen out of the world
checkHeight();
// MouseOver
if(isSelected(gameEvents.lastMouse.x, gameEvents.lastMouse.y))
{
isInfo = true;
sprite.setColor(sf::Color(120, 120, 120, 255));
}
else
{
isInfo = false;
sprite.setColor(sf::Color(255, 255, 255, 255));
}
}
else // ------------------------------- INACTIVE -------------------------------
{
if(body->GetType() == b2_staticBody)
{
checkNotifications();
if(isSelected(gameEvents.lastMouse.x, gameEvents.lastMouse.y))
{
sprite.setColor(sf::Color(120, 120, 120, 255));
}
else
{
sprite.setColor(sf::Color(200, 200, 200, 255));
}
}
}
// ------------------------------- Independant of active status -------------------------------
// Check for minimap
// float percX = gameEvents.lastMouseWindow.x / window_width;
// float percY = gameEvents.lastMouseWindow.y / window_height;
//
// if(percX >= 0.0f && percX <= minimapSize && percY >= 1.0f - minimapSize && percY <= 1.0f)
// {
// sprite.setColor(sf::Color(255, 255, 255, 255));
// }
// Flicker as long as the timer is active
if(!hitTimer.timeReached())
{
sprite.setColor(sf::Color::Red);
}
// Update timer
hitTimer.update();
hitNumberTimer.update();
}
void Box::draw(sf::RenderWindow& window)
{
// Draw Box
if(body->IsActive() || (!body->IsActive() && debug_showInactiveGroundBoxes))
{
window.draw(sprite);
window.draw(overlaySprite);
}
// Dynamic
if(body->GetType() == b2_dynamicBody)
{
// Draw target
if(hasActiveTargetArea)
{
// Line
if(debug_showTargetLines)
{
sf::Vector2f a(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
sf::Vector2f b(target.position.x, target.position.y);
sf::Vertex line[2] = {a, b};
line[0].color = sf::Color::Red;
line[1].color = sf::Color::Red;
window.draw(line, 2, sf::Lines);
}
// Area
if(debug_showTargetArea)
{
sf::RectangleShape rect;
sf::Vector2f pos(target.originalPosition.x - target.radius, target.originalPosition.y - target.radius);
sf::Vector2f size(abs((target.originalPosition.x - target.radius) - (target.originalPosition.x + target.radius)), abs((target.originalPosition.y - target.radius) - (target.originalPosition.y + target.radius)));
rect.setPosition(pos);
rect.setSize(size);
rect.setFillColor(sf::Color(0, 100, 200, 25));
window.draw(rect);
}
}
}
else // Static
{
// Health
if(debug_showStaticHealth)
{
std::stringstream healthString;
healthString << health;
sf::Text textHealth;
textHealth.setString(healthString.str());
textHealth.setCharacterSize(10);
textHealth.setPosition(body->GetPosition().x * SCALE - BOX_SIZE/2, body->GetPosition().y * SCALE - BOX_SIZE/2);
textHealth.setColor(sf::Color::Black);
window.draw(textHealth);
}
}
}
sf::Sprite* Box::getSprite()
{
return &sprite;
}
b2Body* Box::getBody()
{
return body;
}
bool Box::isSelected(int mouseX, int mouseY)
{
int minX = sprite.getPosition().x - BOX_SIZE/2;
int maxX = minX + BOX_SIZE;
int minY = sprite.getPosition().y - BOX_SIZE/2;
int maxY = minY + BOX_SIZE;
if(mouseX <= maxX && mouseX >= minX && mouseY <= maxY && mouseY >= minY)
{
return true;
}
else
{
return false;
}
}
void Box::startContact(Box* box, bool isDynamic)
{
if(isDynamic)
{
contactDynamicList.push_back(box);
}
else
{
contactStaticList.push_back(box);
}
}
void Box::endContact(Box* box,bool isDynamic)
{
if(isDynamic)
{
contactDynamicList.erase(std::remove(contactDynamicList.begin(), contactDynamicList.end(), box), contactDynamicList.end());
}
else
{
contactStaticList.erase(std::remove(contactStaticList.begin(), contactStaticList.end(), box), contactStaticList.end());
}
}
void Box::moveToTargetArea()
{
// Move only if we have a target
if(hasActiveTargetArea)
{
repeatAttack = false;
// Get center and calculate directions
float centerX = body->GetPosition().x * SCALE;
float centerY = body->GetPosition().y * SCALE;
float cSpeed = speed;
float cJumpHeight = jumpHeight;
if(centerX > target.position.x)
{
cSpeed = -cSpeed;
}
if(centerY < target.position.y)
{
cJumpHeight = 0.0f;
}
// STOP CONDITION: We are within target boundaries
if(centerX <= target.position.x + target.radius && centerX >= target.position.x - target.radius && centerY <= target.position.y + target.radius && centerY >= target.position.y - target.radius)
{
hasActiveTargetArea = false;
//dampenedJump();
body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
}
else
{
// Can jump again if we touch a static box
if(contactStaticList.size() > 0)
{
isJumping = false;
}
else
{
// Can jump if we are not in motion anylonger
// Prevents getting stuck on dynamic boxes
if(!isInMotion())
{
isJumping = false;
}
}
// If either of our dynamic contacts is a float box
// we can jump again
for(unsigned int i = 0; i < contactDynamicList.size(); i++)
{
if(contactDynamicList.at(i)->type == "floatbox" && ((team == TEAM_ALLY && contactDynamicList.at(i)->team != TEAM_ENEMY) || (team == TEAM_ENEMY && contactDynamicList.at(i)->team != TEAM_ALLY)))
{
isJumping = false;
}
}
// We can only jump if we are not currently jumping
if(!isJumping)
{
// If we are at the same horizontal position from before the last jump, we are probably stuck.
// In this case use normal jump height instead of 0 to enable climbing
if((int)preJumpPosition.x == (int)(centerX))
{
cJumpHeight = jumpHeight;
}
preJumpPosition = b2Vec2(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
jump(cJumpHeight, cSpeed);
isJumping = true;
}
// If we have reached our goal on the horizontal plane, terminate movement
if(centerX >= target.position.x - target.radius && centerX <= target.position.x + target.radius)
{
hasActiveTargetArea = false;
dampenedJump();
}
}
}
}
void Box::moveToTarget(float x, float y, float radius, bool withStopCondition, float speed)
{
// Current position
float centerX = body->GetPosition().x * SCALE;
float centerY = body->GetPosition().y * SCALE;
float distanceX = x - centerX;
float distanceY = y - centerY;
float distance = sqrt(distanceX*distanceX + distanceY*distanceY);
float mod = speed / distance;
float targetSpeedX = distanceX * mod;
float targetSpeedY = distanceY * mod;
// Stop condition
if(withStopCondition && centerX <= x + radius && centerX >= x - radius && centerY <= y + radius && centerY >= y - radius)
{
hasActiveTargetArea = false;
body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
}
else
{
body->SetLinearVelocity(b2Vec2(targetSpeedX, targetSpeedY));
}
}
void Box::jump(float jump, float speed)
{
int rndS = rand() % 200 + 900;
int rndH = rand() % 200 + 900;
float rndSpeed = rndS / 1000.0f;
float rndHeight = rndH / 1000.0f;
b2Vec2 force(speed * rndSpeed, -jump * rndHeight);
body->SetLinearVelocity(force);
}
void Box::dampenedJump()
{
float verticalSpeed = body->GetLinearVelocity().y;
float horizontalSpeed = stopSpeed;
if(body->GetLinearVelocity().x < 0)
{
horizontalSpeed = -horizontalSpeed;
}
body->SetLinearVelocity(b2Vec2(horizontalSpeed, verticalSpeed));
}
void Box::setLatestImpulse(float imp)
{
lastImpulseReceived = imp;
}
void Box::processImpulse()
{
if(lastImpulseReceived >= IMPULSE_EFFECT_THRESHOLD)
{
// Dynamic logic
if(body->GetType() == b2_dynamicBody)
{
//std::cout << "Dynamic Force received: " << lastImpulseReceived << std::endl;
}
else // Static logic
{
//std::cout << "Static Force received: " << lastImpulseReceived << std::endl;
// Lose health
//health -= (int)(lastImpulseReceived * IMPULSE_DAMAGE_MULTIPLIER);
// if(health <= 0)
// {
// isDead = true;
// }
}
// Reset impulse
lastImpulseReceived = 0.0f;
}
}
bool Box::isInMotion()
{
if(body->GetLinearVelocity().x == 0.0f && body->GetLinearVelocity().y == 0.0f)
{
return false;
}
return true;
}
void Box::attack()
{
if(repeatAttack && attackTimer.timeReached())
{
isAttacking = true;
}
if(isAttacking)
{
attackTimer.resetTimer();
aiTarget = NULL;
// If we are stuck with another box
if(stickyMaster != NULL)
{
// If the other box is not from our team, we cannot
if(stickyMaster->team != team)
{
return;
}
}
hasActiveTargetArea = false;
if(!attackStarted)
{
if(contactStaticList.size() > 0 || contactDynamicList.size())
{
body->ApplyForceToCenter(b2Vec2(0.0f, -stompJumpForce));
}
attackStarted = true;
}
else
{
if(body->GetLinearVelocity().y >= -1.0f && body->GetLinearVelocity().y <= 1.0f)
{
body->ApplyForceToCenter(b2Vec2(0.0f, stompForce));
}
if(body->GetLinearVelocity().y > 0.0f)
{
for(unsigned int i = 0; i < contactDynamicList.size(); i++)
{
if(contactDynamicList.at(i)->team != team)
{
contactDynamicList.at(i)->takeHit(attackDamage, team);
attackStarted = false;
isAttacking = false;
}
}
for(unsigned int i = 0; i < contactStaticList.size(); i++)
{
if(contactStaticList.at(i)->team != team)
{
contactStaticList.at(i)->takeHit(attackDamage, team);
attackStarted = false;
isAttacking = false;
}
}
if(attackStarted && isAttacking)
{
if(contactDynamicList.size() > 0)
{
attackStarted = false;
isAttacking = false;
}
}
if(!attackStarted && !isAttacking)
{
playRandomAttackSound();
}
}
}
}
if(repeatAttack)
{
attackTimer.update();
}
}
void Box::changeTeam(int team)
{
this->team = team;
isActive = false;
resetAiTarget();
resetAttack();
updateTexture();
}
void Box::checkHeight()
{
if(body->GetPosition().y * SCALE > GROUND_STARTING_HEIGHT + MAX_WORLD_HEIGHT * BOX_SIZE - BOX_SIZE*3)
{
repeatAttack = false;
}
if((body->GetPosition().y * SCALE > GROUND_STARTING_HEIGHT + MAX_WORLD_HEIGHT * BOX_SIZE) ||
body->GetPosition().y * SCALE < -MAX_SKY_HEIGHT)
{
isDead = true;
}
}
void Box::notifyNeighbours()
{
// Iterate through all neighbours
for(unsigned int i = 0; i < 9; i++)
{
// Ignore the center box because that is the one about to be deleted
if(i == 4)
{
continue;
}
// Make sure the neighbour exists
if(neighbours[i].exists)
{
// Iterate through the neighbours of the existing neighbour
for(unsigned int j = 0; j < 9; j++)
{
// Skip the center box
if(j == 4)
{
continue;
}
// Make sure the neighbour of the neighbour exists
if(groundBoxes->at(neighbours[i].index)->neighbours[j].exists)
{
// Check if the neighbour of our neighbour is our original starting point
if(groundBoxes->at(neighbours[i].index)->neighbours[j].index == neighbours[4].index)
{
// Make sure our starting point is marked as non-existant in all neighbours neighbourhoods
groundBoxes->at(neighbours[i].index)->neighbours[j].exists = false;
}
}
}
// Notify all existing neighbours
groundBoxes->at(neighbours[i].index)->isNotified = true;
}
}
}
void Box::checkNotifications()
{
if(isNotified)
{
int empty = 0;
for(int i = 0; i < 9; i++)
{
if(neighbours[i].exists)
{
empty++;
}
}
if(empty > 0)
{
setBodyActive(true);
}
isNotified = false;
}
}
void Box::checkForConversion()
{
bool top = neighbours[1].exists;
bool left = neighbours[3].exists;
bool bottom = neighbours[5].exists;
bool right = neighbours[7].exists;
if(!top && !left && !bottom && !right)
{
isFlaggedForConversion = true;
}
}
void Box::reduceCharges(int amount)
{
tagCharges -= amount;
if(tagCharges < 0)
{
tagCharges = 0;
}
}
void Box::displayAdditionals(sf::RenderWindow& window)
{
displayMarker(window);
displayInfo(window);
if(!hitNumberTimer.timeReached())
{
float posX = body->GetPosition().x * SCALE -10;
float posY = body->GetPosition().y * SCALE - BOX_SIZE/2 -15;
text_damageTaken.setPosition(posX, posY);
window.draw(text_damageTaken);
}
}
void Box::displayMarker(sf::RenderWindow& window)
{
if(team == TEAM_ALLY && isActive)
{
sf::Vector2f a(body->GetPosition().x * SCALE - 5, body->GetPosition().y * SCALE - BOX_SIZE/2.0f - 13);
sf::Vector2f b(body->GetPosition().x * SCALE + 5, body->GetPosition().y * SCALE - BOX_SIZE/2.0f - 13);
sf::Vector2f c(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE - BOX_SIZE/2.0f - 5);
sf::Vertex line[3] = {a, b, c};
line[0].color = sf::Color::Green;
line[1].color = sf::Color::Green;
line[2].color = sf::Color::Green;
window.draw(line, 3, sf::Triangles);
}
// if(hasActiveTargetArea)
// {
// float centerX = body->GetPosition().x * SCALE;
// float centerY = body->GetPosition().y * SCALE;
//
// float distX = target.position.x - centerX;
// float distY = target.position.y - centerY;
// float dist = sqrt(distX*distX + distY*distY);
// float mod = (BOX_SIZE) / dist;
//
// float lengthX = distX * mod;
// float lengthY = distY * mod;
//
// sf::Vector2f a(centerX, centerY);
// sf::Vector2f b(centerX + lengthX, centerY + lengthY);
//
// sf::Vertex line[2] = {a, b};
//
// line[0].color = sf::Color::White;
// line[1].color = sf::Color::White;
//
// window.draw(line, 2, sf::Lines);
// }
}
void Box::displayInfo(sf::RenderWindow& window)
{
if(global_showFriendlyBoxInfo || isInfo)
{
updateInfo();
window.draw(text_background);
for(unsigned int i = 0; i < text_info.size(); i++)
{
window.draw(text_info.at(i));
}
}
}
void Box::addBoxInfo(int id, std::string text)
{
// Position
calcInfoBoxPosition();
float posX = text_topLeftX;
float posY = text_topLeftY;
// Type
text_info.at(id).setString(text);
text_info.at(id).setPosition(posX, posY + id * DEFAULT_LINE_HEIGHT);
}
void Box::updateInfo()
{
std::stringstream stream_type;
stream_type << "Type: " << getProperType(type);
std::stringstream stream_charges;
stream_charges << "Charges: " << tagCharges;
std::string string_team;
if(team == TEAM_NEUTRAL)
{
text_info.at(0).setColor(sf::Color::White);
string_team = "Neutral";
}
else if(team == TEAM_ALLY)
{
text_info.at(0).setColor(sf::Color::Green);
string_team = "Ally";
}
else if(team == TEAM_ENEMY)
{
text_info.at(0).setColor(sf::Color::Red);
string_team = "Enemy";
}
std::stringstream stream_health;
stream_health << "Health: " << health;
// Position
calcInfoBoxPosition();
float posX = text_topLeftX;
float posY = text_topLeftY;
// Background
text_background.setFillColor(sf::Color(0, 0, 0, 155));
text_background.setPosition(posX - DEFAULT_TEXT_BOX_PADDING, posY - DEFAULT_TEXT_BOX_PADDING);
text_background.setSize(sf::Vector2f((posX + BOX_SIZE*3.0f + DEFAULT_TEXT_BOX_PADDING) - (posX - DEFAULT_TEXT_BOX_PADDING), (posY + DEFAULT_LINE_HEIGHT * text_info.size() + DEFAULT_TEXT_BOX_PADDING) - (posY - DEFAULT_TEXT_BOX_PADDING)));
// Team
text_info.at(0).setString(string_team);
text_info.at(0).setPosition(posX, posY);
text_info.at(0).setStyle(sf::Text::Bold);
// Type
addBoxInfo(1, stream_type.str());
// Charges
addBoxInfo(2, stream_charges.str());
// Health
addBoxInfo(3, stream_health.str());
}
int Box::addBoxInfoLine()
{
text_info.push_back(sf::Text("", font_default, DEFAULT_TEXT_SIZE));
text_info.at(text_info.size() - 1).setColor(sf::Color::White);
return text_info.size() - 1;
}
void Box::calcInfoBoxPosition()
{
text_topLeftX = body->GetPosition().x * SCALE - BOX_SIZE/0.7f;
text_topLeftY = body->GetPosition().y * SCALE - BOX_SIZE/1.7f - (text_info.size()) * DEFAULT_LINE_HEIGHT - DEFAULT_BOX_INFO_DISTANCE;
}
void Box::notifyContactsOnConversion(bool staticToDynamic)
{
// ------------------------------- Conversion from static to dynamic -------------------------------
if(staticToDynamic)
{
// All dynamic contacts remove this from their static list and push this into the dynamic list
for(unsigned int i = 0; i < contactDynamicList.size(); i++)
{
contactDynamicList.at(i)->contactStaticList.erase(std::remove(contactDynamicList.at(i)->contactStaticList.begin(), contactDynamicList.at(i)->contactStaticList.end(), this), contactDynamicList.at(i)->contactStaticList.end());
contactDynamicList.at(i)->contactDynamicList.push_back(this);
}
// All static contacts remove this from their static list and push this into the dynamic list
for(unsigned int i = 0; i < contactStaticList.size(); i++)
{
contactStaticList.at(i)->contactStaticList.erase(std::remove(contactStaticList.at(i)->contactStaticList.begin(), contactStaticList.at(i)->contactStaticList.end(), this), contactStaticList.at(i)->contactStaticList.end());
contactStaticList.at(i)->contactDynamicList.push_back(this);
}
}
else // ------------------------------- Conversion from dynamic to static -------------------------------
{
// All dynamic contacts remove this from their dynamic list and push this into the static list
for(unsigned int i = 0; i < contactDynamicList.size(); i++)
{
contactDynamicList.at(i)->contactDynamicList.erase(std::remove(contactDynamicList.at(i)->contactDynamicList.begin(), contactDynamicList.at(i)->contactDynamicList.end(), this), contactDynamicList.at(i)->contactDynamicList.end());
contactDynamicList.at(i)->contactStaticList.push_back(this);
}
// All static contacts remove this from their dynamic list and push this into the static list
for(unsigned int i = 0; i < contactStaticList.size(); i++)
{
contactStaticList.at(i)->contactDynamicList.erase(std::remove(contactStaticList.at(i)->contactDynamicList.begin(), contactStaticList.at(i)->contactDynamicList.end(), this), contactStaticList.at(i)->contactDynamicList.end());
contactStaticList.at(i)->contactStaticList.push_back(this);
}
}
}
void Box::setBodyActive(bool active)
{
body->SetActive(active);
updateTexture();
}
Box* Box::getDynamicBoxByCoordinates(int x, int y)
{
for(unsigned int i = 0; i < boxes->size(); i++)
{
float minX = boxes->at(i)->getBody()->GetPosition().x * SCALE - BOX_SIZE / 2.0f;
float maxX = minX + BOX_SIZE;
float minY = boxes->at(i)->getBody()->GetPosition().y * SCALE - BOX_SIZE / 2.0f;
float maxY = minY + BOX_SIZE;
if(x >= minX && x <= maxX && y >= minY && y <= maxY)
{
return boxes->at(i);
}
}
return NULL;
}
void Box::ai()
{
if(stickyMaster == NULL)
{
for(unsigned int i = 0; i < contactDynamicList.size(); i++)
{
if(contactDynamicList.at(i)->type == "floatbox" && contactDynamicList.at(i)->isSticky && type != "floatbox")
{
//moveToTarget(contactDynamicList.at(i)->getBody()->GetPosition().x * SCALE, contactDynamicList.at(i)->getBody()->GetPosition().y * SCALE, 0, false, 4.0f);
stickyMaster = contactDynamicList.at(i);
body->SetGravityScale(0.0f);
}
}
}
if(stickyMaster != NULL)
{
body->SetLinearVelocity(stickyMaster->getBody()->GetLinearVelocity());
float centerX = body->GetPosition().x * SCALE;
float centerY = body->GetPosition().y * SCALE;
float stickyX = stickyMaster->getBody()->GetPosition().x * SCALE;
float stickyY = stickyMaster->getBody()->GetPosition().y * SCALE;
float distX = stickyX - centerX;
float distY = stickyY - centerY;
float dist = sqrt(distX*distX + distY*distY);
if(!stickyMaster->isSticky || dist > BOX_SIZE + 3.0f)
{
body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
body->SetGravityScale(1.0f);
stickyMaster = NULL;
}
}
if((team == TEAM_ENEMY && type != "floatbox") || (team == TEAM_ALLY && type != "floatbox" && type != "chargebox" && !isActive && global_enableAllyAI))
{
if(aiAttackTimer.timeReached())
{
if(aiTarget == NULL)
{
int minX = body->GetPosition().x * SCALE - BOX_SIZE/2 - attackRange;
int minY = body->GetPosition().y * SCALE - BOX_SIZE/2 - attackRange;
int maxX = minX + 2 * attackRange;
int maxY = minY + 2 * attackRange;