root / proj / src / ent.c @ 230
History | View | Annotate | Download (8.26 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 | 193 | up20180642 | sprite_t *dude; |
24 | sprite_t *weapon; |
||
25 | 229 | up20180655 | int health, current_health;
|
26 | 193 | up20180642 | }; |
27 | 201 | up20180642 | gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){ |
28 | gunner_t *ret = malloc(sizeof(gunner_t));
|
||
29 | 193 | up20180642 | if(ret == NULL) return NULL; |
30 | ret->x = 0.0; |
||
31 | ret->y = 0.0; |
||
32 | 229 | up20180655 | ret->health = 100;
|
33 | 230 | up20180655 | ret->current_health = ret->health; |
34 | 193 | up20180642 | ret->dude = sprite_ctor(dude ); |
35 | ret->weapon = sprite_ctor(weapon); |
||
36 | if(ret->dude == NULL || ret->weapon == NULL){ |
||
37 | 201 | up20180642 | gunner_dtor(ret); |
38 | 193 | up20180642 | return NULL; |
39 | } else return ret; |
||
40 | } |
||
41 | 201 | up20180642 | void (gunner_dtor)(gunner_t *p){
|
42 | 193 | up20180642 | if(p == NULL) return; |
43 | sprite_dtor(p->dude); |
||
44 | sprite_dtor(p->weapon); |
||
45 | free(p); |
||
46 | } |
||
47 | 216 | up20180642 | void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
48 | void (gunner_set_angle)(gunner_t *p, double angle ){ |
||
49 | 193 | up20180642 | sprite_set_angle(p->dude , angle); |
50 | sprite_set_angle(p->weapon, angle); |
||
51 | } |
||
52 | 230 | up20180655 | void (gunner_set_health) (gunner_t *p, int health) { |
53 | if (health < 0) health = 0; |
||
54 | p->health = health; |
||
55 | } |
||
56 | void (gunner_set_curr_health) (gunner_t *p, int health) { |
||
57 | if (health < 0) health = 0; |
||
58 | p->current_health = health; |
||
59 | } |
||
60 | 229 | up20180655 | double (gunner_get_x) (const gunner_t *p){ return p->x; } |
61 | double (gunner_get_y) (const gunner_t *p){ return p->y; } |
||
62 | int (gunner_get_health) (const gunner_t *p){ return p->health; } |
||
63 | int (gunner_get_curr_health) (const gunner_t *p){ return p->current_health; } |
||
64 | int16_t (gunner_get_x_screen) (const gunner_t *p){ return (p->x-x_origin)*scale; } |
||
65 | int16_t (gunner_get_y_screen) (const gunner_t *p){ return (p->y-y_origin)*scale; } |
||
66 | 201 | up20180642 | void (gunner_draw)(gunner_t *p){
|
67 | const int16_t x_screen = gunner_get_x_screen(p);
|
||
68 | const int16_t y_screen = gunner_get_y_screen(p);
|
||
69 | 193 | up20180642 | sprite_set_pos (p->dude , x_screen, y_screen); |
70 | sprite_set_pos (p->weapon, x_screen, y_screen); |
||
71 | sprite_set_scale(p->dude , scale); |
||
72 | sprite_set_scale(p->weapon, scale); |
||
73 | sprite_draw (p->weapon); |
||
74 | sprite_draw (p->dude ); |
||
75 | 229 | up20180655 | gunner_draw_health(p); |
76 | 193 | up20180642 | } |
77 | 203 | up20180642 | |
78 | 229 | up20180655 | void (gunner_draw_health)(const gunner_t *p) { |
79 | int16_t w = sprite_get_w(p->dude); |
||
80 | int16_t h = sprite_get_h(p->dude); |
||
81 | 230 | up20180655 | double x = gunner_get_x_screen(p) - w/2; |
82 | double y = gunner_get_y_screen(p) - h/2 - 10; |
||
83 | 229 | up20180655 | int curr_health = gunner_get_curr_health(p);
|
84 | int health = gunner_get_health(p);
|
||
85 | double perc = (double)curr_health/health; |
||
86 | rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
|
||
87 | rectangle_set_fill_color(green_bar, 0x00FF00);
|
||
88 | rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10); |
||
89 | rectangle_set_fill_color(red_bar, 0xFF0000);
|
||
90 | rectangle_draw(green_bar); |
||
91 | rectangle_draw(red_bar); |
||
92 | rectangle_dtor(green_bar); |
||
93 | rectangle_dtor(red_bar); |
||
94 | } |
||
95 | |||
96 | 203 | up20180642 | struct bullet{
|
97 | double x, y; //real position |
||
98 | 226 | up20180642 | double vx, vy;
|
99 | 203 | up20180642 | sprite_t *b; |
100 | 230 | up20180655 | int damage;
|
101 | 203 | up20180642 | }; |
102 | 227 | up20180642 | bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy){ |
103 | 203 | up20180642 | bullet_t *ret = malloc(sizeof(bullet_t));
|
104 | if(ret == NULL) return NULL; |
||
105 | 227 | up20180642 | ret-> x = x; |
106 | ret-> y = y; |
||
107 | ret->vx = vx; |
||
108 | ret->vy = vy; |
||
109 | 230 | up20180655 | ret->damage = 1;
|
110 | 203 | up20180642 | ret->b = sprite_ctor(b); |
111 | if(ret->b == NULL){ |
||
112 | bullet_dtor(ret); |
||
113 | return NULL; |
||
114 | 227 | up20180642 | } |
115 | double angle = atan2(-ret->vy, ret->vx);
|
||
116 | sprite_set_angle(ret->b, angle-M_PI_2); |
||
117 | return ret;
|
||
118 | 203 | up20180642 | } |
119 | void (bullet_dtor)(bullet_t *p){
|
||
120 | if(p == NULL) return; |
||
121 | sprite_dtor(p->b); |
||
122 | free(p); |
||
123 | } |
||
124 | double (bullet_get_x) (const bullet_t *p){ return p->x; } |
||
125 | double (bullet_get_y) (const bullet_t *p){ return p->y; } |
||
126 | int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
||
127 | int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
||
128 | 230 | up20180655 | int (bullet_get_damage) (const bullet_t *p){ return p->damage; } |
129 | void (bullet_set_damage) (bullet_t *p, int damage) { |
||
130 | if (damage < 0) damage = 0; |
||
131 | p->damage = damage; |
||
132 | } |
||
133 | 226 | up20180642 | void (bullet_update_movement)(bullet_t *p){
|
134 | p->x += p->vx; |
||
135 | p->y += p->vy; |
||
136 | } |
||
137 | 203 | up20180642 | void (bullet_draw)(bullet_t *p){
|
138 | const int16_t x_screen = bullet_get_x_screen(p);
|
||
139 | const int16_t y_screen = bullet_get_y_screen(p);
|
||
140 | sprite_set_pos (p->b, x_screen, y_screen); |
||
141 | sprite_set_scale(p->b, scale); |
||
142 | sprite_draw (p->b); |
||
143 | } |
||
144 | 216 | up20180642 | |
145 | struct map{
|
||
146 | basic_sprite_t *bsp_background; |
||
147 | sprite_t *background; |
||
148 | uint8_t *collide; |
||
149 | }; |
||
150 | map_t* (map_ctor)(const char **background, const char **collide){ |
||
151 | map_t *ret = malloc(sizeof(map_t));
|
||
152 | if(ret == NULL) return NULL; |
||
153 | |||
154 | ret->bsp_background = NULL;
|
||
155 | ret->background = NULL;
|
||
156 | ret->collide = NULL;
|
||
157 | |||
158 | ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
||
159 | ret->background = sprite_ctor(ret->bsp_background); |
||
160 | if(ret->bsp_background == NULL || |
||
161 | ret->background == NULL){ map_dtor(ret); return NULL; } |
||
162 | |||
163 | basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
||
164 | if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
||
165 | const uint16_t W = basic_sprite_get_w(bsp_collide);
|
||
166 | const uint16_t H = basic_sprite_get_h(bsp_collide);
|
||
167 | ret->collide = malloc(W*H*sizeof(uint8_t));
|
||
168 | if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
||
169 | const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
||
170 | for(unsigned i = 0; i < W*H; ++i){ |
||
171 | ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
||
172 | } |
||
173 | basic_sprite_dtor(bsp_collide); |
||
174 | |||
175 | return ret;
|
||
176 | } |
||
177 | void (map_dtor)(map_t *p){
|
||
178 | if(p == NULL) return; |
||
179 | sprite_dtor(p->background); |
||
180 | basic_sprite_dtor(p->bsp_background); |
||
181 | free(p->collide); |
||
182 | free(p); |
||
183 | } |
||
184 | int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
||
185 | int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
||
186 | 220 | up20180655 | int (map_collides_point)(const map_t *p, double x, double y){ |
187 | 216 | up20180642 | const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
188 | int16_t x_ = x, y_ = y; |
||
189 | if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
||
190 | uint32_t pos = x_ + y_*w; |
||
191 | return p->collide[pos];
|
||
192 | } |
||
193 | 220 | up20180655 | |
194 | 226 | up20180642 | int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
195 | 228 | up20180655 | double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
196 | 220 | up20180655 | double shooter_x = gunner_get_x(shooter);
|
197 | 224 | up20180655 | double shooter_y = gunner_get_y(shooter);
|
198 | for (double x = -radius; x < radius; x += 1) { |
||
199 | double y1 = sqrt(radius*radius - x*x);
|
||
200 | 220 | up20180655 | double y2 = -y1;
|
201 | 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; |
202 | 220 | up20180655 | } |
203 | return 0; |
||
204 | } |
||
205 | |||
206 | 226 | up20180642 | int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
207 | double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
||
208 | double bullet_x = bullet_get_x(bull);
|
||
209 | double bullet_y = bullet_get_y(bull);
|
||
210 | for (double x = -radius; x < radius; x += 1){ |
||
211 | double y1 = sqrt(radius*radius - x*x);
|
||
212 | double y2 = -y1;
|
||
213 | if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
||
214 | } |
||
215 | 220 | up20180655 | return 0; |
216 | } |
||
217 | |||
218 | 228 | up20180655 | int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){ |
219 | double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
||
220 | double shooter_x = gunner_get_x(shooter);
|
||
221 | double shooter_y = gunner_get_y(shooter);
|
||
222 | |||
223 | double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
||
224 | double bullet_x = bullet_get_x(bull);
|
||
225 | double bullet_y = bullet_get_y(bull);
|
||
226 | |||
227 | double dx = shooter_x - bullet_x;
|
||
228 | double dy = shooter_y - bullet_y;
|
||
229 | double distance = sqrt(dx*dx + dy*dy);
|
||
230 | return distance <= shooter_radius+bullet_radius;
|
||
231 | } |
||
232 | |||
233 | 216 | up20180642 | void (map_draw)(map_t *p){
|
234 | const int16_t x_screen = map_get_x_screen(p);
|
||
235 | const int16_t y_screen = map_get_y_screen(p);
|
||
236 | sprite_set_pos (p->background, x_screen, y_screen); |
||
237 | sprite_set_scale(p->background, scale); |
||
238 | sprite_draw (p->background); |
||
239 | } |