Project

General

Profile

Revision 236

improved bullet stuff

View differences:

proj/include/ent.h
16 16
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon);
17 17
void      (gunner_dtor)(gunner_t *p);
18 18
void (gunner_set_pos)               (gunner_t *p, double x, double y);
19
void  (gunner_set_spawn)        (gunner_t *p, double x, double y);
19
void (gunner_set_spawn)             (gunner_t *p, double x, double y);
20 20
void (gunner_set_angle)             (gunner_t *p, double angle      );
21 21
void (gunner_set_health)            (gunner_t *p, int health);
22 22
void (gunner_set_curr_health)       (gunner_t *p, int health);
......
34 34

  
35 35
struct bullet;
36 36
typedef struct bullet bullet_t;
37
bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy);
37
bullet_t* (bullet_ctor)(const gunner_t *shooter, basic_sprite_t *b, double x, double y, double vx, double vy);
38 38
void      (bullet_dtor)(bullet_t *p);
39 39
double  (bullet_get_x)       (const bullet_t *p);
40 40
double  (bullet_get_y)       (const bullet_t *p);
proj/include/proj_func.h
36 36

  
37 37
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list);
38 38

  
39
void (update_game_state)(const map_t *map, gunner_t *shooter, list_t *bullet_list);
39
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list);
40 40

  
41 41
void update_scale(void);
42 42

  
proj/src/ent.c
101 101
}
102 102

  
103 103
struct bullet{
104
    const gunner_t *shooter;
104 105
    double x, y; //real position
105 106
    double vx, vy;
106 107
    sprite_t *b;
107 108
    int damage;
108 109
};
109
bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy){
110
bullet_t* (bullet_ctor)(const gunner_t *shooter, basic_sprite_t *b, double x, double y, double vx, double vy){
110 111
    bullet_t *ret = malloc(sizeof(bullet_t));
111 112
    if(ret == NULL) return NULL;
113
    ret->shooter = shooter;
112 114
    ret-> x =  x;
113 115
    ret-> y =  y;
114 116
    ret->vx = vx;
......
246 248
}
247 249

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

  
249 253
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
250 254
    double shooter_x = gunner_get_x(shooter);
251 255
    double shooter_y = gunner_get_y(shooter);
proj/src/proj.c
54 54
    int r;
55 55

  
56 56
    #ifdef DIOGO
57
        /*
57 58
        uint8_t conf;
58 59
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
59 60
        conf = 0x19; //00011001
60 61
        //printf("0x%02X\n", conf);
61 62
        if((r = sys_outb(0x3F8+3, conf))) return 1;
62 63
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
64
        */
63 65
    #endif
64 66

  
65 67
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
......
177 179
    #ifdef TELMO
178 180
        ent_set_scale(DEFAULT_SCALE);
179 181

  
182
        list_t *shooter_list = list_ctor();
183

  
180 184
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
181 185
        gunner_set_spawn(shooter1, 75, 75);
182 186
        gunner_set_pos(shooter1, 75, 75);
......
185 189
        gunner_set_spawn(shooter2, 975, 75);
186 190
        gunner_set_pos(shooter2, 775, 75);
187 191

  
192
        list_insert(shooter_list, list_end(shooter_list), shooter1);
193
        list_insert(shooter_list, list_end(shooter_list), shooter2);
194

  
188 195
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
189 196

  
190
        list_t *bullet_list = list_ctor();
197
        list_t *bullet_list  = list_ctor();
191 198

  
192 199
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
193 200
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
......
227 234

  
228 235
                                    if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
229 236

  
230
                                    update_game_state(map1, shooter2, bullet_list);
237
                                    update_game_state(map1, shooter_list, bullet_list);
231 238

  
232 239
                                    if(map_collides_gunner(map1, shooter1)){
233 240
                                        printf("COLLIDING\n");
......
288 295
    }
289 296

  
290 297
    #ifdef TELMO
291
        gunner_dtor(shooter1); shooter1 = NULL;
292
        gunner_dtor(shooter2); shooter2 = NULL;
298
        //gunner_dtor(shooter1); shooter1 = NULL;
299
        //gunner_dtor(shooter2); shooter2 = NULL;
293 300

  
301
        while(list_size(shooter_list) > 0){
302
            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
303
            gunner_dtor(p);
304
        }
305

  
294 306
        while(list_size(bullet_list) > 0){
295 307
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
296
            free(p);
308
            bullet_dtor(p);
297 309
        }
298 310
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
299 311
    #endif
proj/src/proj_func.c
70 70
    double angle = gunner_get_angle(shooter);
71 71
    double vx = -BULLET_SPEED * fm_sin(angle);
72 72
    double vy = -BULLET_SPEED * fm_cos(angle);
73
    bullet_t *bullet = bullet_ctor(get_bullet(), gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
73
    bullet_t *bullet = bullet_ctor(shooter, get_bullet(), gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
74 74
    list_insert(bullet_list, list_end(bullet_list), bullet);
75 75
}
76 76

  
77
void (update_game_state)(const map_t *map, gunner_t *shooter, list_t *bullet_list) {
77
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
78 78

  
79 79
    bullet_update_movement_list(bullet_list);
80 80

  
81
    list_node_t *it = list_begin(bullet_list);
82
    while (it != list_end(bullet_list)) {
83
        bullet_t *bullet = *(bullet_t**)list_node_val(it);
81
    list_node_t *bullet_it = list_begin(bullet_list);
82
    while (bullet_it != list_end(bullet_list)) {
83
        /// Collision with walls
84
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
84 85
        if (map_collides_bullet(map, bullet)) {
85
            list_node_t *aux = list_node_next(it);
86
            bullet = (bullet_t*)list_erase(bullet_list, it);
87
            free(bullet);
88
            it = aux;
86
            list_node_t *aux = list_node_next(bullet_it);
87
            /// Delete bullet
88
            bullet_dtor(list_erase(bullet_list, bullet_it));
89
            /// Advance iterator
90
            bullet_it = aux;
89 91
            continue;
90 92
        }
91

  
92
        if (gunner_collides_bullet(shooter, bullet)) {
93
            list_node_t *aux = list_node_next(it);
94
            bullet = (bullet_t*)list_erase(bullet_list, it);
95
            gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
96
            if (gunner_get_curr_health(shooter) <= 0) {
97
                gunner_set_curr_health(shooter, gunner_get_health(shooter));
98
                gunner_set_pos(shooter, gunner_get_spawn_x(shooter), gunner_get_spawn_y(shooter));
99
            }
100

  
101
            free(bullet);
102
            it = aux;
103
            continue;
93
        /// Collision with shooters
94
        list_node_t *shooter_it = list_begin(shooter_list);
95
        int deleted_bullet = false;
96
        while(shooter_it != list_end(shooter_list)){
97
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
98
            if(gunner_collides_bullet(shooter, bullet)) {
99
                list_node_t *aux = list_node_next(bullet_it);
100
                /// Change health
101
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
102
                if (gunner_get_curr_health(shooter) <= 0) {
103
                    shooter = list_erase(shooter_list, shooter_it);
104
                    gunner_dtor(shooter);
105
                }
106
                /// Delete bullet
107
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
108
                /// Advance iterator
109
                bullet_it = aux;
110
                break;
111
            } else shooter_it = list_node_next(shooter_it);
104 112
        }
105

  
106
        it = list_node_next(it);
113
        if(deleted_bullet) continue;
114
        /// Advance iterator if necessary
115
        bullet_it = list_node_next(bullet_it);
107 116
    }
108 117
}
109 118

  

Also available in: Unified diff