-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapa.c
109 lines (96 loc) · 2.89 KB
/
mapa.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
#include "main.h"
///Tenta ler o mapa retornando 0 se não conseguir abrir, e 1 se conseguir abrir
int leMapa(char mapa[HEIGHT][WIDTH], HEROI* heroi, SAIDA* saida, INIMIGOS* inimigos, CHAVE* chave)
{
char str[81];
int i=0, j=0;
FILE *arq;
//Pointer to the file read
arq = fopen("data/mapa.txt", "r");
//Testa se o arquivo abriu
if(arq == NULL)
{
printf("ERRO: O mapa não pode ser aberto\n");
fclose(arq);
return 0;
}
//Lê cada linha do arquivo
while(fgets(str, WIDTH + 1, arq) != NULL || i<HEIGHT)
{
//Se não for uma linha em branco
if(str[0] != '\n')
{
//Adiciona a linha no mapa
for(j=0; j<WIDTH; j++)
{
decodificaMapa(i, j, &str[j], heroi, saida, inimigos, chave);
mapa[i][j] = str[j];
}
i++;
}
}
fclose(arq);
return 1;
}
///Laço 'for' simples para printar SOMENTE as paredes do mapa
void printaParedes(char mapa[HEIGHT][WIDTH])
{
int i, j;
// Printa somente as paredes
for(i=0; i<HEIGHT; i++)
for(j=0; j<WIDTH; j++)
if (mapa[i][j] == '#') //Parede
putchxy(j, i, CHAR_PAREDE);
else if (mapa[i][j] == '0') //Refem
putchxy(j, i, CHAR_REFEM);
else if (mapa[i][j] == '%') //Bonus
putchxy(j, i, CHAR_BONUS);
else if (mapa[i][j] == 'K'){ //Chave
putchxy(j, i, 'o');
printf("¬");
}
return;
}
/// Decodifica as representações do mapa
void decodificaMapa(int x, int y, char* c, HEROI* heroi, SAIDA* saida, INIMIGOS* inimigos, CHAVE* chave)
{
switch(toupper(*c))
{
case '@': //Inimigos
if (inimigos->qtde < MAX_INIMIGOS) //Somente adicionarei novos inimigos se eu ainda tiver espaço na array
{
inimigos->listaInimigos[inimigos->qtde].x = y;
inimigos->listaInimigos[inimigos->qtde].y = x;
inimigos->listaInimigos[inimigos->qtde].direcao = DIREITA;
inimigos->listaInimigos[inimigos->qtde].u_direcao = DIREITA;
inimigos->listaInimigos[inimigos->qtde].passos_restantes = 0;
inimigos->listaInimigos[inimigos->qtde].t_sono = 0;
inimigos->listaInimigos[inimigos->qtde].estado = 1;
inimigos->listaInimigos[inimigos->qtde].ciclos = 0;
inimigos->qtde++;
}
break;
case 'X': //Saída
if(!saida->x && !saida->y)
{
*c = '#';
saida->x = y;
saida->y = x;
}
break;
case 'O': //Decodifica o heroi
if (!heroi->x && !heroi->y)
{
heroi->x = y;
heroi->y = x;
}
break;
case 'K': //Chave
if(!chave->x && !chave-y)
{
chave->x = y;
chave->y = x;
}
break;
}
}