-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.c
63 lines (57 loc) · 3.16 KB
/
bot.c
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
#include "./headers/header.h"
/* ARG = ponteiro para vetor bola, ponteiro para PADDLE de bot
RET/MOD = retorna um tipo BALL
Calcula a distancia entre todas as bolas e o pad e retorna a instancia de BALL mais proxima */
BALL testBallDistance(BALL *dummy_ball, PADDLE *botPad){
int i=0;
float distance, closerDist=80*80;
BALL closerBall = {.position.x = (MAP_WIDTH/2), .position.y = (MAP_HEIGHT/2)}; //Atribui a closerBall uma bola imaginaria parada no centro do mapa
if(botPad->vertical == FALSE){
for(i=0; i < MAXBALL; i++){
if(dummy_ball[i].enabled == TRUE && dummy_ball[i].velocity.y == -1){
distance = (dummy_ball[i].position.x - (botPad->position.x + botPad->len/2))*(dummy_ball[i].position.x - (botPad->position.x + botPad->len/2)) + (dummy_ball[i].position.y - botPad->position.y)*(dummy_ball[i].position.y - botPad->position.y);
if(distance < closerDist){
closerDist = distance;
closerBall = dummy_ball[i];
}
}
}
}
else{
for(i=0; i < MAXBALL; i++){
if(dummy_ball[i].enabled == TRUE && dummy_ball[i].velocity.y == -1){
distance = (dummy_ball[i].position.y - (botPad->position.y + botPad->len/2))*(dummy_ball[i].position.y - (botPad->position.y + botPad->len/2)) + (dummy_ball[i].position.x - botPad->position.x)*(dummy_ball[i].position.x - botPad->position.x);
if(distance < closerDist){
closerDist = distance;
closerBall = dummy_ball[i];
}
}
}
}
return closerBall;
}
/* ARG = ponteiro para PADDLE de bot, ponteiro para vetor bola, ponteiro para LEVEL
RET/MOD = modifica as propriedades do PADDLE de bot
botDecisionControl() decide se o bot irá mover para esquerda ou direita comparando sua posição com a bola mais próxima */
void botDecisionControl(PADDLE *botPad, BALL *dummy_ball, LEVEL *level){
BALL closerBall;
if(botPad->botMode == TRUE){
closerBall = testBallDistance(dummy_ball, botPad);
if(botPad->vertical == FALSE){
if((botPad->position.x + botPad->len/2) > closerBall.position.x)
botPad->velocity.x = -1; //botPad está mais a direita da bola, então deve andar para esquerda(-1)
else if((botPad->position.x + botPad->len/2) < closerBall.position.x)
botPad->velocity.x = 1; //botPad está mais a esquerda da bola, então deve andar para direita(1)
else
botPad->velocity.x = 0; //botPad está alinhado com a bola, então deve ficar parado(0)
}
}
else{
if((botPad->position.y + botPad->len/2) > closerBall.position.y)
botPad->velocity.y = -1; //botPad está mais a direita da bola, então deve andar para esquerda(-1)
else if((botPad->position.y + botPad->len/2) < closerBall.position.y)
botPad->velocity.y = 1; //botPad está mais a esquerda da bola, então deve andar para direita(1)
else
botPad->velocity.y = 0; //botPad está alinhado com a bola, então deve ficar parado(0)
}
}