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