-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinimigo.c
190 lines (156 loc) · 5.61 KB
/
inimigo.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
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
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "deflib.h"
/*
- INIMIGO:
- Possui a função que gera os inimigos e
as funções que os movimentam.
*/
void sorteiaPosInimigo(Jogador inimigo[], int end, Jogador player, Rectangle wallRecs[][N_COLUNAS]) {
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
float randX, randY;
int lin, col, ok, i, contador = 0;
Rectangle randPos;
do { // TENTA SORTEAR UMA POSIÇÃO ALEATÓRIA PARA UM INIMIGO 50 VEZES
contador ++;
ok = 1;
int shouldBreak = 0;
randX = (float)((int)rand()%(screenWidth - inimigo[end].sizeX));
randY = (float)((int)rand()%(screenHeight - inimigo[end].sizeY - 100));
randPos = (Rectangle){randX, randY, inimigo[end].sizeX, inimigo[end].sizeY};
int m = 100; // margem de distância até o player
if(CheckCollisionRecs(
randPos, (Rectangle){player.x-m, player.y-m, player.sizeX+2*m, player.sizeY+2*m})){
ok = 0;
}
// COLISÃO COM PAREDES
for(lin = 0; lin < N_LINHAS; lin++) {
for(col = 0; col < N_COLUNAS; col++) {
if(CheckCollisionRecs(randPos, wallRecs[lin][col])) {
//printf("Entrei\n");
ok = 0;
shouldBreak = 1;
break;
}
}
if(shouldBreak)
break;
}
// COLISÃO COM CÉLULAS JÁ EXISTENTES
for(i = 0; i < QUANT_INIMIGOS; i++) {
if(inimigo[i].naTela == 1) {
Rectangle inimigNaTela = {inimigo[i].x, inimigo[i].y,
inimigo[i].sizeX, inimigo[i].sizeY};
if(CheckCollisionRecs(randPos, inimigNaTela)) {
ok = 0;
break;
}
}
}
//printf("%d\n", ok);
} while(ok == 0 && contador < 50);
if (contador < 50){
//printf("inimigo %d x: %.2f e %.2f y \n", end, inimigo[end].x, inimigo[end].y);
inimigo[end].x = randX;
inimigo[end].y = randY;
//printf("inimigo %d x: %.2f e %.2f y \n", end, inimigo[end].x, inimigo[end].y);
inimigo[end].pers = (Rectangle){randX+OFFSET_X, randY+OFFSET_Y, LARGURA_TANQUE, TAMANHO_TANQUE};
//printf("'%c' RandX: %.2f RandY: %.2f\n", end+'0', randX, randY);
} else {
//printf("End '%c' nao foi incluido\n", end+'0');
inimigo[end].naTela = 0;
}
//printf("inimigo pers : %f, %f, %f, %f \n",inimigo[end].pers.x,inimigo[end].pers.y,inimigo[end].pers.height,inimigo[end].pers.width);
}
void UpdateINIMIGO(Jogador inimigo[], int contFrames,
Jogador player, Rectangle wallRecs[][N_COLUNAS]) {
int end, i, passou;
if(sorteiaZero(contFrames) == 1){
for(end = 0; end < QUANT_INIMIGOS; end++) {
if(inimigo[end].naTela == 0) {
for(i = 0; i < QUANT_TIROS; i++) {
passou = 1;
if(inimigo[end].tiros[i].naTela == 1) {
// NÃO COLOCA INIMIGO NA POSIÇÃO EM QUE AINDA POSSUI TIROS A SEREM RENDERIZADOS
passou = 0;
break;
}
}
if(passou) {
inimigo[end].naTela = 1;
inimigo[end].vidas = 1;
inimigo[end].sizeX = LARGURA_TANQUE;
inimigo[end].sizeY = TAMANHO_TANQUE;
break;
}
}
}
// OBS: NESSE MOMENTO, END POSSUI O PRIMEIRO ENDEREÇO LIVRE DO
// ARRAY DE INIMIGOS
//if(controle != 1 ){
if(end < QUANT_INIMIGOS) {
sorteiaPosInimigo(inimigo, end, player, wallRecs);
//printf("End '%c': %.2f %.2f\n", end+'0', inimigo[end].x, inimigo[end].y);
}
}
//}
// DEIXA APENAS OS QUE ESTÃO VIVOS
for(end = 0; end < QUANT_INIMIGOS; end++) {
if(inimigo[end].naTela == 1 && inimigo[end].vidas <= 0) {
inimigo[end].naTela = 0;
inimigo[end].timer = 0;
inimigo[end].sizeX = 0; // OBS: tamanho é zerado para não haver colisão com os tiros, já que naTela não é considerado nesse caso
inimigo[end].sizeY = 0;
//for(i = 0; i < QUANT_TIROS; i++) {
// inimigo[end].tiros[i].naTela = 0;
//}
}
}
}
/*float abs(float a) {
float res = a;
if(res < 0) res = -res;
return res;
}*/
void Movimenta_Random (Jogador *inimigo, Jogador player){
float xAnt = inimigo->x, yAnt = inimigo->y;
switch((int)inimigo->r){
case 0:
inimigo->y -= inimigo->vel;
break;
case 180:
inimigo->y += inimigo->vel;
break;
case 90:
inimigo->x += inimigo->vel;
break;
case 270:
inimigo->x -= inimigo->vel;
break;
}
int distanciamento_social = 100;
float dX = abs(inimigo->x - player.x), dY = abs(inimigo->y - player.y);
// OS INIMIGOS NÃO SE APROXIMAM DO PLAYER
if(dX < distanciamento_social && dY < distanciamento_social) {
inimigo->x = xAnt;
inimigo->y = yAnt;
}
}
int sorteiaR(Jogador *inimigo){
int vetorPosicao[4] = {0,90,180,270};
int escolha = rand() %4;
return ((int)inimigo->r+vetorPosicao[escolha])%360;
}
// DESCONTINUADA: Sorteia momento que o inimigo deve atirar
// Mentira, é melhor com ela
int EnemyShots(Jogador *inimigo){
int info = 0;
if (inimigo->dogtag != 100 && inimigo->alinhado == 1){
info = rand()%80;
}else
info = 0;
return info;
}