Project

General

Profile

Revision 236

improved bullet stuff

View differences:

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