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