Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 230

History | View | Annotate | Download (8.26 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
int     (gunner_get_health)         (const gunner_t *p){ return p->health; }
63
int     (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
64
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
65
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
66
void (gunner_draw)(gunner_t *p){
67
    const int16_t x_screen = gunner_get_x_screen(p);
68
    const int16_t y_screen = gunner_get_y_screen(p);
69
    sprite_set_pos  (p->dude  , x_screen, y_screen);
70
    sprite_set_pos  (p->weapon, x_screen, y_screen);
71
    sprite_set_scale(p->dude  , scale);
72
    sprite_set_scale(p->weapon, scale);
73
    sprite_draw     (p->weapon);
74
    sprite_draw     (p->dude  );
75
    gunner_draw_health(p);
76
}
77

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

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

    
145
struct map{
146
    basic_sprite_t *bsp_background;
147
    sprite_t *background;
148
    uint8_t *collide;
149
};
150
map_t* (map_ctor)(const char **background, const char **collide){
151
    map_t *ret = malloc(sizeof(map_t));
152
    if(ret == NULL) return NULL;
153

    
154
    ret->bsp_background = NULL;
155
    ret->background     = NULL;
156
    ret->collide        = NULL;
157

    
158
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
159
    ret->background     = sprite_ctor(ret->bsp_background);
160
    if(ret->bsp_background == NULL ||
161
        ret->background     == NULL){ map_dtor(ret); return NULL; }
162

    
163
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
164
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
165
    const uint16_t W = basic_sprite_get_w(bsp_collide);
166
    const uint16_t H = basic_sprite_get_h(bsp_collide);
167
    ret->collide = malloc(W*H*sizeof(uint8_t));
168
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
169
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
170
    for(unsigned i = 0; i < W*H; ++i){
171
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
172
    }
173
    basic_sprite_dtor(bsp_collide);
174

    
175
    return ret;
176
}
177
void (map_dtor)(map_t *p){
178
    if(p == NULL) return;
179
    sprite_dtor(p->background);
180
    basic_sprite_dtor(p->bsp_background);
181
    free(p->collide);
182
    free(p);
183
}
184
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
185
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
186
int (map_collides_point)(const map_t *p, double x, double y){
187
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
188
    int16_t x_ = x, y_ = y;
189
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
190
    uint32_t pos = x_ + y_*w;
191
    return p->collide[pos];
192
}
193

    
194
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
195
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
196
    double shooter_x = gunner_get_x(shooter);
197
    double shooter_y = gunner_get_y(shooter);
198
    for (double x = -radius; x < radius; x += 1) {
199
        double y1 = sqrt(radius*radius - x*x);
200
        double y2 = -y1;
201
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
202
    }
203
    return 0;
204
}
205

    
206
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
207
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
208
    double bullet_x = bullet_get_x(bull);
209
    double bullet_y = bullet_get_y(bull);
210
    for (double x = -radius; x < radius; x += 1){
211
        double y1 = sqrt(radius*radius - x*x);
212
        double y2 = -y1;
213
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
214
    }
215
    return 0;
216
}
217

    
218
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
219
    double shooter_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

    
223
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
224
    double bullet_x = bullet_get_x(bull);
225
    double bullet_y = bullet_get_y(bull);
226

    
227
    double dx = shooter_x - bullet_x;
228
    double dy = shooter_y - bullet_y;
229
    double distance = sqrt(dx*dx + dy*dy);
230
    return distance <= shooter_radius+bullet_radius;
231
}
232

    
233
void   (map_draw)(map_t *p){
234
    const int16_t x_screen = map_get_x_screen(p);
235
    const int16_t y_screen = map_get_y_screen(p);
236
    sprite_set_pos  (p->background, x_screen, y_screen);
237
    sprite_set_scale(p->background, scale);
238
    sprite_draw     (p->background);
239
}