From 280552fa4750b5dac9243782f9c0a7e0b7eea6f8 Mon Sep 17 00:00:00 2001 From: quou Date: Fri, 5 May 2023 09:25:39 +1000 Subject: Add a basic enemy. --- enemy.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 enemy.c (limited to 'enemy.c') diff --git a/enemy.c b/enemy.c new file mode 100644 index 0000000..9a67cba --- /dev/null +++ b/enemy.c @@ -0,0 +1,103 @@ +#include "bullet.h" +#include "components.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; + Entity e; + + e = new_entity(world); + add_components( + world, + e, + ctype_position | + ctype_sprite | + ctype_enemy | + ctype_skull + ); + pos = &world->positions[e]; + sprite = &world->sprites[e]; + enemy = &world->enemies[e]; + + pos->x = x; + pos->y = y; + + init_csprite(sprite, sprite_skull_right); + + enemy->hp = skull_hp; + enemy->shoot_timer = 0; + + return e; +} + +void enemy_system(World* world) { + /* Skulls. */ + int i; + unsigned bits; + CPosition* pos; + CEnemy* enemy; + Player* player; + CPosition* ppos; + int dpx, dpy, tpx, tpy, d; + char buf[32]; + + player = &world->player; + ppos = &world->positions[player->entity]; + + for (i = 0; i < world->entity_count; i++) { + bits = world->bitmask[i]; + if ( + (bits & ctype_position) && + (bits & ctype_enemy) && + (bits & ctype_skull) + ) { + pos = &world->positions[i]; + enemy = &world->enemies[i]; + + dpx = (ppos->x - pos->x) >> 4; + dpy = (ppos->y - pos->y) >> 4; + tpx = dpx; + tpy = dpy; + d = ((dpx * dpx) >> fbits) + ((dpy * dpy) >> fbits); + + 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, + pos->y, + tpx * enemy_bullet_speed, + tpy * enemy_bullet_speed + ); + enemy->shoot_timer = 0; + } + + enemy->shoot_timer++; + } + } +} -- cgit v1.2.3-54-g00ecf