root / proj / src / ent.c @ 224
History | View | Annotate | Download (5.6 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "ent.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "sprite.h" |
7 |
|
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 |
sprite_t *b; |
66 |
}; |
67 |
bullet_t* (bullet_ctor)(basic_sprite_t *b){ |
68 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
69 |
if(ret == NULL) return NULL; |
70 |
ret->x = 0.0; |
71 |
ret->y = 0.0; |
72 |
ret->b = sprite_ctor(b); |
73 |
if(ret->b == NULL){ |
74 |
bullet_dtor(ret); |
75 |
return NULL; |
76 |
}else return ret; |
77 |
} |
78 |
void (bullet_dtor)(bullet_t *p){
|
79 |
if(p == NULL) return; |
80 |
sprite_dtor(p->b); |
81 |
free(p); |
82 |
} |
83 |
void (bullet_set_pos) (bullet_t *p, double x, double y){ p->x = x; p->y = y; } |
84 |
void (bullet_set_angle)(bullet_t *p, double angle ){ sprite_set_angle(p->b, angle); } |
85 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
86 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
87 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
88 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
89 |
void (bullet_draw)(bullet_t *p){
|
90 |
const int16_t x_screen = bullet_get_x_screen(p);
|
91 |
const int16_t y_screen = bullet_get_y_screen(p);
|
92 |
sprite_set_pos (p->b, x_screen, y_screen); |
93 |
sprite_set_scale(p->b, scale); |
94 |
sprite_draw (p->b); |
95 |
} |
96 |
|
97 |
struct map{
|
98 |
basic_sprite_t *bsp_background; |
99 |
sprite_t *background; |
100 |
uint8_t *collide; |
101 |
}; |
102 |
map_t* (map_ctor)(const char **background, const char **collide){ |
103 |
map_t *ret = malloc(sizeof(map_t));
|
104 |
if(ret == NULL) return NULL; |
105 |
|
106 |
ret->bsp_background = NULL;
|
107 |
ret->background = NULL;
|
108 |
ret->collide = NULL;
|
109 |
|
110 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
111 |
ret->background = sprite_ctor(ret->bsp_background); |
112 |
if(ret->bsp_background == NULL || |
113 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
114 |
|
115 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
116 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
117 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
118 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
119 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
120 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
121 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
122 |
for(unsigned i = 0; i < W*H; ++i){ |
123 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
124 |
} |
125 |
basic_sprite_dtor(bsp_collide); |
126 |
|
127 |
return ret;
|
128 |
} |
129 |
void (map_dtor)(map_t *p){
|
130 |
if(p == NULL) return; |
131 |
sprite_dtor(p->background); |
132 |
basic_sprite_dtor(p->bsp_background); |
133 |
free(p->collide); |
134 |
free(p); |
135 |
} |
136 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
137 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
138 |
int (map_collides_point)(const map_t *p, double x, double y){ |
139 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
140 |
int16_t x_ = x, y_ = y; |
141 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
142 |
uint32_t pos = x_ + y_*w; |
143 |
return p->collide[pos];
|
144 |
} |
145 |
|
146 |
int (map_collides_gunner)(const map_t *p, gunner_t *shooter) { |
147 |
double radius = sprite_get_w(shooter->dude)/2.0; |
148 |
double shooter_x = gunner_get_x(shooter);
|
149 |
double shooter_y = gunner_get_y(shooter);
|
150 |
for (double x = -radius; x < radius; x += 1) { |
151 |
double y1 = sqrt(radius*radius - x*x);
|
152 |
double y2 = -y1;
|
153 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
154 |
} |
155 |
return 0; |
156 |
} |
157 |
|
158 |
int (map_collides_bullet)(const map_t *p, bullet_t *bullet) { |
159 |
return 0; |
160 |
} |
161 |
|
162 |
void (map_draw)(map_t *p){
|
163 |
const int16_t x_screen = map_get_x_screen(p);
|
164 |
const int16_t y_screen = map_get_y_screen(p);
|
165 |
sprite_set_pos (p->background, x_screen, y_screen); |
166 |
sprite_set_scale(p->background, scale); |
167 |
sprite_draw (p->background); |
168 |
} |