forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameKartObject.cpp
executable file
·852 lines (705 loc) · 24.3 KB
/
GameKartObject.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
#include <GL/glfw.h>
#include "GameKartObject.h"
#include "GameSceneryObject.h"
#include "GameUpgradeObject.h"
#include "GamePointObject.h"
#include "Upgrades/GamePartNone.h"
#include "Upgrades/GameActiveNone.h"
#include <cmath>
#include "util.h"
#include <iostream>
using namespace std;
extern SoundManager *g_sound_manager;
extern int g_num_squashes;
const float GameKartObject::tireTurnAngleTime = 1.0/4;
GameKartObject::GameKartObject(const char *fileName) : GamePhysicalObject(fileName) {
hud = new GameHUD();
for (int i = 0; i < 4; i++) {
GameDrawableObject *tire = new GameDrawableObject("models/tire.obj");
wheels.push_back(tire);
}
/* fill each kartSlot with a blank upgrade. simplifies logic and allows
* players to choose no part for a slot */
GamePartUpgrade *part = new GamePartNone();
frontPart = sidePart = backPart = part;
frontParts.push_front(part);
sideParts.push_front(part);
backParts.push_front(part);
activeUpgrades.push_front(new GameActiveNone());
usingController = false;
tireAngle = 0.0;
tireTurnAngle = 0.0;
carPitchAngle = 0.0;
carRollAngle = 0.0;
frontScale = 0.1;
backScale = 0.1;
sideScale = 0.1;
frontScaleChanging = true;
sideScaleChanging = true;
backScaleChanging = true;
frontScaleDir = 1;
sideScaleDir = 1;
backScaleDir = 1;
perspective = 45.0f;
actionOn = false;
points = 0;
winState = 0;
// load sounds
ding_sound = g_sound_manager->getSample("sounds/ding.ogg");
// made in sfxr
activate_part_sound = g_sound_manager->getSample("sounds/select.wav");
deactivate_part_sound = g_sound_manager->getSample("sounds/deselect.wav");
// exaggerated sound
collide_sound = g_sound_manager->getSample("sounds/crash.ogg");
flying_sound = g_sound_manager->getSample("sounds/flying.ogg");
outOfEnergy_sound = g_sound_manager->getSample("sounds/outofenergy.ogg");
// to only play sound once per state change
playedFlyingSound = false;
pausedFlyingSound = true;
playedOutOfEnergy = false;
}
int GameKartObject::getInput(int request) {
switch (request) {
case 0:
return input.up;
case 1:
return input.down;
case 2:
return input.left;
case 3:
return input.right;
case 4:
return input.action;
}
return -1;
}
void GameKartObject::onCollide(GameDrawableObject *other, float dt)
{
//Need some way of telling if PhysicsActor came from upgrade
if (GameUpgradeObject *upgrade = dynamic_cast<GameUpgradeObject *>(other)) {
upgrade->addToKart(this);
points += 50;
}
else if (GameSceneryObject *scenery = dynamic_cast<GameSceneryObject *>(other)) {
vec3 oldPos = getPosition();
//float oldSpeed = getSpeed();
float top = scenery->getHeightAt(oldPos.x, oldPos.z);
float bottom = scenery->getBottomAt(oldPos.x, oldPos.z);
setVelocity(vec3(0));
if (oldPos.y - getRideHeight() < top and oldPos.y + getRideHeight() > bottom) {//and oldPos.y + 1 >= newHeight) {
if (oldPos.y + 20 * dt > top) {
//float angle = vecAngle(scenery->getTopVectorAt(oldPos.x, oldPos.z), vec3(0, 1, 0));
//printf("%s!!!\n", other->getName());
//carPitchAngle = angle;
setPosition(vec3(oldPos.x, top+getRideHeight(), oldPos.z));
setFallSpeed(0);
airborn = false;
//fallSpeed = (oldPos.y-getRideHeight() - top)*abs(getSpeed());
//setSpeed(oldSpeed + (oldSpeed > 0 ? fallSpeed : -fallSpeed));
}
else {
// bounce off
collide_sound->play();
/*vec3 othPos = other->getPosition();
vec3 oldPos = getPosition();
vec3 direction = othPos - oldPos;
direction = normalize(direction);
direction *= (other->getRadius() + getRadius());
float oldSpeed = getSpeed() * 0.1f;*/
//printf("before: %f, %f\n", getSpeed(), getDirection());
vec3 oldVec = getDirectionVector();
//printf("test: %f, %f\n", getDirection(), glm::degrees(glm::atan(-oldVec.z, oldVec.x)));
vec3 sideNorm = scenery->getSideVectorAt(oldPos.x, oldPos.z);
vec3 reflect = glm::reflect(oldVec, sideNorm);
//printf("%f %f | %f %f -> %f %f\n", oldVec.x, oldVec.z, sideNorm.x, sideNorm.z, reflect.x, reflect.z);
setSpeed(-getSpeed()*0.75);
setDirection(glm::degrees(glm::atan(-reflect.z, reflect.x)));
//printf("after: %f, %f\n", getSpeed(), getDirection());
if (absAngleDif(preCollisionDir, getDirection()) > 90) {
setDirection(getDirection()+180);
setSpeed(-getSpeed());
//printf("flip: %f %f\n", getSpeed(), getDirection());
}
//vec3 oldPos = getPosition(), oldVel = sideNorm * (1+abs(preCollisionSpd));
//vec3 oldVel(preCollisionSpd*cos(radians(preCollisionDir)), 0, preCollisionSpd*sin(radians(preCollisionDir)));
vec3 oldVel = -getVelocity();
setPosition(vec3(oldPos.x - oldVel.x*dt, oldPos.y, oldPos.z - oldVel.z*dt));
//setSpeed(-getSpeed() * 0.25f);
}
}
}
else if (GameKartObject *otherKart = dynamic_cast<GameKartObject *>(other)) {
collide_sound->play();
//printf("before: %f, %f\n", getSpeed(), getDirection());
vec3 preCollisionVel(preCollisionSpd*cos(radians(preCollisionDir)), 0, preCollisionSpd*sin(radians(preCollisionDir)));
vec3 othCollisionVel(otherKart->preCollisionSpd*cos(radians(otherKart->preCollisionDir)), 0, otherKart->preCollisionSpd*sin(radians(otherKart->preCollisionDir)));
vec3 resCollisionVel(((othCollisionVel - preCollisionVel)*0.75f + preCollisionVel + othCollisionVel)*0.5f);
setDirection(degrees(atan(resCollisionVel.z, resCollisionVel.x)));
setSpeed(length(resCollisionVel));
/*setDirection(otherKart->preCollisionDir);
setSpeed(otherKart->preCollisionSpd);*/
//printf("after: %f, %f\n", getSpeed(), getDirection());
//printf("%f ", vecAngle(getDirectionVector(), otherKart->getDirectionVector()));
//printf("%f\n", absAngleDif(preCollisionDir, otherKart->preCollisionDir));
if (absAngleDif(preCollisionDir, getDirection()) > 90) {
setDirection(getDirection()+180);
setSpeed(-getSpeed());
//printf("flip: %f\n", getDirection());
}
vec3 oldPos = getPosition(), oldVel = getVelocity();
setPosition(vec3(oldPos.x + oldVel.x*dt, oldPos.y, oldPos.z + oldVel.z*dt));
//printf("%f %f %f\n", oldVel.x, oldVel.y, oldVel.z);
/*vec3 othPos = other->getPosition();
vec3 oldPos = getPosition();
vec3 direction = othPos - oldPos;
direction = normalize(direction);
direction *= (other->getRadius() + getRadius()) * 1.3;
//float oldSpeed = getSpeed() * 0.1f;
setSpeed(-getSpeed() * 0.25f);
setPosition(vec3(othPos.x - direction.x, othPos.y - direction.y, othPos.z - direction.z));*/
}
else if (GamePointObject *point = dynamic_cast<GamePointObject *>(other)) {
// Collided with cuby thing
ding_sound->play();
//other->setName("squashed_thingy");
//other->setScale(vec3(other->getScale().x, 0.02, other->getScale().z));
point->onCollide(this);
if (point->pickUp()) {
points += point->getPoints();
point->scheduleForRemoval();
}
//other->scheduleForRemoval();
}
else {
GamePhysicalObject::onCollide(other, dt);
}
//return true;
}
GameKartObject::~GameKartObject()
{
#ifdef USE_SOUND
delete ding_sound;
delete crash_sound;
delete activate_sound;
delete deactivate_sound;
#endif
cout << "Kart Object Deleted\n";
}
/*void GameKartObject::stop()
{
// cout << "bunnie stop\n";
//stage = STILL;
}*/
void GameKartObject::transform(RenderingHelper &modelViewMatrix) {
GamePhysicalObject::transform(modelViewMatrix);
modelViewMatrix.rotate(carPitchAngle,vec3(0.0,0.0,1.0));
modelViewMatrix.rotate(-carRollAngle,vec3(1.0,0.0,0.0));
}
void GameKartObject::draw(PhongShader *meshShader, RenderingHelper modelViewMatrix, float levelOfDetail)
{
tireAngle+=(getSpeed()/2.0);
//rot.x = carRollAngle;
//rot.z = carPitchAngle;
//modelViewMatrix.rotate(carRollAngle,vec3(1.0,0.0,0.0));
//GameDrawableObject::drawSpecial(meshShader, modelViewMatrix,carPitchAngle,carRollAngle);
GameDrawableObject::draw(meshShader, modelViewMatrix, levelOfDetail);
modelViewMatrix.pushMatrix();
modelViewMatrix.translate(getPosition());
modelViewMatrix.scale(0.5,0.5,0.5);
//modelViewMatrix.pushMatrix();
//meshShader->use();
//modelViewMatrix.translate(position());
//modelViewMatrix.scale(scl.x, scl.y, scl.z);
modelViewMatrix.rotate(rot.x, vec3(1.0, 0.0, 0.0));
modelViewMatrix.rotate(rot.y, vec3(0.0, 1.0, 0.0));
modelViewMatrix.rotate(rot.z, vec3(0.0, 0.0, 1.0));
modelViewMatrix.rotate(carPitchAngle,vec3(0.0,0.0,1.0));
modelViewMatrix.rotate(-carRollAngle,vec3(1.0,0.0,0.0));
//meshShader->setModelMatrix(modelViewMatrix.getMatrix());
//glBindVertexArray(vertexArray);
//GLuint h_aPos = meshShader->getPosLocation();
/*safe_glEnableVertexAttribArray(h_aPos);
glBindBuffer(GL_ARRAY_BUFFER, meshStorage.vertexBuffer);
safe_glVertexAttribPointer(h_aPos, 3, GL_FLOAT, GL_FALSE, 0, 0);
for (int i = 0; i < meshStorage.numMeshes; i++) {
//printf("We are drawing, right? %d\n", indexBufferLength[i]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshStorage.indexBuffer[i]);
glDrawElements(GL_TRIANGLES, meshStorage.indexBufferLength[i], GL_UNSIGNED_SHORT, 0);
}*/
//safe_glDisableVertexAttribArray(h_aPos);
//glBindVertexArray(0);
//modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.translate(glm::vec3(-2.2,0.0,-2.2));
modelViewMatrix.rotate(90.0,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireAngle,vec3(1.0,0.0,0.0));
modelViewMatrix.scale(5.0,6.0,6.0);
wheels[0]->draw(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.translate(glm::vec3(-2.2,0.0,2.2));
modelViewMatrix.rotate(90.0,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireAngle,vec3(1.0,0.0,0.0));
modelViewMatrix.scale(5.0,6.0,6.0);
wheels[1]->draw(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.translate(glm::vec3(2.2,0.0,-2.2));
modelViewMatrix.rotate(90.0,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireTurnAngle,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireAngle,vec3(1.0,0.0,0.0));
modelViewMatrix.scale(5.0,6.0,6.0);
wheels[2]->draw(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.translate(glm::vec3(2.2,0.0,2.2));
modelViewMatrix.rotate(90.0,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireTurnAngle,vec3(0.0,1.0,0.0));
modelViewMatrix.rotate(tireAngle,vec3(1.0,0.0,0.0));
modelViewMatrix.scale(5.0,6.0,6.0);
wheels[3]->draw(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
// draw parts
modelViewMatrix.pushMatrix();
modelViewMatrix.scale(1.0 * frontScale,1.0*frontScale,1.0*frontScale);
modelViewMatrix.translate(glm::vec3(2.0,0.0,0.0));
frontParts.front()->drawOnKart(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.scale(1.0 * sideScale,1.0*sideScale,1.0*sideScale);
modelViewMatrix.translate(glm::vec3(0.0,0.0,0.0));
sideParts.front()->drawOnKart(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
modelViewMatrix.pushMatrix();
modelViewMatrix.scale(1.0 * backScale,1.0*backScale,1.0*backScale);
modelViewMatrix.translate(glm::vec3(-2.0,0.0,0.0));
backParts.front()->drawOnKart(meshShader,modelViewMatrix, levelOfDetail);
modelViewMatrix.popMatrix();
//draw action effect
if (actionOn) {
activeUpgrades.front()->drawEffect(meshShader, modelViewMatrix);
}
modelViewMatrix.popMatrix();
//Draws Wheels and Upgrades More efficiently *****
/*for (int i = 0; i < (int)wheels.size(); i++) {
modelViewMatrix.pushMatrix();
wheels[i]->draw(meshShader,modelViewMatrix);
modelViewMatrix.popMatrix();
}
for (int i = 0; i < (int)upgrades.size(); i++) {
modelViewMatrix.pushMatrix();
upgrades[i]->draw(meshShader,modelViewMatrix);
modelViewMatrix.popMatrix();
}*/
}
void GameKartObject::setHUDColor(vec3 color)
{
hud->setColor(color);
}
void GameKartObject::drawHUD(float dt, bool rushed) {
hud->prepareShader();
hud->drawSpeed(getSpeed());
hud->drawTimer(dt, rushed);
hud->drawEnergy(getMaxEnergy(), getEnergy(), activeUpgrades.front()->getName());
hud->drawScore(getPoints());
if (winState == 1) {
hud->drawWin();
} else if (winState == -1) {
hud->drawLose();
}
}
void GameKartObject::changePartScale(int part,int dir, float dt)
{
float change = 0.0;
if(dir == 1)
{
change = 4.0 * dt;
}
else
if(dir == 0)
{
change = -3.0 * dt;
}
if(part == 0)
{
if(dir == 1 && frontScale < 1.0)
{frontScale+=change;}
if(dir == 0 && frontScale >0)
{frontScale+=change;}
}
if(part == 1)
{
if(dir == 1 && sideScale < 1.0)
{sideScale+=change;}
if(dir == 0 && sideScale >0)
{sideScale+=change;}
}
if(part == 2)
{
if(dir == 1 && backScale < 1.0)
{backScale+=change;}
if(dir == 0 && backScale >0)
{backScale+=change;}
}
}
void GameKartObject::changeTireTurnAngle(float dt, float mult, float speedDampedTurnAngle)
{
float targetAngle = mult*speedDampedTurnAngle;
if (tireTurnAngle < targetAngle) {
tireTurnAngle += dt * (1 + speedDampedTurnAngle)/tireTurnAngleTime;
if (tireTurnAngle > targetAngle)
tireTurnAngle = targetAngle;
}
else if (tireTurnAngle > targetAngle) {
tireTurnAngle -= dt * (1 + speedDampedTurnAngle)/tireTurnAngleTime;
if (tireTurnAngle < targetAngle)
tireTurnAngle = targetAngle;
}
}
void GameKartObject::changeKartPitchAngle(float dt, float pitchAngle)
{
float targetAngle = pitchAngle;
if(carPitchAngle < targetAngle)
{
carPitchAngle += dt * 10.0;
if (carPitchAngle > targetAngle)
carPitchAngle = targetAngle;
}
else if (carPitchAngle > targetAngle)
{
carPitchAngle -= dt * 10.0;
if (carPitchAngle < targetAngle)
carPitchAngle = targetAngle;
}
}
void GameKartObject::changeKartRollAngle(float dt, float mult, float speedDampedRollAngle)
{
float targetAngle = mult*speedDampedRollAngle;
if(carRollAngle < targetAngle)
{
carRollAngle += dt * (1 + speedDampedRollAngle)/tireTurnAngleTime;;
if (carRollAngle > targetAngle)
carRollAngle = targetAngle;
}
else if (carRollAngle > targetAngle)
{
carRollAngle -= dt * (1 + speedDampedRollAngle)/tireTurnAngleTime;;
if (carRollAngle < targetAngle)
carRollAngle = targetAngle;
}
}
void GameKartObject::addPartToList(list<GamePartUpgrade *> &list, GamePartUpgrade *part)
{
list.front()->cycleStatOff(&properties);
list.push_front(part);
part->cycleStatOn(&properties);
}
void GameKartObject::cyclePartList(list<GamePartUpgrade *> &list)
{
list.front()->cycleStatOff(&properties);
list.push_back(list.front());
list.pop_front();
list.front()->cycleStatOn(&properties);
if (dynamic_cast<GamePartNone *>(list.front())) {
//deactivate_part_sound->play();
}
}
void GameKartObject::addActive(GameActiveUpgrade *active)
{
if (actionOn) {
activeUpgrades.front()->activeEnd(this);
actionOn = false;
}
activeUpgrades.push_front(active);
}
void GameKartObject::cycleActives()
{
if (actionOn) {
activeUpgrades.front()->activeEnd(this);
actionOn = false;
}
activeUpgrades.push_back(activeUpgrades.front());
activeUpgrades.pop_front();
// empty or switch sound
if (dynamic_cast<GameActiveNone *>(activeUpgrades.front())) {
deactivate_part_sound->play();
} else {
activate_part_sound->play();
}
}
void GameKartObject::win()
{
winState = 1;
}
void GameKartObject::lose()
{
winState = -1;
}
void GameKartObject::reset()
{
winState = 0;
}
void GameKartObject::update(float dt)
{
float oldSpeed = getSpeed(), newSpeed;
float oldDirection = getDirection();
float targetPerspective = 45.0f + getSpeed();
//perspective += getSpeed() / properties.getTopSpeed() - 0.5f;
if (targetPerspective > 90.0f) {
targetPerspective = 90.0f;
} else if (targetPerspective < 45.0f) {
targetPerspective = 45.0f;
}
if (perspective < targetPerspective) {
perspective += 90.0f*dt;
if (perspective > targetPerspective) {
perspective = targetPerspective;
}
} else if (perspective > targetPerspective) {
perspective -= 90.0*dt;
if (perspective < targetPerspective) {
perspective = targetPerspective;
}
}
frontParts.front()->partUpdate(this, dt);
sideParts.front()->partUpdate(this, dt);
backParts.front()->partUpdate(this, dt);
if (!isAirborn()) {
float speedDampedTurnAngle = properties.getTurnSpeed() * (1 - std::min(1.0f, abs(getSpeed())/properties.getTurnSpeed()));
if (joystickState[0] < 0.0) {
changeTireTurnAngle(dt, -1, speedDampedTurnAngle);
//changeKartRollAngle(dt,-25.0);
} else if(joystickState[0] > 0.0) {
changeTireTurnAngle(dt, 1, speedDampedTurnAngle);
//changeKartRollAngle(dt,25.0);
} else if (joystickState[0] == 0.0){
changeTireTurnAngle(dt, 0, speedDampedTurnAngle);
//changeKartRollAngle(dt,0);
}
float directionMult = dt*tireTurnAngle * getSpeed()/M_PI;
setDirection(oldDirection+directionMult);
}
setRotation(vec3(0, getDirection(), 0 ));
if(joystickState[3] > 0.0 && oldSpeed < properties.getTopSpeed() && !isAirborn()) {
short speedUp = (oldSpeed > 0.0) ? properties.getAcceleration() : properties.getBrakeSpeed();
//changeKartPitchAngle(dt,25.0);
newSpeed = oldSpeed + (speedUp * dt);
if (newSpeed > properties.getTopSpeed()){
newSpeed = properties.getTopSpeed();}
} else if(joystickState[3] < 0.0 && !isAirborn()) {
short speedDown = (oldSpeed < 0.0) ? properties.getAcceleration() : properties.getBrakeSpeed();
//changeKartPitchAngle(dt,-25.0);
newSpeed = oldSpeed - (speedDown * dt);
if (newSpeed < -properties.getTopSpeed())
newSpeed = -properties.getTopSpeed();
} else {
if (abs(oldSpeed) > friction * dt)
newSpeed = oldSpeed - (sign(oldSpeed) * friction * dt);
else {
newSpeed = 0;
//changeKartPitchAngle(dt, 0.0);
//changeKartRollAngle(dt,0.0);
}
}
setSpeed(newSpeed);
if (buttonState[1] == GLFW_PRESS) { //inputMap.cycleActive
if (!buttonDown[1]) {
cycleActives();
buttonDown[1] = true;
}
}
else {
buttonDown[1] = false;
}
if (buttonState[2] == GLFW_PRESS) { //inputMap.cycleFront
if (!buttonDown[2]) {
frontScaleChanging = true;
if(frontScaleDir == 0)
frontScale=0.9;
else
frontScale = 0.1;
//cycleFrontParts();
buttonDown[2] = true;
}
}
else {
buttonDown[2] = false;
}
if (buttonState[3] == GLFW_PRESS) { //inputMap.cycleSide
if (!buttonDown[3]) {
sideScaleChanging = true;
if(sideScaleDir == 0)
sideScale=0.9;
else
sideScale = 0.1;
//cycleSideParts();
buttonDown[3] = true;
}
}
else {
buttonDown[3] = false;
}
if (buttonState[4] == GLFW_PRESS) { //inputMap.cycleBack
if (!buttonDown[4]) {
backScaleChanging = true;
if(backScaleDir == 0)
backScale=0.9;
else
backScale = 0.1;
//cycleBackParts();
buttonDown[4] = true;
}
}
else {
buttonDown[4] = false;
}
// be optimistic
bool notEnoughEnergy = false;
if (buttonState[0] == GLFW_PRESS) { //inputMap.action
if (!buttonDown[0]) {
buttonDown[0] = true;
notEnoughEnergy = !activeUpgrades.front()->activeStart(this);
if (notEnoughEnergy) {
properties.regenEnergy(dt);
}
else { // !notEnoughEnergy
actionOn = true;
}
}
else { // buttonDown[0]
if (actionOn) {
notEnoughEnergy = !activeUpgrades.front()->activeUpdate(this, dt);
if (notEnoughEnergy) {
activeUpgrades.front()->activeEnd(this);
actionOn = false;
}
else { // !notEnoughEnergy
}
}
else { // !actionOn
properties.regenEnergy(dt);
}
}
}
else { // buttonState[0] == GLFW_RELEASE
if (buttonDown[0]) {
buttonDown[0] = false;
// could play sound again after releasing action key
playedOutOfEnergy = false;
if (actionOn) {
activeUpgrades.front()->activeEnd(this);
actionOn = false;
}
else { // !actionOn
properties.regenEnergy(dt);
}
}
else { // !buttonDown[0]
properties.regenEnergy(dt);
}
}
if (notEnoughEnergy && !playedOutOfEnergy) {
outOfEnergy_sound->play();
playedOutOfEnergy = true;
}
// to only play/puse once per state change
//static int i = 0;
// is flying?
// from GamePhysicalObject::update()
if (isAirborn()) {
/*if(getFallSpeed() < 0)
changeKartPitchAngle(dt,10.0);
else if (getFallSpeed() > 0)
changeKartPitchAngle(dt,-10.0);
else*/
changeKartPitchAngle(dt,-getFallSpeed());
/*if(joystickState[0] > 0.0)
changeKartRollAngle(dt,25.0);
else if(joystickState[0] < 0.0)
changeKartRollAngle(dt,-25.0);
else
changeKartRollAngle(dt,0.0);*/
if (joystickState[0] == 0.0) {
changeKartRollAngle(dt, 0, properties.getTurnSpeed());
}
// flying_sound->resume();
if (!playedFlyingSound) {
// printf("play %d\n", i++);
flying_sound->fadeIn(1000, -1); // fadin and loop forever
playedFlyingSound = true;
pausedFlyingSound = false;
}
} else {
carPitchAngle = 0;
carRollAngle = 0;
//changeKartPitchAngle(dt,0.0);
//changeKartRollAngle(dt,0.0);
if (!pausedFlyingSound) {
// printf("paused %d\n", i);
flying_sound->fadeOut(1000);
pausedFlyingSound = true;
playedFlyingSound = false;
}
}
// do this last
airborn = true;
GamePhysicalObject::update(dt); //actually move the cart with these updated values
if(frontScaleChanging == true)
{
changePartScale(0,frontScaleDir, dt);
}
if(sideScaleChanging == true)
{
changePartScale(1,sideScaleDir, dt);
}
if(backScaleChanging == true)
{
changePartScale(2,backScaleDir, dt);
}
if(frontScale >=1.0)
{
frontScaleChanging = false;
frontScaleDir = 0;
}
if(frontScale <= 0.0)
{
frontScaleChanging = true;
if (frontPartAdding == 0)
{cycleFrontParts();}
else
{addPartToList(frontParts, frontPart);
frontPartAdding = 0;}
frontScaleDir = 1;
}
if(sideScale >=1.0)
{
sideScaleChanging = false;
sideScaleDir = 0;
}
if(sideScale <= 0.0)
{
sideScaleChanging = true;
if(sidePartAdding == 0)
{cycleSideParts();}
else
{addPartToList(sideParts,sidePart);
sidePartAdding = 0;}
sideScaleDir = 1;
}
if(backScale >=1.0)
{
backScaleChanging = false;
backScaleDir = 0;
}
if(backScale <= 0.0)
{
backScaleChanging = true;
if (backPartAdding == 0){
cycleBackParts();}
else
{addPartToList(backParts,backPart);
backPartAdding = 0;}
backScaleDir = 1;
}
//printf("%f %d\n", backScale, backScaleDir);
}