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