-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.c
218 lines (168 loc) · 5.66 KB
/
collision.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
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
#ifndef COLLISION_C
#define COLLISION_C
#include "main.h"
#include "string.h"
#include "draw.c"
#include "lerlevel.c"
void tocar_som(Sound som) {
PlaySound(som);
}
// verifica se o inimigo colidiu com o jogador
bool checar_contato_inimigo(struct Inimigo inimigo) {
Rectangle box_jogador = {player.posx + 10, player.posy, ppl_width, ppl_height};
return (CheckCollisionRecs(box_jogador, inimigo.hitbox));
}
void matar_jogador(int ini_index, double horamorte) {
tocar_som(sounds.death);
player.vidas--;
while (GetTime() - horamorte < 3) {
BeginDrawing();
ClearBackground(BLACK);
draw_jogador();
draw_inimigo(inimigos[ini_index]);
DrawText("Voce morreu!", largura / 2 - 20, altura / 2 - 10, 16, YELLOW);
//DrawTextEx(fonte.alpha_beta, "Voce morreu", posicao_texto, 16.0, 5.0, YELLOW);
EndDrawing();
// garante que o jogo não fique travado quando o jogador morrer
if (WindowShouldClose()) CloseWindow();
}
// resetar a posicao do jogador e dos inimigos
resetar_posicoes();
}
// responde se há um baú fechado por perto
bool bau_perto(struct Bau bau) {
if (bau.estado) return false; // retorna false caso o baú já tenha sido aberto
Rectangle box_jogador = {player.posx, player.posy, ppl_width, ppl_height};
Rectangle box_bau = {(bau.posx -32), (bau.posy -32), 64, 64};
return (CheckCollisionRecs(box_jogador, box_bau));
}
// responde se há uma porta por perto
bool porta_perto(struct Porta porta)
{
if (!porta.liberada) return 0;
Rectangle box_jogador = {player.posx, player.posy, ppl_width, ppl_height};
Rectangle box_porta = {(porta.posx-35), (porta.posy-35), 70, 70};
return (CheckCollisionRecs(box_jogador, box_porta));
}
// marca o baú como fechado e dá ao jogador seus espólios
void loot_bau(struct Bau *bau) {
bau->estado = 1;
tocar_som(sounds.chest);
switch (bau->conteudo) {
case 'M':
player.municao += bau->qnt;
strcpy(game.legenda, "Voce abriu o bau e encontrou municao!");
break;
case 'V': player.vidas += bau->qnt;
strcpy(game.legenda, "Voce abriu o bau e ganhou uma vida!");
break;
case 'F': player.facas += bau->qnt;
strcpy(game.legenda, "Voce abriu o bau e encontrou facas!");
break;
case 'P': game.pontuacao += bau->qnt;
strcpy(game.legenda, "Voce abriu o bau e ganhou pontos extras!");
break;
}
}
void loot_faca() {
int i;
Rectangle box_faca;
Rectangle box_jogador = {player.posx, player.posy, ppl_width, ppl_height};
box_faca.width = 35;
box_faca.height = 30;
for (i = 0; i < 10; i++) {
if (!facas[i].ar) {
box_faca.x = facas[i].posx - 17;
box_faca.y = facas[i].posy;
//DrawRectangleLines(box_faca.x, box_faca.y, box_faca.width, box_faca.height, BLUE);
//DrawRectangleLines(box_jogador.x, box_jogador.y, box_jogador.width, box_jogador.height, BLUE);
if (CheckCollisionRecs(box_jogador, box_faca)) {
facas[i].posx = largura * 2;
facas[i].posy = altura * 2;
player.facas++;
game.seletor_facas--;
strcpy(game.legenda, "Voce apanhou uma faca!");
game.horalegenda = GetTime();
}
}
}
}
Rectangle disparobox() {
Rectangle box_bullet;
switch (player.direcao) {
case 'C': box_bullet.x = player.posx + 17; box_bullet.y = 0;
box_bullet.width = 1; box_bullet.height = player.posy;
break;
case 'B': box_bullet.x = player.posx + 17; box_bullet.y = player.posy;
box_bullet.width = 1; box_bullet.height = altura - player.posy;
break;
case 'D': box_bullet.x = player.posx + 11; box_bullet.y = player.posy + 24;
box_bullet.width = largura; box_bullet.height = 1;
break;
case 'E': box_bullet.x = 0; box_bullet.y = player.posy + 24;
box_bullet.width = player.posx + 17; box_bullet.height = 1;
break;
}
return box_bullet;
}
void matar_inimigo(struct Inimigo *inimigo) {
inimigo->vivo = 0;
inimigo->horamorte = GetTime();
strcpy(game.legenda, "Voce abateu um inimigo!");
game.horalegenda = GetTime();
game.pontuacao += 10;
}
void tiro() {
int i;
if (IsKeyPressed(KEY_SPACE) && player.municao) {
for (i = 0; i < 10; i++) {
Rectangle box_inimigo = {inimigos[i].posx, inimigos[i].posy, ppl_width, ppl_height};
if (CheckCollisionRecs(disparobox(), box_inimigo) && inimigos[i].vivo)
matar_inimigo(&inimigos[i]);
}
player.horadisparo = GetTime();
player.municao--;
tocar_som(sounds.shot);
}
}
int hitfaca(struct Faca faca) {
int i;
// testar se a faca acerta algum inimigo
for (i = 0; i < 10; i++) {
if (CheckCollisionRecs(faca.hitbox, inimigos[i].hitbox) && inimigos[i].vivo) {
matar_inimigo(&inimigos[i]);
game.pontuacao += 5;
return 1;
}
}
// testar se a faca acerta alguma parede
if (faca.posx < 15 || faca.posx > largura - 15) return 1;
if (faca.posy < 15 || faca.posy > altura - 15 - hud_height) return 1;
return 0;
}
void arremesso(struct Faca *faca) {
// caso a faca esteja guardada, será arremessada agora
if (!faca->ar) {
faca->posx = player.posx + 5;
faca->posy = player.posy + 15;
faca->hitbox.x = player.posx + 10;
faca->hitbox.y = player.posy + 15;
faca->direcao = player.direcao;
tocar_som(sounds.knife);
}
// tendo sido arremessada, percorrerá o cenário de acordo com a direção
faca->ar = 1;
switch (faca->direcao) {
case 'C': faca->posy -= 3; faca->hitbox.y -= 3; break;
case 'B': faca->posy += 3; faca->hitbox.y += 3; break;
case 'D': faca->posx += 3; faca->hitbox.x += 3; break;
case 'E': faca->posx -= 3; faca->hitbox.x -= 3; break;
}
// checa a colisão da faca
if (hitfaca(*faca)) {
faca->ar = 0;
faca->drop.x = faca->posx;
faca->drop.y = faca->posy;
}
}
#endif