root / proj / src / ent.c @ 216
History | View | Annotate | Download (5.02 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "ent.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "sprite.h" |
7 |
|
8 |
static double scale = 1.0; |
9 |
static int16_t x_origin = 0; |
10 |
static int16_t y_origin = 0; |
11 |
|
12 |
void (ent_set_scale) (double n){ scale = n; } |
13 |
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; } |
14 |
double (ent_get_scale) (void){ return scale; } |
15 |
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; } |
16 |
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; } |
17 |
|
18 |
struct gunner{
|
19 |
double x, y; //real position |
20 |
sprite_t *dude; |
21 |
sprite_t *weapon; |
22 |
}; |
23 |
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){ |
24 |
gunner_t *ret = malloc(sizeof(gunner_t));
|
25 |
if(ret == NULL) return NULL; |
26 |
ret->x = 0.0; |
27 |
ret->y = 0.0; |
28 |
ret->dude = sprite_ctor(dude ); |
29 |
ret->weapon = sprite_ctor(weapon); |
30 |
if(ret->dude == NULL || ret->weapon == NULL){ |
31 |
gunner_dtor(ret); |
32 |
return NULL; |
33 |
} else return ret; |
34 |
} |
35 |
void (gunner_dtor)(gunner_t *p){
|
36 |
if(p == NULL) return; |
37 |
sprite_dtor(p->dude); |
38 |
sprite_dtor(p->weapon); |
39 |
free(p); |
40 |
} |
41 |
void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
42 |
void (gunner_set_angle)(gunner_t *p, double angle ){ |
43 |
sprite_set_angle(p->dude , angle); |
44 |
sprite_set_angle(p->weapon, angle); |
45 |
} |
46 |
double (gunner_get_x) (const gunner_t *p){ return p->x; } |
47 |
double (gunner_get_y) (const gunner_t *p){ return p->y; } |
48 |
int16_t (gunner_get_x_screen)(const gunner_t *p){ return (p->x-x_origin)*scale; } |
49 |
int16_t (gunner_get_y_screen)(const gunner_t *p){ return (p->y-y_origin)*scale; } |
50 |
void (gunner_draw)(gunner_t *p){
|
51 |
const int16_t x_screen = gunner_get_x_screen(p);
|
52 |
const int16_t y_screen = gunner_get_y_screen(p);
|
53 |
sprite_set_pos (p->dude , x_screen, y_screen); |
54 |
sprite_set_pos (p->weapon, x_screen, y_screen); |
55 |
sprite_set_scale(p->dude , scale); |
56 |
sprite_set_scale(p->weapon, scale); |
57 |
sprite_draw (p->weapon); |
58 |
sprite_draw (p->dude ); |
59 |
} |
60 |
|
61 |
struct bullet{
|
62 |
double x, y; //real position |
63 |
sprite_t *b; |
64 |
}; |
65 |
bullet_t* (bullet_ctor)(basic_sprite_t *b){ |
66 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
67 |
if(ret == NULL) return NULL; |
68 |
ret->x = 0.0; |
69 |
ret->y = 0.0; |
70 |
ret->b = sprite_ctor(b); |
71 |
if(ret->b == NULL){ |
72 |
bullet_dtor(ret); |
73 |
return NULL; |
74 |
}else return ret; |
75 |
} |
76 |
void (bullet_dtor)(bullet_t *p){
|
77 |
if(p == NULL) return; |
78 |
sprite_dtor(p->b); |
79 |
free(p); |
80 |
} |
81 |
void (bullet_set_pos) (bullet_t *p, double x, double y){ p->x = x; p->y = y; } |
82 |
void (bullet_set_angle)(bullet_t *p, double angle ){ sprite_set_angle(p->b, angle); } |
83 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
84 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
85 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
86 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
87 |
void (bullet_draw)(bullet_t *p){
|
88 |
const int16_t x_screen = bullet_get_x_screen(p);
|
89 |
const int16_t y_screen = bullet_get_y_screen(p);
|
90 |
sprite_set_pos (p->b, x_screen, y_screen); |
91 |
sprite_set_scale(p->b, scale); |
92 |
sprite_draw (p->b); |
93 |
} |
94 |
|
95 |
struct map{
|
96 |
basic_sprite_t *bsp_background; |
97 |
sprite_t *background; |
98 |
uint8_t *collide; |
99 |
}; |
100 |
map_t* (map_ctor)(const char **background, const char **collide){ |
101 |
map_t *ret = malloc(sizeof(map_t));
|
102 |
if(ret == NULL) return NULL; |
103 |
|
104 |
ret->bsp_background = NULL;
|
105 |
ret->background = NULL;
|
106 |
ret->collide = NULL;
|
107 |
|
108 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
109 |
ret->background = sprite_ctor(ret->bsp_background); |
110 |
if(ret->bsp_background == NULL || |
111 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
112 |
|
113 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
114 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
115 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
116 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
117 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
118 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
119 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
120 |
for(unsigned i = 0; i < W*H; ++i){ |
121 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
122 |
} |
123 |
basic_sprite_dtor(bsp_collide); |
124 |
|
125 |
return ret;
|
126 |
} |
127 |
void (map_dtor)(map_t *p){
|
128 |
if(p == NULL) return; |
129 |
sprite_dtor(p->background); |
130 |
basic_sprite_dtor(p->bsp_background); |
131 |
free(p->collide); |
132 |
free(p); |
133 |
} |
134 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
135 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
136 |
int (map_collides)(const map_t *p, double x, double y){ |
137 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
138 |
int16_t x_ = x, y_ = y; |
139 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
140 |
uint32_t pos = x_ + y_*w; |
141 |
return p->collide[pos];
|
142 |
} |
143 |
void (map_draw)(map_t *p){
|
144 |
const int16_t x_screen = map_get_x_screen(p);
|
145 |
const int16_t y_screen = map_get_y_screen(p);
|
146 |
sprite_set_pos (p->background, x_screen, y_screen); |
147 |
sprite_set_scale(p->background, scale); |
148 |
sprite_draw (p->background); |
149 |
} |