-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncoes.c
216 lines (201 loc) · 8.07 KB
/
funcoes.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
#include <stdio.h>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include "game.h"
#include "lista.h"
#include "init_sprites.h"
#include "display.h"
#include "som.h"
tile **aloca_area(int lin, int col){
int i;
tile **area; // aloca matriz de jogo com método vetor de ponteiros para linhas
area = malloc(COL*sizeof(tile*));
area[0] = calloc(LIN*COL, sizeof(tile));
for(i=1; i<LIN; i++)
area[i] = area[0] + i*COL;
return area;
}
void inicializa_jogo(tile **area, jogador *player, nodo **pedras, nodo **diamantes, nodo **butterflies, nodo **fireflies, jogo *jogo){
// inicializa a área de jogo. lê um arquivo de texto contendo instruções
// de como desenhar o mapa, e armazena tudo em uma matriz de "tile"
int i, j;
char aux;
char buf[128];
nodo *novo_item; // variavel tipo nodo para armazenar um item criado
snprintf(buf, sizeof(buf), "./resources/level%d.txt", jogo->n_level);
ALLEGRO_FILE *level = al_fopen(buf, "r");
if(!level) // se fopen for mal sucedido
exit(1);
for(i = 0; i < LIN; i++)
for(j = 0; j < COL; j++){
al_fread(level, &aux, 1); // fread para ler do arquivo txt
if (aux != '\n'){
switch(aux){
case 'W':
area[i][j].tipo = Wall;
break;
case 'w':
area[i][j].tipo = Brick;
break;
case 'd':
area[i][j].tipo = Diamond;
novo_item = cria_nodo(0, j, i);
insere_nodo(diamantes, novo_item);
break;
case 'r':
area[i][j].tipo = Rock;
novo_item = cria_nodo(0, j, i);
insere_nodo(pedras, novo_item);
break;
case '.':
area[i][j].tipo = Dirt;
break;
case 'P':
area[i][j].tipo = SaidaFechada;
jogo->saida_x = j;
jogo->saida_y = i;
break;
case 'X':
area[i][j].tipo = Empty;
player->x = j;
player->y = i;
break;
case ' ':
area[i][j].tipo = Empty;
break;
case 'B':
area[i][j].tipo = Enemy;
novo_item = cria_nodo(0, j, i);
novo_item->dir = UP;
*butterflies = insere_nodo(butterflies, novo_item);
break;
case 'q':
area[i][j].tipo = Enemy;
novo_item = cria_nodo(0, j, i);
novo_item->dir = UP;
*fireflies = insere_nodo(fireflies, novo_item);
break;
default:
break;
}
}else j--;
}
al_fclose(level);
}
int colisao(tile **area, int direcao, jogador player){
int px = player.x, py = player.y; //player.x e player.y, para simplificar
int cima = area[py-1][px].tipo, //ladrilhos adjacentes
esquerda = area[py][px-1].tipo,
baixo = area[py+1][px].tipo,
direita = area[py][px+1].tipo;
if (direcao == UP && (cima == Empty || cima == Dirt || cima == Diamond || cima == SaidaAberta)) // condicoes para nao ter colisao
return 0; // retorna 0 se nao existir colisao
if (direcao == LEFT && (esquerda == Empty || esquerda == Dirt || esquerda == Diamond || esquerda == SaidaAberta))
return 0;
if (direcao == DOWN && (baixo == Empty || baixo == Dirt || baixo == Diamond || baixo == SaidaAberta))
return 0;
if (direcao == RIGHT && (direita == Empty || direita == Dirt || direita == Diamond || direita == SaidaAberta))
return 0;
if (direcao == LEFT && (esquerda == Rock && area[py][px-2].tipo == Empty))
return 0; // testa tambem os casos de poder empurrar
if (direcao == RIGHT && (direita == Rock && area[py][px+2].tipo == Empty))
return 0;
return 1; // retorna 1 se existir colisao
}
void move_player(tile **area, jogador *player, int dir){
// funcao para mover o player
switch(dir){
case UP: // caso a direcao seja pra cima, define o espaco de cima
area[player->y-1][player->x].tipo = Player; // como player, e o atual como vazio
area[player->y][player->x].tipo = Empty;
player->y--;
break;
case DOWN: // faz o mesmo para outras direcoes
area[player->y+1][player->x].tipo = Player;
area[player->y][player->x].tipo = Empty;
player->y++;
break;
case LEFT:
area[player->y][player->x-1].tipo = Player;
area[player->y][player->x].tipo = Empty;
player->x--;
player->dir = LEFT;
break;
case RIGHT:
area[player->y][player->x+1].tipo= Player;
area[player->y][player->x].tipo = Empty;
player->x++;
player->dir = RIGHT;
break;
}
}
int empurra(tile **area, jogador *player, nodo *pedras, int dir){ //direção será -1 para esquerda ou 1 para direita
nodo *pedra_proxima;
if (area[player->y][player->x+(dir)].tipo == Rock && area[player->y][player->x+(2*dir)].tipo == Empty){
pedra_proxima = busca_nodo(pedras, player->x+dir, player->y);
area[player->y][player->x+(2*dir)].tipo = Rock; // x+2 ou x-2 será pedra, dependendo da direção
area[player->y][player->x+dir].tipo = Player; // x+1 ou x-1 será Player, dependendo da direção
area[player->y][player->x].tipo = Empty; //substitui o espaço atual por vazio
player->x+=dir;
pedra_proxima->x+=dir;
return 1; // retorna 1 se conseguiu empurrar
}
return 0; // retorna 0 para sinalizar que não empurrou
}
void coleta_diamante(tile **area, jogador *player, jogo *jogo, t_sons sons, nodo **diamantes){
nodo *diamante_coletado;
diamante_coletado = busca_nodo(*diamantes, player->x, player->y);
if(diamante_coletado != NULL){
deleta_nodo(diamantes, diamante_coletado);
player->score += 10;
jogo->d_restantes--;
al_play_sample(sons.diamante, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
}
void desenha_mapa(tile **area, t_sprites sprites){
int i, j;
for(i = 0; i < LIN; i++)
for(j = 0; j < COL; j++){
switch(area[i][j].tipo){
case Wall:
al_draw_bitmap(sprites.wall, j*16, i*16 + OFF, 0);
break;
case Dirt:
al_draw_bitmap(sprites.dirt, j*16, i*16 + OFF, 0);
break;
case SaidaAberta:
al_draw_bitmap(sprites.exit, j*16, i*16 + OFF, 0);
break;
case Brick:
al_draw_bitmap(sprites.brick, j*16, i*16 + OFF, 0);
break;
default:
break;
}
}
}
void testa_init(bool test, const char *objeto){
if(test) return;
printf("%s não pôde ser inicializado\n", objeto);
exit(1);
}
int explode(tile **area, t_sprites sprites, int x, int y, int frame){
int i,j;
for (i = -1; i <= 1; i++)
for(j = -1; j <= 1; j++){
al_draw_bitmap(sprites.explosao[frame], x+j,y+i + OFF, 0);
area[y+i][x+j].tipo = Empty;
}
return 0;
}
void abre_saida(tile **area, jogo jogo){
area[jogo.saida_y][jogo.saida_x].tipo = SaidaAberta;
}
void gera_tela_help(ALLEGRO_BITMAP **help){
help[0] = al_load_bitmap("./resources/help1.png");
help[1] = al_load_bitmap("./resources/help2.png");
help[2] = al_load_bitmap("./resources/help3.png");
}