diff options
author | quou <quou@disroot.org> | 2023-05-05 09:25:39 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2023-05-05 09:25:39 +1000 |
commit | 280552fa4750b5dac9243782f9c0a7e0b7eea6f8 (patch) | |
tree | 6385dc740cc965dad7dc628e8b10738724ec0a42 /enemy.c | |
parent | 0a083f5a9a747083bbc3a1f0689e76ac5fc3a3b9 (diff) |
Add a basic enemy.
Diffstat (limited to 'enemy.c')
-rw-r--r-- | enemy.c | 103 |
1 files changed, 103 insertions, 0 deletions
@@ -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++; + } + } +} |