-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsprites.c
200 lines (174 loc) · 4.99 KB
/
sprites.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
/*funções específicas para trabalhar com tipo SPRITE
* */
#include "sprites.h"
//cria sprites obrigatórios caso não estejam no arquivo da fase
void create_default_sprites(struct sprite_list *sprite_list){
//se o mrdo nao esta no arquivo da fase
if(list_size(sprite_list->mr_do) == 0 ){
SPRITE mr_do = DEFAULT_MR_DO;
//bota mrdo na base do mapa
mr_do.position = (struct position) {.x = MAX_X / 2, .y = MAX_Y - 1 };
push(&(sprite_list->mr_do), mr_do);
}
//se o ninho nao tiver no arquivo de fase
if(list_size(sprite_list->nest) == 0 ){
SPRITE nest = DEFAULT_NEST;
//bota ninho no centro do mapa
nest.position = (struct position) {.x = MAX_X / 2, .y = MAX_Y / 2 };
push(&(sprite_list->nest), nest);
}
if(list_size(sprite_list->shot) == 0 ){
SPRITE shot = DEFAULT_SHOT;
push(&(sprite_list->shot), shot);
}
}
//cria as lista de sprites a partir do mapa
void make_lists(chtype (*MAP)[MAX_X], struct sprite_list *sl){
//percorre a matriz do mapa
for (int i = 0; i < MAX_Y; i++) {
for (int j = 0; j < MAX_X; j++) {
//sempre vai ter um espaco em branco abaixo de todo o mapa
SPRITE space = DEFAULT_SPACE;
space.position.x = j;
space.position.y = i;
push(&(sl->spaces), space);
SPRITE s;
switch (MAP[i][j]) {
case 'p':
s = DEFAULT_WALL;
s.position = (struct position){.x = j, .y = i};
push(&(sl->walls), s);
break;
case 't':
s = DEFAULT_SHOT;
s.position = (struct position){.x = j, .y = i};
push(&(sl->shot), s);
break;
case 'i':
s = DEFAULT_GHOST;
s.position = (struct position){.x = j, .y = i};
push(&(sl->ghosts), s);
break;
case 'f':
s = DEFAULT_FRUIT;
s.position = (struct position){.x = j, .y = i};
push(&(sl->fruits), s);
break;
case 'd':
s = DEFAULT_MR_DO;
s.position = (struct position){.x = j, .y = i};
push(&(sl->mr_do), s);
break;
case 'n':
s = DEFAULT_NEST;
s.position = (struct position){.x = j, .y = i};
push(&(sl->nest), s);
break;
}
}
}
create_default_sprites(sl);
}
//retorna se dois sprites colidiram ou não
int collided(SPRITE *current, SPRITE *sp){
return (current->alive && sp->alive) &&
(current->representation != sp->representation) &&
(((current->position.x == sp->position.x) && (current->position.y == sp->position.y)) || //mesma posicao
((sp->position.last_x == current->position.x) && (sp->position.last_y == current->position.y))//inverteram de posicao
);
}
//verifica a colisão de toda a lista de sprites
void check_sprite_collision(struct sprite_list *sl){
SPRITE *list;
SPRITE *current = sl->walls;
while(current != NULL){
//mrdo colide com parede
if (collided(current, sl->mr_do)) {
current->alive = FALSE;
}
current = current->next;
}
current = sl->fruits;
while(current != NULL){
//mrdo colide com fruta
if (collided(current, sl->mr_do)) {
current->alive = FALSE;
game_state.score += 50;
}
//tiro colide com fruta
if (collided(current, sl->shot)) {
current->alive = FALSE;
sl->shot->alive = FALSE;
}
//fantasma colide com fruta
list = sl->ghosts;
while(list != NULL){
if (collided(current, list)) {
current->alive = FALSE;
}
list = list->next;
}
current = current->next;
}
current = sl->ghosts;
while(current != NULL){
//fantasma colide com mrdo
if (collided(current, sl->mr_do)) {
sl->mr_do->alive = FALSE;
}
//fantasma colide com tiro
if (collided(current, sl->shot)) {
current->alive = FALSE;
sl->shot->alive = FALSE;
game_state.score += 10;
}
current = current->next;
}
}
void create_shot(struct sprite_list *sl){
if (!sl->shot->alive) {
sl->shot->alive = TRUE;
sl->shot->position = find_char(sl, CH_MR_DO);
sl->shot->direction = sl->mr_do->direction;
}
}
void create_ghost(struct sprite_list *sl){
if (list_size(sl->ghosts) < MAX_GHOSTS) {
SPRITE ghost = DEFAULT_GHOST;
int random_number = rand() % 4;
ghost.direction = random_number;
ghost.position = find_char(sl, CH_NEST);
push(&sl->ghosts, ghost);
}
}
//tipos padrões de sprite para simplificar o código
const SPRITE DEFAULT_GHOST = {
.representation = CH_GHOST,
.alive = TRUE,
.direction = left
};
const SPRITE DEFAULT_FRUIT = {
.alive = TRUE,
.representation = CH_FRUIT
};
const SPRITE DEFAULT_NEST = {
.representation = CH_NEST,
.alive = TRUE
};
const SPRITE DEFAULT_SHOT = {
.representation = CH_SHOT,
.direction = right,
.alive = FALSE
};
const SPRITE DEFAULT_MR_DO = {
.representation = CH_MR_DO,
.alive = TRUE
};
const SPRITE DEFAULT_WALL = {
.representation = CH_WALL,
.alive = TRUE
};
const SPRITE DEFAULT_SPACE = {
.representation = CH_SPACE,
.alive = TRUE
};