root / proj / src / ent.c @ 226
History | View | Annotate | Download (6.17 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 <math.h> |
9 |
|
10 |
static double scale = 1.0; |
11 |
static int16_t x_origin = 0; |
12 |
static int16_t y_origin = 0; |
13 |
|
14 |
void (ent_set_scale) (double n){ scale = n; } |
15 |
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; } |
16 |
double (ent_get_scale) (void){ return scale; } |
17 |
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; } |
18 |
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; } |
19 |
|
20 |
struct gunner{
|
21 |
double x, y; //real position |
22 |
sprite_t *dude; |
23 |
sprite_t *weapon; |
24 |
}; |
25 |
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){ |
26 |
gunner_t *ret = malloc(sizeof(gunner_t));
|
27 |
if(ret == NULL) return NULL; |
28 |
ret->x = 0.0; |
29 |
ret->y = 0.0; |
30 |
ret->dude = sprite_ctor(dude ); |
31 |
ret->weapon = sprite_ctor(weapon); |
32 |
if(ret->dude == NULL || ret->weapon == NULL){ |
33 |
gunner_dtor(ret); |
34 |
return NULL; |
35 |
} else return ret; |
36 |
} |
37 |
void (gunner_dtor)(gunner_t *p){
|
38 |
if(p == NULL) return; |
39 |
sprite_dtor(p->dude); |
40 |
sprite_dtor(p->weapon); |
41 |
free(p); |
42 |
} |
43 |
void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
44 |
void (gunner_set_angle)(gunner_t *p, double angle ){ |
45 |
sprite_set_angle(p->dude , angle); |
46 |
sprite_set_angle(p->weapon, angle); |
47 |
} |
48 |
double (gunner_get_x) (const gunner_t *p){ return p->x; } |
49 |
double (gunner_get_y) (const gunner_t *p){ return p->y; } |
50 |
int16_t (gunner_get_x_screen)(const gunner_t *p){ return (p->x-x_origin)*scale; } |
51 |
int16_t (gunner_get_y_screen)(const gunner_t *p){ return (p->y-y_origin)*scale; } |
52 |
void (gunner_draw)(gunner_t *p){
|
53 |
const int16_t x_screen = gunner_get_x_screen(p);
|
54 |
const int16_t y_screen = gunner_get_y_screen(p);
|
55 |
sprite_set_pos (p->dude , x_screen, y_screen); |
56 |
sprite_set_pos (p->weapon, x_screen, y_screen); |
57 |
sprite_set_scale(p->dude , scale); |
58 |
sprite_set_scale(p->weapon, scale); |
59 |
sprite_draw (p->weapon); |
60 |
sprite_draw (p->dude ); |
61 |
} |
62 |
|
63 |
struct bullet{
|
64 |
double x, y; //real position |
65 |
double vx, vy;
|
66 |
sprite_t *b; |
67 |
}; |
68 |
bullet_t* (bullet_ctor)(basic_sprite_t *b){ |
69 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
70 |
if(ret == NULL) return NULL; |
71 |
ret-> x = 0.0; |
72 |
ret-> y = 0.0; |
73 |
ret->vx = 0.0; |
74 |
ret->vy = 0.0; |
75 |
ret->b = sprite_ctor(b); |
76 |
if(ret->b == NULL){ |
77 |
bullet_dtor(ret); |
78 |
return NULL; |
79 |
}else return ret; |
80 |
} |
81 |
void (bullet_dtor)(bullet_t *p){
|
82 |
if(p == NULL) return; |
83 |
sprite_dtor(p->b); |
84 |
free(p); |
85 |
} |
86 |
void (bullet_set_pos) (bullet_t *p, double x, double y){ p->x = x; p->y = y; } |
87 |
void (bullet_set_angle)(bullet_t *p, double angle ){ sprite_set_angle(p->b, angle); } |
88 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
89 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
90 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
91 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
92 |
void (bullet_update_movement)(bullet_t *p){
|
93 |
p->x += p->vx; |
94 |
p->y += p->vy; |
95 |
} |
96 |
void (bullet_draw)(bullet_t *p){
|
97 |
const int16_t x_screen = bullet_get_x_screen(p);
|
98 |
const int16_t y_screen = bullet_get_y_screen(p);
|
99 |
sprite_set_pos (p->b, x_screen, y_screen); |
100 |
sprite_set_scale(p->b, scale); |
101 |
sprite_draw (p->b); |
102 |
} |
103 |
|
104 |
struct map{
|
105 |
basic_sprite_t *bsp_background; |
106 |
sprite_t *background; |
107 |
uint8_t *collide; |
108 |
}; |
109 |
map_t* (map_ctor)(const char **background, const char **collide){ |
110 |
map_t *ret = malloc(sizeof(map_t));
|
111 |
if(ret == NULL) return NULL; |
112 |
|
113 |
ret->bsp_background = NULL;
|
114 |
ret->background = NULL;
|
115 |
ret->collide = NULL;
|
116 |
|
117 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
118 |
ret->background = sprite_ctor(ret->bsp_background); |
119 |
if(ret->bsp_background == NULL || |
120 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
121 |
|
122 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
123 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
124 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
125 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
126 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
127 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
128 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
129 |
for(unsigned i = 0; i < W*H; ++i){ |
130 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
131 |
} |
132 |
basic_sprite_dtor(bsp_collide); |
133 |
|
134 |
return ret;
|
135 |
} |
136 |
void (map_dtor)(map_t *p){
|
137 |
if(p == NULL) return; |
138 |
sprite_dtor(p->background); |
139 |
basic_sprite_dtor(p->bsp_background); |
140 |
free(p->collide); |
141 |
free(p); |
142 |
} |
143 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
144 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
145 |
int (map_collides_point)(const map_t *p, double x, double y){ |
146 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
147 |
int16_t x_ = x, y_ = y; |
148 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
149 |
uint32_t pos = x_ + y_*w; |
150 |
return p->collide[pos];
|
151 |
} |
152 |
|
153 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
154 |
double radius = sprite_get_w(shooter->dude)/2.0; |
155 |
double shooter_x = gunner_get_x(shooter);
|
156 |
double shooter_y = gunner_get_y(shooter);
|
157 |
for (double x = -radius; x < radius; x += 1) { |
158 |
double y1 = sqrt(radius*radius - x*x);
|
159 |
double y2 = -y1;
|
160 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
161 |
} |
162 |
return 0; |
163 |
} |
164 |
|
165 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
166 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
167 |
double bullet_x = bullet_get_x(bull);
|
168 |
double bullet_y = bullet_get_y(bull);
|
169 |
for (double x = -radius; x < radius; x += 1){ |
170 |
double y1 = sqrt(radius*radius - x*x);
|
171 |
double y2 = -y1;
|
172 |
if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
173 |
} |
174 |
return 0; |
175 |
} |
176 |
|
177 |
void (map_draw)(map_t *p){
|
178 |
const int16_t x_screen = map_get_x_screen(p);
|
179 |
const int16_t y_screen = map_get_y_screen(p);
|
180 |
sprite_set_pos (p->background, x_screen, y_screen); |
181 |
sprite_set_scale(p->background, scale); |
182 |
sprite_draw (p->background); |
183 |
} |