#include "bullet.h" #include "conf.h" #include "level.h" #include #include #include static struct Bullet_table bullet_table; static void bullet_table_free(void); static void bullet_update(struct Bullet *bullet); static void bullet_draw(struct Bullet *bullet, int timer); extern bopti_image_t img_bullet; void bullet_table_init(void) { bullet_table_free(); bullet_table.bullets = malloc(MAX_BULLETS * sizeof(struct Bullet)); bullet_table.n = 0; for (int i = 0; i < MAX_BULLETS; ++i) { bullet_table.bullets[i].active = 0; } } void bullet_table_free(void) { if (bullet_table.bullets != NULL) { bullet_table.bullets = NULL; } }; void bullet_fire(int x, int y, int v) { for (int i = 0; i < MAX_BULLETS; ++i) { if (!bullet_table.bullets[i].active) { bullet_table.bullets[i] = (struct Bullet){ .pos = (struct Vec2){.x = x, .y = y}, .v = v, .active = 1}; break; } } ++bullet_table.n; } void bullet_table_update(void) { for (int i = 0; i < MAX_BULLETS; ++i) { if (bullet_table.bullets[i].active) { bullet_update(&bullet_table.bullets[i]); } } } void bullet_update(struct Bullet *bullet) { bullet->pos.x += bullet->v; /* boom */ const struct Vec2 level_dim = level_get_dim(); if (bullet->pos.x < -6 || bullet->pos.x > level_dim.x * TILE_S || bullet->pos.y < -6 || bullet->pos.y > level_dim.y * TILE_S) { bullet_destroy(bullet); } } void bullet_table_draw(int timer) { for (int i = 0; i < MAX_BULLETS; ++i) { if (bullet_table.bullets[i].active) { bullet_draw(&bullet_table.bullets[i], timer); } } } void bullet_draw(struct Bullet *bullet, int timer) { dsubimage(bullet->pos.x, bullet->pos.y, &img_bullet, (timer / 2) % 2 * 6, 0, 6, 4, 0); } void bullet_destroy(struct Bullet *bullet) { bullet->active = 0; --bullet_table.n; } int bullet_nb(void) { return bullet_table.n; }