Skip to content

Instantly share code, notes, and snippets.

@dmoa
Created August 11, 2023 15:35
Show Gist options
  • Save dmoa/00c421138eb6515d1ff3b4f9e6dec31e to your computer and use it in GitHub Desktop.
Save dmoa/00c421138eb6515d1ff3b4f9e6dec31e to your computer and use it in GitHub Desktop.
void Bullets::Update(Enemies* enemies) {
for (int i = 0; i < MAX_BULLETS; i++) {
Bullet* b = & bullets[i];
if (b->unused) continue;
// Bullet will be deleted because it exits screen or collides.
// If we're out of bounds, fuck the death animation if there is one because
// nobody's going to see an off screen death animation.
if (IsBulletOutOfBounds(b)) {
b->unused = true;
number_of_bullets--;
continue;
}
if (asset_bullets_is_animated[b->type]) {
bool finished_animation = Animation_Update(& b->animation, (Asset_Ase_Animated*) *(b->asset));
// If we're in the dead animation, we don't want to check any other logic,
// except for whether the animation has ended, in which case we can delete the
// bullet.
if (b->animation.name == "Death") {
if (finished_animation) b->unused = true;
continue;
}
}
Enemy* collided_enemy = HasCollided(b, enemies->enemies);
if (collided_enemy == NULL) {
// No collided enemy, so update bullet position and animation and go to next bullet.
b->x = b->x + cos(b->angle) * b->speed * g_dt;
b->y = b->y + sin(b->angle) * b->speed * g_dt;
continue;
}
// damage enemy
collided_enemy->juice_timer = 0.3;
switch (b->type) {
case TURRET_BASIC:
collided_enemy->health -= TURRET_BASIC_DAMAGE;
break;
case TURRET_RAPID:
collided_enemy->health -= TURRET_RAPID_DAMAGE;
break;
case TURRET_SNIPER:
collided_enemy->health -= TURRET_SNIPER_DAMAGE;
break;
case TURRET_SPLASH:
collided_enemy->health -= TURRET_SPLASH_DAMAGE;
break;
case TURRET_SLOW:
collided_enemy->health -= TURRET_SLOW_DAMAGE; // 0 for now.
collided_enemy->slowdown_timer = TURRET_SLOW_SLOWDOWN_DURATION;
collided_enemy->slowdown_factor = TURRET_SLOW_SLOWDOWN_FACTOR;
break;
default:
print("Bullet type invalid!");
break;
}
// we're here => bullets hit something, so remove it because it's already hit something.
// If we have death animation we do the dramatic bullet death, otherwise we just
// delete the bullet.
Animation_Tag tag = GetTag((Asset_Ase_Animated*) *(b->asset), "Death");
if (tag.from != TAG_ERROR_VAL) {
Animation_Set(& b->animation, (Asset_Ase_Animated*) *(b->asset), "Death");
}
else {
b->unused = true;
number_of_bullets--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment