-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFunctions.h
592 lines (527 loc) · 16.4 KB
/
MainFunctions.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
#ifndef START
#include "Graphics.h"
#include "Audio.h"
#include <stdio.h>
#include <Math.h>
#include "ColGen.h"
#include "Variables.h"
#include "CoreFunctions.h"
#endif
bool getWrongDir1(float x, float y, float rotation)
{
while (rotation < 0)
rotation += 2*Pi;
float min_rotation = fmod(rotation*1.0,2*Pi);
if (_gamescreen == 1)
{
if (x >= 301 && x <= 3985 && y >= 2200 && y <= 2593 && (min_rotation >= 5*Pi/3 || min_rotation <= Pi/3))
return 1;
if (x >= 301 && x <= 3985 && y >= 685 && y <= 1064 && (min_rotation >= 2*Pi/3 && min_rotation <= 4*Pi/3))
return 1;
if (x >= 301 && x <= 989 && y >= 1064 && y <= 2200 && (min_rotation >= Pi/6 && min_rotation <= 5*Pi/6))
return 1;
if (x >= 3308 && x <= 3985 && y >= 1064 && y <= 2200 && (min_rotation >= 7*Pi/6 && min_rotation <= 11*Pi/6))
return 1;
}
return 0;
}
void lapRefresh(Carro* carro)
{
if ((*carro).laps >= 1 && (*carro).laps <= 3)
{
if ((*carro).lapTime[_gamescreen-1][(*carro).laps-1] == 0)
{
if ((*carro).laps >= 2)
(*carro).lapTime[_gamescreen-1][(*carro).laps-1] = _gtimer-(*carro).lapTime[_gamescreen-1][(*carro).laps-1];
else
(*carro).lapTime[_gamescreen-1][(*carro).laps-1] = _gtimer;
}
}
}
void controlLaps(Carro* carro)
{
if (_gamescreen == 1)
{
if ((*carro).x >= 3308 && (*carro).x <= 3985 && (*carro).oldFrontY < 1620 && (*carro).y+sin((*carro).rotation)*(*carro).halfwidth >= 1620)
{
(*carro).laps += 1;
lapRefresh(carro);
}
else if ((*carro).x >= 3308 && (*carro).x <= 3985 && (*carro).oldFrontY > 1620 && (*carro).y+sin((*carro).rotation)*(*carro).halfwidth <= 1620)
(*carro).laps -= 1;
}
}
Position PosInScreen(float px, float py)
{
Position p;
if (_gamescreen == 1)
{
p.x = (px-player.x+831)*_xscale;//831- 835
p.y = (py-player.y+559)*_yscale;//559- 519
}
else if (_gamescreen == 2)
{
p.x = (px-player.x+418+100)*_xscale;//418- 422
p.y = (py-player.y+234+100)*_yscale;//234- 274
}
return p;
}
float cdistance(Position a, Position b){
return sqrt(pow(b.y - a.y,2) + pow(b.x - a.x,2)) ;
}
Position newPosition(Position p, float angle, float dist){
//angle = (angle +90) * PI / 180.0;
float _sin = sin(angle);
float _cos = cos(angle);
Position newP;
newP.x = (dist * _cos) + p.x;
newP.y = (dist * _sin) + p.y;
return newP;
}
float getAngle(Position p1, Position p2) //Devolverá ângulo em radianos entre p1 e p2 considerando o círculo trigonométrico, com p1 na origem e p2 na circunferência
{
float firstquadrantangle = atan2(abs(p2.y - p1.y), abs(p2.x - p1.x));
if (p2.x - p1.x >= 0) //Se p1 for a origem, p2 estará no:
{
if (p2.y - p1.y >= 0)
return firstquadrantangle; //primeiro quadrante
else
return 2*Pi-firstquadrantangle; //quarto quadrante
}
else
{
if (p2.y - p1.y >= 0)
return Pi-firstquadrantangle; //segundo quadrante
else
return Pi+firstquadrantangle; //terceiro quadrante
}
}
int getMinorAngleDiffDirection(float angle1, float angle2) //Devolverá 1 se o ângulo1 for mais próximo do ângulo2 pela esquerda e devolverá -1 se for mais próximo pela direita
{
if (angle1 >= angle2)
{
if (angle1-angle2 < Pi)
return -1;
else
return 1;
}
else
{
if (angle2-angle1 < Pi)
return 1;
else
return -1;
}
}
float getMinorAngleDiff(float angle1, float angle2) //Devolverá o menor ângulo entre dois ângulos
{
int teste = getMinorAngleDiffDirection(angle1, angle2);
if (angle1 >= angle2)
{
if (teste == 1)
return angle2+2*Pi-angle1;
else
return angle1-angle2;
}
else
{
if (teste == 1)
return angle2-angle1;
else
return angle1+2*Pi-angle2;
}
}
int Colisao_Carro_Fase1(float pos_x, float pos_y)
{
int pixelX = (int) pos_x;
int pixelY = (int) pos_y;
char c = 'P';
if (pixelX >= 0 && pixelX < 4266 && pixelY >= 0 && pixelY < 3200)
c = map1_col[3199-pixelY][pixelX];
//c = 'B';
return (c == 'B'? 1 : (c == 'V' ? 2 : 0));
}
int Colisao_Carro_Fase2(float pos_x, float pos_y)
{
int pixelX = (int) pos_x;
int pixelY = (int) pos_y;
char c = 'P';
if (pixelX >= 0 && pixelX < 4000 && pixelY >= 0 && pixelY < 3000)
c = map2_col[2999-pixelY][pixelX];
//c = 'B';
return (c == 'B'? 1 : (c == 'V' ? 2 : 0));
}
int Colisao_Carro_Fase(float pos_x, float pos_y)
{
int a = 1;
if (_gamescreen == 1)
a = Colisao_Carro_Fase1(pos_x, pos_y);
else if (_gamescreen == 2)
a = Colisao_Carro_Fase2(pos_x, pos_y);
return a;
}
float saveTargetDistance = 0;
void GameAI()
{
Carro* carroIA = &inimigo1;
if (_debug_mode)
{
graphics.SetColor(255,0,0);
if (_gamescreen == 1)
{
for(int i = 0; i < maxPoint; i++)
{
int px = points[i].x;
int py = points[i].y;
Position p = PosInScreen(px, py);
graphics.FillCircle2D(p.x, p.y, 5, 5);
}
}
else if (_gamescreen == 2)
{
for(int i = 0; i < maxPoint; i++)
{
int px = points2[i].x;
int py = points2[i].y;
Position p = PosInScreen(px, py);
graphics.FillCircle2D(p.x, p.y, 5, 5);
}
}
}
Position target;
if (_gamescreen == 1)
target = points[nextPoint];
else if (_gamescreen == 2)
target = points2[nextPoint];
Position carroPos;
carroPos.x = (*carroIA).x;
carroPos.y = (*carroIA).y;
float targetDistance = cdistance(target, carroPos);
if (saveTargetDistance == 0)
saveTargetDistance = targetDistance;
Position pointing_at = newPosition(carroPos, (*carroIA).rotation, targetDistance);
if (_debug_mode)
{
Position pos1 = PosInScreen(target.x, target.y);
graphics.DrawCircle2D(pos1.x, pos1.y, 40);
Position pos2 = PosInScreen(pointing_at.x, pointing_at.y);
graphics.DrawCircle2D(pos2.x, pos2.y, 30);
}
float minor_rotation = fmod((*carroIA).rotation*1.0,2.0*Pi);
float minor_ideal_angle = getAngle(carroPos, target);
if(_auto)
{
if (getMinorAngleDiffDirection(minor_rotation, minor_ideal_angle) == 1)
{
float anglediff = getMinorAngleDiff(minor_rotation, minor_ideal_angle);
if (anglediff < Pi/180)
(*carroIA).direction = Reto;
else
(*carroIA).direction = Esquerda;
}
else if (getMinorAngleDiffDirection(minor_rotation, minor_ideal_angle) == -1)
{
float anglediff = getMinorAngleDiff(minor_rotation, minor_ideal_angle);
if (anglediff < Pi/180)
(*carroIA).direction = Reto;
else
(*carroIA).direction = Direita;
}
if (targetDistance < 30)
{
nextPoint++;
if (_gamescreen == 1)
saveTargetDistance = cdistance(carroPos, points[nextPoint]);
else if (_gamescreen == 2)
saveTargetDistance = cdistance(carroPos, points2[nextPoint]);
if (nextPoint >= maxPoint)
{
nextPoint = 0;
}
}
else
{
float ideal_speed = saveTargetDistance/80;
if (ideal_speed > (*carroIA).maxspeed/6.4 && (*carroIA).direction != Reto)
ideal_speed = (*carroIA).maxspeed/6.4;
if (_gamescreen == 1 && (*carroIA).laps >= 3)
ideal_speed = 0;
if ((*carroIA).speed < ideal_speed)
(*carroIA).aceleration = Acelerando;
else if ((*carroIA).speed > ideal_speed)
(*carroIA).aceleration = Re;
else
(*carroIA).aceleration = Parado;
}
}
}
void UpdateCar(float timer, Carro* carro)
{
float new_pos_x = (*carro).x;
float new_pos_y = (*carro).y;
(*carro).oldFrontX = (*carro).x+cos((*carro).rotation)*(*carro).halfwidth;
(*carro).oldFrontY = (*carro).y+sin((*carro).rotation)*(*carro).halfwidth;
new_pos_x += cos((*carro).rotation)*(*carro).speed;
new_pos_y += sin((*carro).rotation)*(*carro).speed;
int col_status = Colisao_Carro_Fase(new_pos_x+cos((*carro).rotation)*(*carro).halfwidth, new_pos_y+sin((*carro).rotation)*(*carro).halfwidth);
if ((*carro).speed < 0)
col_status = Colisao_Carro_Fase(new_pos_x-cos((*carro).rotation)*(*carro).halfwidth, new_pos_y-sin((*carro).rotation)*(*carro).halfwidth);
if(col_status == 1){
(*carro).x = new_pos_x;
(*carro).y = new_pos_y;
}
else if(col_status == 2){
(*carro).speed-=0.01*(*carro).speed;
(*carro).x = new_pos_x;
(*carro).y = new_pos_y;
}
else
{
(*carro).speed -= (*carro).speed/10;
}
if ((*carro).aceleration == Acelerando)
{
if ((*carro).speed < 0)
(*carro).speed += 4*timer;
else
(*carro).speed += (*carro).engine*timer/15;
}
else if ((*carro).aceleration == Parado && (*carro).speed > 0)
{
(*carro).speed -= 2*timer;
if ((*carro).speed < 0)
(*carro).speed = 0;
}
else if ((*carro).aceleration == Parado && (*carro).speed < 0)
{
(*carro).speed += 2*timer;
if ((*carro).speed > 0)
(*carro).speed = 0;
}
else if ((*carro).aceleration == Re)
{
if ((*carro).speed > 0)
(*carro).speed -= 4*timer;
else
(*carro).speed -= (*carro).engine*timer/20;
}
if ((*carro).direction == Esquerda)
{
float newrot = (*carro).rotation;
if ((*carro).speed > 0)
newrot += (*carro).handling*timer*(-6.4*pow((*carro).speed,2)/(*carro).maxspeed+(*carro).speed)/(13+pow((*carro).maxspeed/30,3));
else if ((*carro).speed < 0)
newrot -= (*carro).handling*timer*(-6.4*pow((*carro).speed+((*carro).maxspeed/6.4),2)/(*carro).maxspeed+(*carro).speed+((*carro).maxspeed/6.4))/(13+pow((*carro).maxspeed/30,3));
if ((Colisao_Carro_Fase((*carro).x+cos(newrot)*(*carro).halfwidth,(*carro).y+sin(newrot)*(*carro).halfwidth) != 0) && (Colisao_Carro_Fase((*carro).x-cos(newrot)*(*carro).halfwidth,(*carro).y-sin(newrot)*(*carro).halfwidth) != 0))
(*carro).rotation = newrot;
}
else if ((*carro).direction == Direita)
{
if ((*carro).speed > 0)
(*carro).rotation -= (*carro).handling*timer*(-6.4*pow((*carro).speed,2)/(*carro).maxspeed+(*carro).speed)/(13+pow((*carro).maxspeed/30,3));
else if ((*carro).speed < 0)
(*carro).rotation += (*carro).handling*timer*(-6.4*pow((*carro).speed+((*carro).maxspeed/6.4),2)/(*carro).maxspeed+(*carro).speed+((*carro).maxspeed/6.4))/(13+pow((*carro).maxspeed/30,3));
}
if ((*carro).speed > (*carro).maxspeed/10)
(*carro).speed = (*carro).maxspeed/10;
else if ((*carro).speed < -(*carro).maxspeed/15)
(*carro).speed = -(*carro).maxspeed/15;
controlLaps(carro);
}
void UpdateAI(float timer, Carro* carro)
{
float new_pos_x = (*carro).x;
float new_pos_y = (*carro).y;
(*carro).oldFrontX = (*carro).x+cos((*carro).rotation)*(*carro).halfwidth;
(*carro).oldFrontY = (*carro).y+sin((*carro).rotation)*(*carro).halfwidth;
new_pos_x += cos((*carro).rotation)*(*carro).speed;
new_pos_y += sin((*carro).rotation)*(*carro).speed;
//int col_status = Colisao_Carro_Fase(new_pos_x, new_pos_y);
int col_status = 1;
if (col_status == 1)
{
(*carro).x = new_pos_x;
(*carro).y = new_pos_y;
}
else if (col_status == 2)
{
(*carro).speed -= 0.01*(*carro).speed;
(*carro).x = new_pos_x;
(*carro).y = new_pos_y;
}
else
{
// (*carro).speed-=(*carro).speed;
}
if ((*carro).aceleration == Acelerando)
{
if ((*carro).speed < 0)
(*carro).speed += 4*timer;
else
(*carro).speed += (*carro).engine*timer/15;
}
else if ((*carro).aceleration == Parado && (*carro).speed > 0)
{
(*carro).speed -= 2*timer;
if ((*carro).speed < 0)
(*carro).speed = 0;
}
else if ((*carro).aceleration == Parado && (*carro).speed < 0)
{
(*carro).speed += 2*timer;
if ((*carro).speed > 0)
(*carro).speed = 0;
}
else if ((*carro).aceleration == Re)
{
if ((*carro).speed > 0)
(*carro).speed -= 4*timer;
else
(*carro).speed -= (*carro).engine*timer/20;
}
if ((*carro).direction == Esquerda)
{
if ((*carro).speed > 0)
(*carro).rotation += (*carro).handling*timer*(-6.4*pow((*carro).speed,2)/(*carro).maxspeed+(*carro).speed)/(13+pow((*carro).maxspeed/30,3));
else if ((*carro).speed < 0)
(*carro).rotation -= (*carro).handling*timer*(-6.4*pow((*carro).speed+((*carro).maxspeed/6.4),2)/(*carro).maxspeed+(*carro).speed+((*carro).maxspeed/6.4))/(13+pow((*carro).maxspeed/30,3));
}
else if ((*carro).direction == Direita)
{
if ((*carro).speed > 0)
(*carro).rotation -= (*carro).handling*timer*(-6.4*pow((*carro).speed,2)/(*carro).maxspeed+(*carro).speed)/(13+pow((*carro).maxspeed/30,3));
else if ((*carro).speed < 0)
(*carro).rotation += (*carro).handling*timer*(-6.4*pow((*carro).speed+((*carro).maxspeed/6.4),2)/(*carro).maxspeed+(*carro).speed+((*carro).maxspeed/6.4))/(13+pow((*carro).maxspeed/30,3));
}
if ((*carro).speed > (*carro).maxspeed/10)
(*carro).speed = (*carro).maxspeed/10;
else if ((*carro).speed < -(*carro).maxspeed/15)
(*carro).speed = -(*carro).maxspeed/15;
controlLaps(carro);
}
////////////////////////Draw:
void Draw()
{
float xscale = 1024 / 1432.0;
float yscale = 768 / 1075.0;
if (_gamescreen == -2)
{
/*<index>*/
graphics.DrawImage2D(0, 0, 1024,768, png_fundo);
graphics.DrawText2D(15,570,"Tempo do Jogador em cada pista:");
graphics.DrawText2D(15,550,"Pista 1:");
graphics.DrawText2D(15,530,"Primeira volta: %.2f segundos", player.lapTime[0][0]);
graphics.DrawText2D(15,510,"Segunda volta: %.2f segundos", player.lapTime[0][1]);
graphics.DrawText2D(15,490,"Terceira volta: %.2f segundos", player.lapTime[0][2]);
graphics.DrawText2D(15,470,"Pista 2:");
graphics.DrawText2D(15,450,"Primeira volta: %.2f segundos", player.lapTime[1][0]);
graphics.DrawText2D(15,430,"Segunda volta: %.2f segundos", player.lapTime[1][1]);
graphics.DrawText2D(15,410,"Terceira volta: %.2f segundos", player.lapTime[1][2]);
/*</index>*/
}
else if (_gamescreen == 0)
{
/*<index>*/
graphics.DrawImage2D(0, 0, 1024,768, png_fundo);
graphics.DrawImage2D(70, 470, 224, 80, png_botao_jogar);
graphics.DrawImage2D(70, 380, 224, 80, png_botao_placar);
/*</index>*/
}
else if (_gamescreen == 1)
{
graphics.DrawImage2D(0, 0, 1024, 768, -835+player.x, -519-player.y, 1433.6, 1075.2, png_pista1);
//graphics.DrawImage2D(0, 0, 1024, 768, -838+player.x, -595-player.y, 1433.6, 1075.2, png_pista1);
//graphics.DrawImage2D(0, 0, 1024, 768, -860+player.x, -519-player.y, 1433.6, 1075.2, png_pista1);
graphics.SetColor(0,200,200);
_wrongdir = getWrongDir1(player.x,player.y,player.rotation);
if (_wrongdir)
graphics.DrawText2D(450,690,"Sentido Inverso!");
}
else if (_gamescreen == 2)
{
graphics.DrawImage2D(0, 0, 1024, 768, -422+player.x, -274-player.y, 731.53, 568.57, png_pista2);
}
else if (_gamescreen == 3)
{
graphics.DrawImage2D(0, 0, 1024, 768, -425+player.x, -273-player.y, 731.53, 568.57, png_pista3);
}
if (_gamescreen != 0 && _gamescreen != -1 && _gamescreen != -2)
{
graphics.SetColor(200,40,40);
Position inimigo1_pos = PosInScreen(inimigo1.x-131, inimigo1.y-141);//131
graphics.RotateBegin(-90+inimigo1.rotation*180/Pi);
graphics.DrawImage2D(inimigo1_pos.x, inimigo1_pos.y, 200, 200, png_inimigo1);
graphics.RotateEnd();
//graphics.FillCircle2D(inimigo1_pos.x+131*_xscale, inimigo1_pos.y+131*_yscale, 5, 10);
graphics.RotateBegin(-90+player.rotation*180/Pi);
graphics.DrawImage2D(495, 300, 200, 200, png_carro1);
graphics.RotateEnd();
//graphics.FillCircle2D(594, 400, 5, 10);
graphics.DrawText2D(15,710,"Velocidade: %.0fkm/h",abs(player.speed*35/2));
//graphics.DrawText2D(15,690,"Velocidade inimigo: %.0fkm/h",abs(inimigo1.speed*35/2));
graphics.DrawText2D(15,660,"seu x: %f", player.x);
graphics.DrawText2D(15,640,"seu y: %f", player.y);
//graphics.DrawText2D(15,620,"tecla: %d", teste);
//graphics.DrawText2D(15,620,"inimigo x: %f", inimigo1.x);
//graphics.DrawText2D(15,600,"inimigo y: %f", inimigo1.y);
if (player.laps >= 0)
graphics.DrawText2D(15,690,"Laps: %d", player.laps);
else
graphics.DrawText2D(15,690,"Laps: 0");
int secondconvert = (int) _gtimer;
int seconds = fmod(secondconvert*1.0,60);
int minutes = (int) _gtimer/60;
if (seconds >= 10)
graphics.DrawText2D(15,670,"Tempo: 0%d:%d", minutes, seconds);
else
graphics.DrawText2D(15,670,"Tempo: 0%d:0%d", minutes, seconds);
//graphics.DrawText2D(15,560,"laps inimigo: %d", inimigo1.laps);
}
if (_msg == 1 || _msg == 2)
{
if (_msg == 1)
{
graphics.DrawImage2D(0, 0, 1024, 768, png_paper1);
}
else if (_msg == 2)
{
graphics.DrawImage2D(0, 0, 1024, 768, png_instrucoes);
}
graphics.DrawImage2D(png_botao_continuar);
}
}
//////////////////////////Sound:
void Sound()
{
/*<botões de som>*/
graphics.DrawImage2D(990, 720, 23, 23, png_musica);
graphics.DrawImage2D(930, 720, 55, 25, png_som);
if (musica.state == Tocar)
{
if(!musica_1.IsPlaying())
{
musica_1.Stop();
musica_1.Play();
}
}
else if(musica.state == Trocar)
{
if (musica_1.IsPlaying())
{
musica_1.Pause();
musica.state = Parar;
}
else
{
musica_1.Play();
musica.state = Tocar;
}
}
else if (musica.state == Parar)
{
graphics.SetLineWidth(12);
graphics.SetColor(80, 0, 0);
graphics.DrawLine2D(990, 720, 1013, 743);
}
/*</botões de som>*/
}