root / proj / src / ent.c @ 305
History | View | Annotate | Download (11 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "ent.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "utils.h" |
7 |
#include "rectangle.h" |
8 |
#include <math.h> |
9 |
|
10 |
static double scale = 1.0; |
11 |
static int16_t x_origin = 0; |
12 |
static int16_t y_origin = 0; |
13 |
|
14 |
void (ent_set_scale) (double n){ scale = n; } |
15 |
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; } |
16 |
double (ent_get_scale) (void){ return scale; } |
17 |
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; } |
18 |
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; } |
19 |
|
20 |
struct gunner{
|
21 |
double x, y; //real position |
22 |
double spawn_x, spawn_y;
|
23 |
sprite_t *dude; |
24 |
sprite_t *weapon; |
25 |
double health, current_health;
|
26 |
gunner_type type; |
27 |
int team;
|
28 |
}; |
29 |
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, gunner_type type, int team){
|
30 |
gunner_t *ret = malloc(sizeof(gunner_t));
|
31 |
if(ret == NULL) return NULL; |
32 |
ret->spawn_x = 0.0; |
33 |
ret->spawn_y = 0.0; |
34 |
ret->x = 0.0; |
35 |
ret->y = 0.0; |
36 |
ret->health = 100;
|
37 |
ret->current_health = ret->health; |
38 |
ret->type = type; |
39 |
ret->team = team; |
40 |
ret->dude = sprite_ctor(dude ); |
41 |
ret->weapon = sprite_ctor(weapon); |
42 |
if(ret->dude == NULL || ret->weapon == NULL){ |
43 |
gunner_dtor(ret); |
44 |
return NULL; |
45 |
} else return ret; |
46 |
} |
47 |
void (gunner_dtor)(gunner_t *p){
|
48 |
if(p == NULL) return; |
49 |
sprite_dtor(p->dude); |
50 |
sprite_dtor(p->weapon); |
51 |
free(p); |
52 |
} |
53 |
void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
54 |
void (gunner_set_spawn) (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; } |
55 |
void (gunner_set_angle)(gunner_t *p, double angle ){ |
56 |
sprite_set_angle(p->dude , angle); |
57 |
sprite_set_angle(p->weapon, angle); |
58 |
} |
59 |
void (gunner_set_health) (gunner_t *p, double health) { |
60 |
if (health < 0) health = 0; |
61 |
p->health = health; |
62 |
} |
63 |
void (gunner_set_curr_health) (gunner_t *p, double health) { |
64 |
if (health < 0) health = 0; |
65 |
p->current_health = health; |
66 |
} |
67 |
double (gunner_get_x) (const gunner_t *p){ return p->x; } |
68 |
double (gunner_get_y) (const gunner_t *p){ return p->y; } |
69 |
double (gunner_get_spawn_x) (const gunner_t *p){ return p->spawn_x; } |
70 |
double (gunner_get_spawn_y) (const gunner_t *p){ return p->spawn_y; } |
71 |
double (gunner_get_angle) (const gunner_t *p){ return sprite_get_angle(p->dude); } |
72 |
double (gunner_get_health) (const gunner_t *p){ return p->health; } |
73 |
double (gunner_get_curr_health) (const gunner_t *p){ return p->current_health; } |
74 |
int16_t (gunner_get_x_screen) (const gunner_t *p){ return (p->x-x_origin)*scale; } |
75 |
int16_t (gunner_get_y_screen) (const gunner_t *p){ return (p->y-y_origin)*scale; } |
76 |
gunner_type (gunner_get_type) (const gunner_t *p){ return p->type; } |
77 |
int (gunner_get_team) (const gunner_t *p){ return p->team; } |
78 |
void (gunner_draw)(gunner_t *p){
|
79 |
const int16_t x_screen = gunner_get_x_screen(p);
|
80 |
const int16_t y_screen = gunner_get_y_screen(p);
|
81 |
sprite_set_pos (p->dude , x_screen, y_screen); |
82 |
sprite_set_pos (p->weapon, x_screen, y_screen); |
83 |
sprite_set_scale(p->dude , scale); |
84 |
sprite_set_scale(p->weapon, scale); |
85 |
sprite_draw (p->weapon); |
86 |
sprite_draw (p->dude ); |
87 |
gunner_draw_health(p); |
88 |
} |
89 |
|
90 |
void (gunner_draw_health)(const gunner_t *p) { |
91 |
int16_t w = sprite_get_w(p->dude); |
92 |
int16_t h = sprite_get_h(p->dude); |
93 |
double x = gunner_get_x_screen(p) - w/2; |
94 |
double y = gunner_get_y_screen(p) - h/2 - 10; |
95 |
double curr_health = gunner_get_curr_health(p);
|
96 |
double health = gunner_get_health(p);
|
97 |
double perc = curr_health/health;
|
98 |
rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
|
99 |
rectangle_set_fill_color(green_bar, 0x00FF00);
|
100 |
rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10); |
101 |
rectangle_set_fill_color(red_bar, 0xFF0000);
|
102 |
rectangle_draw(green_bar); |
103 |
rectangle_draw(red_bar); |
104 |
rectangle_dtor(green_bar); |
105 |
rectangle_dtor(red_bar); |
106 |
} |
107 |
|
108 |
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){ |
109 |
double dx = gunner_get_x(p1) - gunner_get_x(p2);
|
110 |
double dy = gunner_get_y(p1) - gunner_get_y(p2);
|
111 |
return sqrt(dx*dx+dy*dy);
|
112 |
} |
113 |
|
114 |
struct bullet{
|
115 |
const gunner_t *shooter;
|
116 |
double x, y; //real position |
117 |
double vx, vy;
|
118 |
sprite_t *b; |
119 |
double damage;
|
120 |
}; |
121 |
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){ |
122 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
123 |
if(ret == NULL) return NULL; |
124 |
ret->shooter = shooter; |
125 |
ret-> x = x; |
126 |
ret-> y = y; |
127 |
ret->vx = vx; |
128 |
ret->vy = vy; |
129 |
ret->damage = 10;
|
130 |
ret->b = sprite_ctor(b); |
131 |
if(ret->b == NULL){ |
132 |
bullet_dtor(ret); |
133 |
return NULL; |
134 |
} |
135 |
double angle = atan2(-ret->vy, ret->vx);
|
136 |
sprite_set_angle(ret->b, angle-M_PI_2); |
137 |
return ret;
|
138 |
} |
139 |
void (bullet_dtor)(bullet_t *p){
|
140 |
if(p == NULL) return; |
141 |
sprite_dtor(p->b); |
142 |
free(p); |
143 |
} |
144 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
145 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
146 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
147 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
148 |
double (bullet_get_damage) (const bullet_t *p){ return p->damage; } |
149 |
void (bullet_set_damage) (bullet_t *p, double damage) { |
150 |
if (damage < 0) damage = 0; |
151 |
p->damage = damage; |
152 |
} |
153 |
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; } |
154 |
void (bullet_update_movement)(bullet_t *p){
|
155 |
p->x += p->vx; |
156 |
p->y += p->vy; |
157 |
} |
158 |
|
159 |
void (bullet_update_movement_list)(list_t *bullet_list){
|
160 |
if (list_size(bullet_list) == 0) return; |
161 |
|
162 |
list_node_t *it = list_begin(bullet_list); |
163 |
while (it != list_end(bullet_list)) {
|
164 |
bullet_update_movement(*(bullet_t**)list_node_val(it)); |
165 |
it = list_node_next(it); |
166 |
} |
167 |
} |
168 |
|
169 |
void (bullet_draw)(bullet_t *p){
|
170 |
const int16_t x_screen = bullet_get_x_screen(p);
|
171 |
const int16_t y_screen = bullet_get_y_screen(p);
|
172 |
sprite_set_pos (p->b, x_screen, y_screen); |
173 |
sprite_set_scale(p->b, scale); |
174 |
sprite_draw (p->b); |
175 |
} |
176 |
|
177 |
void (gunner_draw_list)(list_t *shooter_list) {
|
178 |
if (list_size(shooter_list) == 0) return; |
179 |
|
180 |
list_node_t *it = list_begin(shooter_list); |
181 |
while (it != list_end(shooter_list)) {
|
182 |
gunner_draw(*(gunner_t**)list_node_val(it)); |
183 |
it = list_node_next(it); |
184 |
} |
185 |
} |
186 |
|
187 |
void (bullet_draw_list)(list_t *bullet_list) {
|
188 |
if (list_size(bullet_list) == 0) return; |
189 |
|
190 |
list_node_t *it = list_begin(bullet_list); |
191 |
while (it != list_end(bullet_list)) {
|
192 |
bullet_draw(*(bullet_t**)list_node_val(it)); |
193 |
it = list_node_next(it); |
194 |
} |
195 |
} |
196 |
|
197 |
struct map{
|
198 |
basic_sprite_t *bsp_background; |
199 |
sprite_t *background; |
200 |
uint8_t *collide; |
201 |
}; |
202 |
map_t* (map_ctor)(const char **background, const char **collide){ |
203 |
map_t *ret = malloc(sizeof(map_t));
|
204 |
if(ret == NULL) return NULL; |
205 |
|
206 |
ret->bsp_background = NULL;
|
207 |
ret->background = NULL;
|
208 |
ret->collide = NULL;
|
209 |
|
210 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
211 |
ret->background = sprite_ctor(ret->bsp_background); |
212 |
if(ret->bsp_background == NULL || |
213 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
214 |
|
215 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
216 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
217 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
218 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
219 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
220 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
221 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
222 |
for(unsigned i = 0; i < W*H; ++i){ |
223 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
224 |
} |
225 |
basic_sprite_dtor(bsp_collide); |
226 |
|
227 |
return ret;
|
228 |
} |
229 |
void (map_dtor)(map_t *p){
|
230 |
if(p == NULL) return; |
231 |
sprite_dtor(p->background); |
232 |
basic_sprite_dtor(p->bsp_background); |
233 |
free(p->collide); |
234 |
free(p); |
235 |
} |
236 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
237 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
238 |
int16_t (map_get_width) (const map_t *p){ return sprite_get_w(p->background); } |
239 |
int16_t (map_get_height) (const map_t *p){ return sprite_get_h(p->background); } |
240 |
int (map_collides_point)(const map_t *p, double x, double y){ |
241 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
242 |
int16_t x_ = x, y_ = y; |
243 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
244 |
uint32_t pos = x_ + y_*w; |
245 |
return p->collide[pos];
|
246 |
} |
247 |
|
248 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
249 |
double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
250 |
double shooter_x = gunner_get_x(shooter);
|
251 |
double shooter_y = gunner_get_y(shooter);
|
252 |
for (double x = -radius; x < radius; x += 1) { |
253 |
double y1 = sqrt(radius*radius - x*x);
|
254 |
double y2 = -y1;
|
255 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
256 |
} |
257 |
return 0; |
258 |
} |
259 |
|
260 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
261 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
262 |
double bullet_x = bullet_get_x(bull);
|
263 |
double bullet_y = bullet_get_y(bull);
|
264 |
for (double x = -radius; x < radius; x += 1){ |
265 |
double y1 = sqrt(radius*radius - x*x);
|
266 |
double y2 = -y1;
|
267 |
if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
268 |
} |
269 |
return 0; |
270 |
} |
271 |
|
272 |
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){ |
273 |
if(bull->shooter == shooter) return false; |
274 |
|
275 |
double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
276 |
double shooter_x = gunner_get_x(shooter);
|
277 |
double shooter_y = gunner_get_y(shooter);
|
278 |
|
279 |
double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
280 |
double bullet_x = bullet_get_x(bull);
|
281 |
double bullet_y = bullet_get_y(bull);
|
282 |
|
283 |
double dx = shooter_x - bullet_x;
|
284 |
double dy = shooter_y - bullet_y;
|
285 |
double distance = sqrt(dx*dx + dy*dy);
|
286 |
return distance <= shooter_radius+bullet_radius;
|
287 |
} |
288 |
|
289 |
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) { |
290 |
double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0; |
291 |
double shooter1_x = gunner_get_x(shooter1);
|
292 |
double shooter1_y = gunner_get_y(shooter1);
|
293 |
|
294 |
double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0; |
295 |
double shooter2_x = gunner_get_x(shooter2);
|
296 |
double shooter2_y = gunner_get_y(shooter2);
|
297 |
|
298 |
double dx = shooter1_x - shooter2_x;
|
299 |
double dy = shooter1_y - shooter2_y;
|
300 |
double distance = sqrt(dx*dx + dy*dy);
|
301 |
return distance <= shooter1_radius+shooter2_radius;
|
302 |
} |
303 |
|
304 |
void (map_draw)(map_t *p){
|
305 |
const int16_t x_screen = map_get_x_screen(p);
|
306 |
const int16_t y_screen = map_get_y_screen(p);
|
307 |
sprite_set_pos (p->background, x_screen, y_screen); |
308 |
sprite_set_scale(p->background, scale); |
309 |
sprite_draw (p->background); |
310 |
} |