Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 245

History | View | Annotate | Download (10.1 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
    double spawn_x, spawn_y;
24
    sprite_t *dude;
25
    sprite_t *weapon;
26
    int health, current_health;
27
};
28
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){
29
    gunner_t *ret = malloc(sizeof(gunner_t));
30
    if(ret == NULL) return NULL;
31
    ret->spawn_x = 0.0;
32
    ret->spawn_y = 0.0;
33
    ret->x = 0.0;
34
    ret->y = 0.0;
35
    ret->health = 100;
36
    ret->current_health = ret->health;
37
    ret->dude   = sprite_ctor(dude  );
38
    ret->weapon = sprite_ctor(weapon);
39
    if(ret->dude == NULL || ret->weapon == NULL){
40
        gunner_dtor(ret);
41
        return NULL;
42
    } else return ret;
43
}
44
void (gunner_dtor)(gunner_t *p){
45
    if(p == NULL) return;
46
    sprite_dtor(p->dude);
47
    sprite_dtor(p->weapon);
48
    free(p);
49
}
50
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
51
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
52
void (gunner_set_angle)(gunner_t *p, double angle      ){
53
    sprite_set_angle(p->dude  , angle);
54
    sprite_set_angle(p->weapon, angle);
55
}
56
void (gunner_set_health)        (gunner_t *p, int health) {
57
    if (health < 0) health = 0;
58
    p->health = health;
59
}
60
void (gunner_set_curr_health)   (gunner_t *p, int health) {
61
    if (health < 0) health = 0;
62
    p->current_health = health;
63
}
64
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
65
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
66
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
67
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
68
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
69
int     (gunner_get_health)         (const gunner_t *p){ return p->health; }
70
int     (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
71
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
72
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
73
void (gunner_draw)(gunner_t *p){
74
    const int16_t x_screen = gunner_get_x_screen(p);
75
    const int16_t y_screen = gunner_get_y_screen(p);
76
    sprite_set_pos  (p->dude  , x_screen, y_screen);
77
    sprite_set_pos  (p->weapon, x_screen, y_screen);
78
    sprite_set_scale(p->dude  , scale);
79
    sprite_set_scale(p->weapon, scale);
80
    sprite_draw     (p->weapon);
81
    sprite_draw     (p->dude  );
82
    gunner_draw_health(p);
83
}
84

    
85
void (gunner_draw_health)(const gunner_t *p) {
86
    int16_t w = sprite_get_w(p->dude);
87
    int16_t h = sprite_get_h(p->dude);
88
    double x = gunner_get_x_screen(p) - w/2;
89
    double y = gunner_get_y_screen(p) - h/2 - 10;
90
    int curr_health = gunner_get_curr_health(p);
91
    int health = gunner_get_health(p);
92
    double perc = (double)curr_health/health;
93
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
94
    rectangle_set_fill_color(green_bar, 0x00FF00);
95
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
96
    rectangle_set_fill_color(red_bar, 0xFF0000);
97
    rectangle_draw(green_bar);
98
    rectangle_draw(red_bar);
99
    rectangle_dtor(green_bar);
100
    rectangle_dtor(red_bar);
101
}
102

    
103
struct bullet{
104
    const gunner_t *shooter;
105
    double x, y; //real position
106
    double vx, vy;
107
    sprite_t *b;
108
    int damage;
109
};
110
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
111
    bullet_t *ret = malloc(sizeof(bullet_t));
112
    if(ret == NULL) return NULL;
113
    ret->shooter = shooter;
114
    ret-> x =  x;
115
    ret-> y =  y;
116
    ret->vx = vx;
117
    ret->vy = vy;
118
    ret->damage = 10;
119
    ret->b = sprite_ctor(b);
120
    if(ret->b == NULL){
121
        bullet_dtor(ret);
122
        return NULL;
123
    }
124
    double angle = atan2(-ret->vy, ret->vx);
125
    sprite_set_angle(ret->b, angle-M_PI_2);
126
    return ret;
127
}
128
void (bullet_dtor)(bullet_t *p){
129
    if(p == NULL) return;
130
    sprite_dtor(p->b);
131
    free(p);
132
}
133
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
134
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
135
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
136
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
137
int     (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
138
void    (bullet_set_damage)  (bullet_t *p, int damage) {
139
    if (damage < 0) damage = 0;
140
    p->damage = damage;
141
}
142
void (bullet_update_movement)(bullet_t *p){
143
    p->x += p->vx;
144
    p->y += p->vy;
145
}
146

    
147
void (bullet_update_movement_list)(list_t *bullet_list){
148
    if (bullet_list == NULL) return;
149
    if (list_size(bullet_list) == 0) return;
150

    
151
    list_node_t *it = list_begin(bullet_list);
152
    while (it != list_end(bullet_list)) {
153
        bullet_update_movement(*(bullet_t**)list_node_val(it));
154
        it = list_node_next(it);
155
    }
156
}
157

    
158
void (bullet_draw)(bullet_t *p){
159
    const int16_t x_screen = bullet_get_x_screen(p);
160
    const int16_t y_screen = bullet_get_y_screen(p);
161
    sprite_set_pos  (p->b, x_screen, y_screen);
162
    sprite_set_scale(p->b, scale);
163
    sprite_draw     (p->b);
164
}
165

    
166
void (bullet_draw_list)(list_t *bullet_list) {
167
    if (bullet_list == NULL) return;
168
    if (list_size(bullet_list) == 0) return;
169

    
170
    list_node_t *it = list_begin(bullet_list);
171
    while (it != list_end(bullet_list)) {
172
        bullet_draw(*(bullet_t**)list_node_val(it));
173
        it = list_node_next(it);
174
    }
175
}
176

    
177
struct map{
178
    basic_sprite_t *bsp_background;
179
    sprite_t *background;
180
    uint8_t *collide;
181
};
182
map_t* (map_ctor)(const char **background, const char **collide){
183
    map_t *ret = malloc(sizeof(map_t));
184
    if(ret == NULL) return NULL;
185

    
186
    ret->bsp_background = NULL;
187
    ret->background     = NULL;
188
    ret->collide        = NULL;
189

    
190
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
191
    ret->background     = sprite_ctor(ret->bsp_background);
192
    if(ret->bsp_background == NULL ||
193
        ret->background     == NULL){ map_dtor(ret); return NULL; }
194

    
195
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
196
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
197
    const uint16_t W = basic_sprite_get_w(bsp_collide);
198
    const uint16_t H = basic_sprite_get_h(bsp_collide);
199
    ret->collide = malloc(W*H*sizeof(uint8_t));
200
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
201
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
202
    for(unsigned i = 0; i < W*H; ++i){
203
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
204
    }
205
    basic_sprite_dtor(bsp_collide);
206

    
207
    return ret;
208
}
209
void (map_dtor)(map_t *p){
210
    if(p == NULL) return;
211
    sprite_dtor(p->background);
212
    basic_sprite_dtor(p->bsp_background);
213
    free(p->collide);
214
    free(p);
215
}
216
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
217
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
218
int (map_collides_point)(const map_t *p, double x, double y){
219
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
220
    int16_t x_ = x, y_ = y;
221
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
222
    uint32_t pos = x_ + y_*w;
223
    return p->collide[pos];
224
}
225

    
226
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
227
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
228
    double shooter_x = gunner_get_x(shooter);
229
    double shooter_y = gunner_get_y(shooter);
230
    for (double x = -radius; x < radius; x += 1) {
231
        double y1 = sqrt(radius*radius - x*x);
232
        double y2 = -y1;
233
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
234
    }
235
    return 0;
236
}
237

    
238
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
239
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
240
    double bullet_x = bullet_get_x(bull);
241
    double bullet_y = bullet_get_y(bull);
242
    for (double x = -radius; x < radius; x += 1){
243
        double y1 = sqrt(radius*radius - x*x);
244
        double y2 = -y1;
245
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
246
    }
247
    return 0;
248
}
249

    
250
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
251
    if(bull->shooter == shooter) return false;
252

    
253
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
254
    double shooter_x = gunner_get_x(shooter);
255
    double shooter_y = gunner_get_y(shooter);
256

    
257
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
258
    double bullet_x = bullet_get_x(bull);
259
    double bullet_y = bullet_get_y(bull);
260

    
261
    double dx = shooter_x - bullet_x;
262
    double dy = shooter_y - bullet_y;
263
    double distance = sqrt(dx*dx + dy*dy);
264
    return distance <= shooter_radius+bullet_radius;
265
}
266

    
267
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
268
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
269
    double shooter1_x = gunner_get_x(shooter1);
270
    double shooter1_y = gunner_get_y(shooter1);
271

    
272
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
273
    double shooter2_x = gunner_get_x(shooter2);
274
    double shooter2_y = gunner_get_y(shooter2);
275

    
276
    double dx = shooter1_x - shooter2_x;
277
    double dy = shooter1_y - shooter2_y;
278
    double distance = sqrt(dx*dx + dy*dy);
279
    return distance <= shooter1_radius+shooter2_radius;
280
}
281

    
282
void   (map_draw)(map_t *p){
283
    const int16_t x_screen = map_get_x_screen(p);
284
    const int16_t y_screen = map_get_y_screen(p);
285
    sprite_set_pos  (p->background, x_screen, y_screen);
286
    sprite_set_scale(p->background, scale);
287
    sprite_draw     (p->background);
288
}