#include "game.h" #include static const float BULLET_SPEED = 600.0f; static void destroy_bullet(unsigned char index) { struct Bullet *b = game->bullets[index]; free(b); game->bullets[index] = NULL; game->bullets_left ++; } static void bullet_on_collision(struct Entity *this, struct Entity *other) { int i, j; // check if it is the enemy player // TODO: Ask if this is good practice struct Bullet *b = (struct Bullet *)this - offsetof(struct Bullet, entity); if((void *)b->owner == (void *)other) return; if(!this->collidable && !other->collidable) return; for(i = 0; i < game->num_entities; i ++) { if(game->entities[i] == this) { for(j = i; j < game->num_entities - 1; j ++) game->entities[j] = game->entities[j + 1]; //game->entities[i] = NULL; } } game->num_entities --; destroy_bullet(b->index); } void shoot_bullet(struct Player *owner) { int i, j; float speed, angle; struct Bullet *b; struct Gun *g = &owner->guns[owner->current_gun]; if(game->bullets_left < g->projectiles_per_shot || !g->ready || !g->bullets || owner->reloading) return; for(i = 0; i < g->projectiles_per_shot; i ++) { b = malloc( sizeof(struct Bullet) ); b->owner = owner; b->entity.on_collision = bullet_on_collision; b->entity.collidable = false; b->entity.use_src_rect = false; b->entity.texture = game->bullet_texture; b->entity.x = owner->entity.x; b->entity.y = owner->entity.y + 10; b->entity.w = BULLET_WIDTH; b->entity.h = BULLET_WIDTH; b->damage = g->damage; b->entity.name = "bullet"; angle = g->angle * ((float)rand() / (float)RAND_MAX) - g->angle / 2.0f; speed = (owner->entity.mirror ? -BULLET_SPEED : BULLET_SPEED); b->entity.dx = speed; b->entity.dy = SDL_sinf(angle) * 100.0f; for(j = 0; j < MAX_BULLETS; j ++) { if(!game->bullets[j]) { game->bullets[j] = b; b->index = j; break; } } game->bullets_left --; } g->bullets --; g->ready = false; g->cooldown_elapsed = 0.0f; Mix_PlayChannel(-1, g->sound_effect, 0); } void update_bullets(void) { int i; struct Bullet *b; for(i = 0; i < MAX_BULLETS; i ++) { b = game->bullets[i]; if(b) { b->entity.x += b->entity.dx * game->frame_time; b->entity.y += b->entity.dy * game->frame_time; if(b->entity.x > (float)game->width || b->entity.x < 0.0f || b->entity.y > (float)game->height || b->entity.y < 0.0f) { destroy_bullet(b->index); continue; } game->entities[game->num_entities ++] = &b->entity; } } }