Right before coverage destroys everything.

This commit is contained in:
Zed A. Shaw 2025-03-16 23:17:43 -04:00
parent 1d2968f826
commit c4611c0138
9 changed files with 54 additions and 46 deletions

View file

@ -48,10 +48,10 @@ void System::enemy_ai_initialize(GameLevel &level) {
auto &world = *level.world;
auto &map = *level.map;
world.query<Position, EnemyAI>([&](const auto ent, auto& pos, auto& config) {
world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
if(world.has<ai::EntityAI>(ent)) {
auto&enemy = world.get<ai::EntityAI>(ent);
auto&personality = world.get<Personality>(ent);
auto& enemy = world.get<ai::EntityAI>(ent);
auto& personality = world.get<Personality>(ent);
enemy.set_state("detect_enemy", map.distance(pos.location) < personality.hearing_distance);
enemy.update();
@ -85,10 +85,7 @@ void System::enemy_pathing(GameLevel &level) {
if(enemy_ai.wants_to("find_enemy")) {
map.neighbors(out, motion.random, PATHING_TOWARD);
}
if(enemy_ai.wants_to("run_away")) {
fmt::println("ENEMY {} wants to run away", ent);
} else if(enemy_ai.wants_to("run_away")) {
map.neighbors(out, motion.random, PATHING_AWAY);
}
@ -100,8 +97,6 @@ void System::enemy_pathing(GameLevel &level) {
}
void System::init_positions(DinkyECS::World &world, SpatialMap &collider) {
// BUG: instead of separate things maybe just one
// BUG: Collision component that references what is collide
world.query<Position>([&](auto ent, auto &pos) {
if(world.has<Combat>(ent)) {
const auto& combat = world.get<Combat>(ent);
@ -132,14 +127,11 @@ inline void move_entity(SpatialMap &collider, Map &game_map, Position &position,
}
void System::motion(GameLevel &level) {
auto &map = *level.map;
auto &world = *level.world;
auto &collider = *level.collision;
world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
level.world->query<Position, Motion>(
[&](auto ent, auto &position, auto &motion) {
// don't process entities that don't move
if(motion.dx != 0 || motion.dy != 0) {
move_entity(collider, map, position, motion, ent);
move_entity(*level.collision, *level.map, position, motion, ent);
}
});
}
@ -161,7 +153,7 @@ void System::death(GameLevel &level, components::ComponentMap& components) {
// we need to send this event for everything that dies
world.send<Events::GUI>(Events::GUI::DEATH, ent, {});
} else if(float(combat.hp) / float(combat.max_hp) < 0.5f) {
fmt::println("ENEMY HP low: {}/{}", combat.hp, combat.max_hp);
// if enemies are below 50% health they are marked with bad health
if(world.has<ai::EntityAI>(ent)) {
auto& enemy_ai = world.get<ai::EntityAI>(ent);
enemy_ai.set_state("health_good", false);
@ -177,7 +169,7 @@ void System::death(GameLevel &level, components::ComponentMap& components) {
// remove their enemy setting
world.remove<Motion>(ent);
world.remove<Combat>(ent);
world.remove<EnemyAI>(ent);
world.remove<EnemyConfig>(ent);
world.remove<Personality>(ent);
world.remove<ai::EntityAI>(ent);
world.remove<Animation>(ent);
@ -196,6 +188,17 @@ void System::death(GameLevel &level, components::ComponentMap& components) {
}
}
inline void animate_entity(DinkyECS::World &world, DinkyECS::Entity entity) {
if(world.has<Animation>(entity)) {
auto& animation = world.get<Animation>(entity);
animation.play();
}
if(auto snd = world.get_if<Sound>(entity)) {
sound::play(snd->attack);
}
}
void System::combat(GameLevel &level) {
auto &collider = *level.collision;
auto &world = *level.world;
@ -211,32 +214,19 @@ void System::combat(GameLevel &level) {
for(auto entity : nearby) {
if(world.has<ai::EntityAI>(entity)) {
auto& enemy_ai = world.get<ai::EntityAI>(entity);
enemy_ai.set_state("enemy_found", true);
enemy_ai.update();
auto& enemy_combat = world.get<Combat>(entity);
Events::Combat result {
player_combat.attack(enemy_combat), 0
};
if(world.has<ai::EntityAI>(entity)) {
auto& enemy_ai = world.get<ai::EntityAI>(entity);
enemy_ai.set_state("in_combat", true);
enemy_ai.update();
}
enemy_ai.set_state("enemy_found", true);
enemy_ai.set_state("in_combat", true);
enemy_ai.update();
enemy_ai.dump();
if(enemy_ai.wants_to("kill_enemy")) {
result.enemy_did = enemy_combat.attack(player_combat);
if(world.has<Animation>(entity)) {
auto& animation = world.get<Animation>(entity);
animation.play();
}
if(auto snd = world.get_if<Sound>(entity)) {
sound::play(snd->attack);
}
animate_entity(world, entity);
}
world.send<Events::GUI>(Events::GUI::COMBAT, entity, result);
@ -258,8 +248,6 @@ void System::collision(GameLevel &level) {
int combat_count = 0;
// AI: I think also this would a possible place to run AI decisions
// BUG: this logic is garbage, needs a refactor
for(auto entity : nearby) {
if(world.has<Combat>(entity)) {
auto combat = world.get<Combat>(entity);
@ -268,6 +256,8 @@ void System::collision(GameLevel &level) {
world.send<Events::GUI>(Events::GUI::COMBAT_START, entity, entity);
}
} else if(world.has<InventoryItem>(entity)) {
// BUG: this should really be part of the inventory API and I just
// call into that to work it, rather than this hard coded crap
auto item = world.get<InventoryItem>(entity);
auto& item_pos = world.get<Position>(entity);
auto& inventory = world.get<Inventory>(player.entity);
@ -309,6 +299,7 @@ void System::collision(GameLevel &level) {
}
if(combat_count == 0) {
// BUG: this is probably how we get stuck in combat
world.send<Events::GUI>(Events::GUI::NO_NEIGHBORS, player.entity, player.entity);
}
}
@ -341,6 +332,8 @@ void System::plan_motion(DinkyECS::World& world, Point move_to) {
/*
* This one is called inside the MapViewUI very often so
* just avoide GameMap unlike the others.
*
* BUG: Just get rid of this and use the new UI to do the map
*/
void System::draw_entities(DinkyECS::World &world, Map &map, const Matrix &lights, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) {
auto &tiles = map.tiles();