root / proj / src / ent.c @ 234
History | View | Annotate | Download (9.95 KB)
1 | 193 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "ent.h" |
||
4 | |||
5 | #include "graph.h" |
||
6 | 216 | up20180642 | #include "sprite.h" |
7 | 226 | up20180642 | #include "utils.h" |
8 | 229 | up20180655 | #include "rectangle.h" |
9 | 220 | up20180655 | #include <math.h> |
10 | |||
11 | 193 | up20180642 | 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 | 194 | up20180642 | double (ent_get_scale) (void){ return scale; } |
18 | 193 | up20180642 | double (ent_get_XLength)(void){ return graph_get_XRes()/scale; } |
19 | double (ent_get_YLength)(void){ return graph_get_YRes()/scale; } |
||
20 | |||
21 | 201 | up20180642 | struct gunner{
|
22 | 203 | up20180642 | double x, y; //real position |
23 | 232 | up20180655 | double spawn_x, spawn_y;
|
24 | 193 | up20180642 | sprite_t *dude; |
25 | sprite_t *weapon; |
||
26 | 229 | up20180655 | int health, current_health;
|
27 | 193 | up20180642 | }; |
28 | 201 | up20180642 | gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){ |
29 | gunner_t *ret = malloc(sizeof(gunner_t));
|
||
30 | 193 | up20180642 | if(ret == NULL) return NULL; |
31 | 232 | up20180655 | ret->spawn_x = 0.0; |
32 | ret->spawn_y = 0.0; |
||
33 | 193 | up20180642 | ret->x = 0.0; |
34 | ret->y = 0.0; |
||
35 | 229 | up20180655 | ret->health = 100;
|
36 | 230 | up20180655 | ret->current_health = ret->health; |
37 | 193 | up20180642 | ret->dude = sprite_ctor(dude ); |
38 | ret->weapon = sprite_ctor(weapon); |
||
39 | if(ret->dude == NULL || ret->weapon == NULL){ |
||
40 | 201 | up20180642 | gunner_dtor(ret); |
41 | 193 | up20180642 | return NULL; |
42 | } else return ret; |
||
43 | } |
||
44 | 201 | up20180642 | void (gunner_dtor)(gunner_t *p){
|
45 | 193 | up20180642 | if(p == NULL) return; |
46 | sprite_dtor(p->dude); |
||
47 | sprite_dtor(p->weapon); |
||
48 | free(p); |
||
49 | } |
||
50 | 216 | up20180642 | void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
51 | 232 | up20180655 | void (gunner_set_spawn) (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; } |
52 | 216 | up20180642 | void (gunner_set_angle)(gunner_t *p, double angle ){ |
53 | 193 | up20180642 | sprite_set_angle(p->dude , angle); |
54 | sprite_set_angle(p->weapon, angle); |
||
55 | } |
||
56 | 230 | up20180655 | 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 | 229 | up20180655 | double (gunner_get_x) (const gunner_t *p){ return p->x; } |
65 | double (gunner_get_y) (const gunner_t *p){ return p->y; } |
||
66 | 232 | up20180655 | 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 | 231 | up20180655 | double (gunner_get_angle) (const gunner_t *p){ return sprite_get_angle(p->dude); } |
69 | 229 | up20180655 | 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 | 201 | up20180642 | 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 | 193 | up20180642 | 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 | 229 | up20180655 | gunner_draw_health(p); |
83 | 193 | up20180642 | } |
84 | 203 | up20180642 | |
85 | 229 | up20180655 | 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 | 230 | up20180655 | double x = gunner_get_x_screen(p) - w/2; |
89 | double y = gunner_get_y_screen(p) - h/2 - 10; |
||
90 | 229 | up20180655 | 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 | 203 | up20180642 | struct bullet{
|
104 | double x, y; //real position |
||
105 | 226 | up20180642 | double vx, vy;
|
106 | 203 | up20180642 | sprite_t *b; |
107 | 230 | up20180655 | int damage;
|
108 | 203 | up20180642 | }; |
109 | 227 | up20180642 | bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy){ |
110 | 203 | up20180642 | bullet_t *ret = malloc(sizeof(bullet_t));
|
111 | if(ret == NULL) return NULL; |
||
112 | 227 | up20180642 | ret-> x = x; |
113 | ret-> y = y; |
||
114 | ret->vx = vx; |
||
115 | ret->vy = vy; |
||
116 | 232 | up20180655 | ret->damage = 10;
|
117 | 203 | up20180642 | ret->b = sprite_ctor(b); |
118 | if(ret->b == NULL){ |
||
119 | bullet_dtor(ret); |
||
120 | return NULL; |
||
121 | 227 | up20180642 | } |
122 | double angle = atan2(-ret->vy, ret->vx);
|
||
123 | sprite_set_angle(ret->b, angle-M_PI_2); |
||
124 | return ret;
|
||
125 | 203 | up20180642 | } |
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 | 230 | up20180655 | 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 | 226 | up20180642 | void (bullet_update_movement)(bullet_t *p){
|
141 | p->x += p->vx; |
||
142 | p->y += p->vy; |
||
143 | } |
||
144 | 231 | up20180655 | |
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 | 203 | up20180642 | 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 | 216 | up20180642 | |
164 | 231 | up20180655 | 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 | 216 | up20180642 | 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 | 220 | up20180655 | int (map_collides_point)(const map_t *p, double x, double y){ |
217 | 216 | up20180642 | 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 | 220 | up20180655 | |
224 | 226 | up20180642 | int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
225 | 228 | up20180655 | double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
226 | 220 | up20180655 | double shooter_x = gunner_get_x(shooter);
|
227 | 224 | up20180655 | 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 | 220 | up20180655 | double y2 = -y1;
|
231 | 224 | up20180655 | if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
232 | 220 | up20180655 | } |
233 | return 0; |
||
234 | } |
||
235 | |||
236 | 226 | up20180642 | 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 | 220 | up20180655 | return 0; |
246 | } |
||
247 | |||
248 | 228 | up20180655 | 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 | 233 | up20180655 | 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 | 216 | up20180642 | 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 | } |