#include "bullet.h" #include "components.h" #include "debris.h" #include "game_config.h" #include "sprite.h" #include "standard.h" #include "world.h" Entity new_skull(World* world, int x, int y) { CSprite* sprite; CPosition* pos; CEnemy* enemy; CCollider* col; Entity e; e = new_entity(world); add_components( world, e, ctype_position | ctype_sprite | ctype_enemy | ctype_skull | ctype_collider | ctype_moveable ); pos = &world->positions[e]; sprite = &world->sprites[e]; enemy = &world->enemies[e]; col = &world->colliders[e]; pos->x = x; pos->y = y; col->x = 2 << fbits; col->y = 2 << fbits; col->w = 14 << fbits; col->h = 14 << fbits; init_csprite(sprite, sprite_skull); enemy->hp = skull_hp; enemy->shoot_timer = 0; world->enemy_count++; return e; } static void new_skull_debris(World* world, int x, int y) { new_debris( world, x, y, -2 << fbits, -2 << fbits, sprite_skull_debris_1 ); new_debris( world, x, y, 0, -2 << fbits, sprite_skull_debris_2 ); new_debris( world, x, y, -2 << fbits, 0, sprite_skull_debris_3 ); new_debris( world, x, y, 0, 2 << fbits, sprite_skull_debris_4 ); new_debris( world, x, y, 2 << fbits, 2 << fbits, sprite_skull_debris_5 ); } void enemy_system(World* world) { /* Skulls. */ int i; unsigned bits; CPosition* pos; CEnemy* enemy; CSprite* sprite; Player* player; CPosition* ppos; int dpx, dpy, tpx, tpy, d; player = &world->player; ppos = &world->positions[player->entity]; for (i = 0; i < world->entity_count; i++) { bits = world->bitmask[i]; if (bits & ctype_enemy) { enemy = &world->enemies[i]; if (enemy->hp <= 0) { destroy_entity(world, i); world->enemies_killed++; if ((bits & ctype_position) && (bits & ctype_skull)) { pos = &world->positions[i]; new_skull_debris(world, pos->x, pos->y); } } } } for (i = 0; i < world->entity_count; i++) { bits = world->bitmask[i]; if ( (bits & ctype_position) && (bits & ctype_enemy) && (bits & ctype_skull) && (bits & ctype_sprite) ) { pos = &world->positions[i]; enemy = &world->enemies[i]; sprite = &world->sprites[i]; dpx = ((ppos->x - pos->x) >> 4) + 256; dpy = ((ppos->y - pos->y) >> 4) + 256; tpx = dpx; tpy = dpy; d = ((dpx * dpx) + (dpy * dpy)) >> fbits; if (enemy->being_damaged) { enemy->being_damaged--; init_csprite(sprite, sprite_skull_damaged); } else { init_csprite(sprite, sprite_skull); } if (d < 10 << fbits) { dpx = -dpx; dpy = -dpy; enemy->backpedal = 1; } else if (enemy->backpedal) { if (d > 30 << fbits) { enemy->backpedal = 0; } else { dpx = -dpx; dpy = -dpy; } } dpx = (dpx < 0 ? -1 : 1); dpy = (dpy < 0 ? -1 : 1); pos->x += dpx * skull_speed; pos->y += dpy * skull_speed; if (enemy->shoot_timer > skull_shoot_cooldown) { vec_nrmise(&tpx, &tpy); new_enemy_bullet( world, pos->x + 2560, pos->y + 2560, tpx * enemy_bullet_speed, tpy * enemy_bullet_speed ); enemy->shoot_timer = 0; } enemy->shoot_timer++; } } }