Had a mistake where I wasn't tracking the live->dead transition it the new code.

This commit is contained in:
Zed A. Shaw 2026-05-25 11:02:22 -04:00
parent e35e362846
commit 2a92687bc9
3 changed files with 10 additions and 17 deletions

View file

@ -12,7 +12,6 @@ namespace components {
target.take_damage(my_dmg);
}
INVARIANT();
return my_dmg;
}
@ -26,20 +25,19 @@ namespace components {
// don't go below 0
hp = std::max(0, hp - my_dmg);
has_died = hp <= 0;
INVARIANT();
// don't mark it dead here, the System will do it
// so that it can notify of live->dead transition
}
bool Combat::less_than(int level) {
INVARIANT();
// originally this checked main body parts like
// head, stomach, and chest
return hp <= level;
}
bool Combat::is_dead() {
INVARIANT();
return has_died;
dbc::check(hp >= 0, "invalid HP, went below zero");
return hp == 0;
}
bool Combat::almost_dead() {
@ -47,17 +45,10 @@ namespace components {
}
bool Combat::can_heal() {
INVARIANT();
return hp < max_hp;
}
void Combat::apply_healing(Curative& cure) {
INVARIANT();
hp += std::min(hp + cure.hp, max_hp);
}
void Combat::INVARIANT() {
dbc::check(!(hp <= 0 && has_died == false), "entity hp <= 0 && has_died==false, they should be dead");
dbc::check(!(hp > 0 && has_died == true), "entity has hp > 0 but is marked dead, should be alive");
}
}

View file

@ -133,7 +133,6 @@ namespace components {
bool almost_dead();
bool can_heal();
void apply_healing(Curative& cure);
void INVARIANT();
};
struct LightSource {

View file

@ -191,15 +191,18 @@ void System::death() {
std::vector<Entity> dead_things;
world.query<Combat>([&](auto ent, auto &combat) {
// bring out yer dead
if(combat.is_dead()) {
// bring out yer dead, have to do it here to notify on the transition from
// live -> dead
if(!combat.has_died && combat.is_dead()) {
combat.has_died = true;
if(ent != player.entity) {
// we won't change out the player's components later
dead_things.push_back(ent);
}
// we need to send this event for everything that dies
world.send<game::Event>(game::Event::DEATH, ent, {});
} else if(float(combat.hp) / float(combat.max_hp) < 0.5f) {
} else if(combat.almost_dead()) {
// 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);