-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmovement.c
187 lines (173 loc) · 4.22 KB
/
movement.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
/*controla movimentação dos sprites na tela*/
#include "main.h"
#include "path.h"
#include "movement.h"
//retorna o caractere adjacente a posição dada na direção dada
int next_char(WINDOW *w, struct position p, enum direction direction){
int next_ch;
switch (direction) {
case up:
next_ch = mvwinch(w, p.y - 1, p.x );
break;
case down:
next_ch = mvwinch(w, p.y + 1, p.x );
break;
case right:
next_ch = mvwinch(w, p.y, p.x + 1);
break;
case left:
next_ch = mvwinch(w, p.y, p.x - 1 );
break;
}
return next_ch;
}
//verifica se o sprite pode se mover na direção dada
int can_go_to_direction(WINDOW *w, SPRITE sp, enum direction direction){
int can_go = 0;
//verifica se eh fim do mapa
switch (direction) {
case up:
if (sp.position.y > 0)
can_go = 1;
break;
case right:
if (sp.position.x < MAX_X - 1)
can_go = 1;
break;
case down:
if (sp.position.y < MAX_Y - 1)
can_go = 1;
break;
case left:
if (sp.position.x > 0)
can_go = 1;
}
//verificacoes especificas pra cada tipo de sprite
int next_ch = next_char(w, sp.position, direction);
if (sp.representation == CH_MR_DO){
return (next_ch != -1 && can_go);
}
else{
return (next_ch != CH_WALL && next_ch != -1 && can_go);
}
}
//tenta mover o sprite e retorna um booleano indicando se foi possivel se mover
int move_sprite(WINDOW *w, SPRITE *sprite, enum direction direction){
sprite->position.last_x = sprite->position.x;
sprite->position.last_y = sprite->position.y;
int can_go = can_go_to_direction(w, *sprite, direction);
if (can_go) {
switch (direction) {
case up:
sprite->position.y--;
break;
case right:
sprite->position.x++;
break;
case down:
sprite->position.y++;
break;
case left:
sprite->position.x--;
break;
}
}
sprite->direction = direction;
return can_go;
}
//algoritmo de movimentação dos fantasmas
void move_ghost(WINDOW *w, SPRITE *gh, NODE map_node[MAX_Y][MAX_X], struct position destiny){
//Nó inicial
NODE *start;
start = &map_node[gh->position.y][gh->position.x];
start->g = 0;
start->f = start->h;
start->parent = NULL;
start->status = 1;
//INICIALIZAÇÃO//
int res = findPath(map_node, destiny); //Função que encontra o caminho.
struct position next; //struct position contendo o próximo passo
next = (nextStep(map_node, destiny))->pos;
if(res){
if (next.x < gh->position.x) {
move_sprite(w, gh, left);
}
if (next.y > gh->position.y) {
move_sprite(w, gh, down);
}
if (next.y < gh->position.y) {
move_sprite(w, gh, up);
}
if (next.x > gh->position.x) {
move_sprite(w, gh, right);
}
}
else{
move_sprite(w, gh, right);
}
}
void move_ghosts(WINDOW *w, SPRITE *ghosts){
SPRITE *current = ghosts;
char map_char[MAX_Y][MAX_X];
chtype ch;
struct position destiny;
int mr_do_alive = 0;
for (int i = 0; i < MAX_Y; i++) {
for (int j = 0; j < MAX_X; j++) {
ch = mvwinch(w, i, j);
if (ch == CH_WALL) {
map_char[i][j] = '1';
}
else if(ch == CH_MR_DO){
mr_do_alive = 1;
destiny.y = i;
destiny.x = j;
map_char[i][j] = '2';
}
else{
map_char[i][j] = '2';
}
}
}
if (!mr_do_alive) {
return;
}
NODE map_node[MAX_Y][MAX_X];
createMap(map_node, map_char, destiny);
while(current != NULL){
if (current->alive) {
move_ghost(w, current, map_node, destiny);
}
current = current->next;
}
}
//tiro morre quando nao pode mais se mover
void move_shot(WINDOW *w, SPRITE *s){
if (!move_sprite(w, s, s->direction)) {
s->alive = 0;
}
}
enum direction get_keyboard_direction(chtype ch){
switch(ch){
case KEY_RIGHT:
return right;
case KEY_LEFT:
return left;
case KEY_UP:
return up;
case KEY_DOWN:
return down;
}
return right;
}
int valid_key(chtype ch){
int valid = 0;
switch(ch){
case KEY_RIGHT:
case KEY_LEFT:
case KEY_UP:
case KEY_DOWN:
valid = 1;
}
return valid;
}