root / proj / src / ent.c @ 234
History | View | Annotate | Download (9.95 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 |
double x, y; //real position |
105 |
double vx, vy;
|
106 |
sprite_t *b; |
107 |
int damage;
|
108 |
}; |
109 |
bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy){ |
110 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
111 |
if(ret == NULL) return NULL; |
112 |
ret-> x = x; |
113 |
ret-> y = y; |
114 |
ret->vx = vx; |
115 |
ret->vy = vy; |
116 |
ret->damage = 10;
|
117 |
ret->b = sprite_ctor(b); |
118 |
if(ret->b == NULL){ |
119 |
bullet_dtor(ret); |
120 |
return NULL; |
121 |
} |
122 |
double angle = atan2(-ret->vy, ret->vx);
|
123 |
sprite_set_angle(ret->b, angle-M_PI_2); |
124 |
return ret;
|
125 |
} |
126 |
void (bullet_dtor)(bullet_t *p){
|
127 |
if(p == NULL) return; |
128 |
sprite_dtor(p->b); |
129 |
free(p); |
130 |
} |
131 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
132 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
133 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
134 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
135 |
int (bullet_get_damage) (const bullet_t *p){ return p->damage; } |
136 |
void (bullet_set_damage) (bullet_t *p, int damage) { |
137 |
if (damage < 0) damage = 0; |
138 |
p->damage = damage; |
139 |
} |
140 |
void (bullet_update_movement)(bullet_t *p){
|
141 |
p->x += p->vx; |
142 |
p->y += p->vy; |
143 |
} |
144 |
|
145 |
void (bullet_update_movement_list)(list_t *bullet_list){
|
146 |
if (bullet_list == NULL) return; |
147 |
if (list_size(bullet_list) == 0) return; |
148 |
|
149 |
list_node_t *it = list_begin(bullet_list); |
150 |
while (it != list_end(bullet_list)) {
|
151 |
bullet_update_movement(*(bullet_t**)list_node_val(it)); |
152 |
it = list_node_next(it); |
153 |
} |
154 |
} |
155 |
|
156 |
void (bullet_draw)(bullet_t *p){
|
157 |
const int16_t x_screen = bullet_get_x_screen(p);
|
158 |
const int16_t y_screen = bullet_get_y_screen(p);
|
159 |
sprite_set_pos (p->b, x_screen, y_screen); |
160 |
sprite_set_scale(p->b, scale); |
161 |
sprite_draw (p->b); |
162 |
} |
163 |
|
164 |
void (bullet_draw_list)(list_t *bullet_list) {
|
165 |
if (bullet_list == NULL) return; |
166 |
if (list_size(bullet_list) == 0) return; |
167 |
|
168 |
list_node_t *it = list_begin(bullet_list); |
169 |
while (it != list_end(bullet_list)) {
|
170 |
bullet_draw(*(bullet_t**)list_node_val(it)); |
171 |
it = list_node_next(it); |
172 |
} |
173 |
} |
174 |
|
175 |
struct map{
|
176 |
basic_sprite_t *bsp_background; |
177 |
sprite_t *background; |
178 |
uint8_t *collide; |
179 |
}; |
180 |
map_t* (map_ctor)(const char **background, const char **collide){ |
181 |
map_t *ret = malloc(sizeof(map_t));
|
182 |
if(ret == NULL) return NULL; |
183 |
|
184 |
ret->bsp_background = NULL;
|
185 |
ret->background = NULL;
|
186 |
ret->collide = NULL;
|
187 |
|
188 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
189 |
ret->background = sprite_ctor(ret->bsp_background); |
190 |
if(ret->bsp_background == NULL || |
191 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
192 |
|
193 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
194 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
195 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
196 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
197 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
198 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
199 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
200 |
for(unsigned i = 0; i < W*H; ++i){ |
201 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
202 |
} |
203 |
basic_sprite_dtor(bsp_collide); |
204 |
|
205 |
return ret;
|
206 |
} |
207 |
void (map_dtor)(map_t *p){
|
208 |
if(p == NULL) return; |
209 |
sprite_dtor(p->background); |
210 |
basic_sprite_dtor(p->bsp_background); |
211 |
free(p->collide); |
212 |
free(p); |
213 |
} |
214 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
215 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
216 |
int (map_collides_point)(const map_t *p, double x, double y){ |
217 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
218 |
int16_t x_ = x, y_ = y; |
219 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
220 |
uint32_t pos = x_ + y_*w; |
221 |
return p->collide[pos];
|
222 |
} |
223 |
|
224 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
225 |
double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
226 |
double shooter_x = gunner_get_x(shooter);
|
227 |
double shooter_y = gunner_get_y(shooter);
|
228 |
for (double x = -radius; x < radius; x += 1) { |
229 |
double y1 = sqrt(radius*radius - x*x);
|
230 |
double y2 = -y1;
|
231 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
232 |
} |
233 |
return 0; |
234 |
} |
235 |
|
236 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
237 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
238 |
double bullet_x = bullet_get_x(bull);
|
239 |
double bullet_y = bullet_get_y(bull);
|
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, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
244 |
} |
245 |
return 0; |
246 |
} |
247 |
|
248 |
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){ |
249 |
double shooter_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 |
|
253 |
double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
254 |
double bullet_x = bullet_get_x(bull);
|
255 |
double bullet_y = bullet_get_y(bull);
|
256 |
|
257 |
double dx = shooter_x - bullet_x;
|
258 |
double dy = shooter_y - bullet_y;
|
259 |
double distance = sqrt(dx*dx + dy*dy);
|
260 |
return distance <= shooter_radius+bullet_radius;
|
261 |
} |
262 |
|
263 |
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) { |
264 |
double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0; |
265 |
double shooter1_x = gunner_get_x(shooter1);
|
266 |
double shooter1_y = gunner_get_y(shooter1);
|
267 |
|
268 |
double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0; |
269 |
double shooter2_x = gunner_get_x(shooter2);
|
270 |
double shooter2_y = gunner_get_y(shooter2);
|
271 |
|
272 |
double dx = shooter1_x - shooter2_x;
|
273 |
double dy = shooter1_y - shooter2_y;
|
274 |
double distance = sqrt(dx*dx + dy*dy);
|
275 |
return distance <= shooter1_radius+shooter2_radius;
|
276 |
} |
277 |
|
278 |
void (map_draw)(map_t *p){
|
279 |
const int16_t x_screen = map_get_x_screen(p);
|
280 |
const int16_t y_screen = map_get_y_screen(p);
|
281 |
sprite_set_pos (p->background, x_screen, y_screen); |
282 |
sprite_set_scale(p->background, scale); |
283 |
sprite_draw (p->background); |
284 |
} |