Had a mistake where I wasn't tracking the live->dead transition it the new code.
This commit is contained in:
parent
e35e362846
commit
2a92687bc9
3 changed files with 10 additions and 17 deletions
|
|
@ -12,7 +12,6 @@ namespace components {
|
||||||
target.take_damage(my_dmg);
|
target.take_damage(my_dmg);
|
||||||
}
|
}
|
||||||
|
|
||||||
INVARIANT();
|
|
||||||
return my_dmg;
|
return my_dmg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,20 +25,19 @@ namespace components {
|
||||||
// don't go below 0
|
// don't go below 0
|
||||||
hp = std::max(0, hp - my_dmg);
|
hp = std::max(0, hp - my_dmg);
|
||||||
|
|
||||||
has_died = hp <= 0;
|
// don't mark it dead here, the System will do it
|
||||||
INVARIANT();
|
// so that it can notify of live->dead transition
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Combat::less_than(int level) {
|
bool Combat::less_than(int level) {
|
||||||
INVARIANT();
|
|
||||||
// originally this checked main body parts like
|
// originally this checked main body parts like
|
||||||
// head, stomach, and chest
|
// head, stomach, and chest
|
||||||
return hp <= level;
|
return hp <= level;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Combat::is_dead() {
|
bool Combat::is_dead() {
|
||||||
INVARIANT();
|
dbc::check(hp >= 0, "invalid HP, went below zero");
|
||||||
return has_died;
|
return hp == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Combat::almost_dead() {
|
bool Combat::almost_dead() {
|
||||||
|
|
@ -47,17 +45,10 @@ namespace components {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Combat::can_heal() {
|
bool Combat::can_heal() {
|
||||||
INVARIANT();
|
|
||||||
return hp < max_hp;
|
return hp < max_hp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Combat::apply_healing(Curative& cure) {
|
void Combat::apply_healing(Curative& cure) {
|
||||||
INVARIANT();
|
|
||||||
hp += std::min(hp + cure.hp, max_hp);
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,6 @@ namespace components {
|
||||||
bool almost_dead();
|
bool almost_dead();
|
||||||
bool can_heal();
|
bool can_heal();
|
||||||
void apply_healing(Curative& cure);
|
void apply_healing(Curative& cure);
|
||||||
void INVARIANT();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LightSource {
|
struct LightSource {
|
||||||
|
|
|
||||||
|
|
@ -191,15 +191,18 @@ void System::death() {
|
||||||
std::vector<Entity> dead_things;
|
std::vector<Entity> dead_things;
|
||||||
|
|
||||||
world.query<Combat>([&](auto ent, auto &combat) {
|
world.query<Combat>([&](auto ent, auto &combat) {
|
||||||
// bring out yer dead
|
// bring out yer dead, have to do it here to notify on the transition from
|
||||||
if(combat.is_dead()) {
|
// live -> dead
|
||||||
|
if(!combat.has_died && combat.is_dead()) {
|
||||||
|
combat.has_died = true;
|
||||||
|
|
||||||
if(ent != player.entity) {
|
if(ent != player.entity) {
|
||||||
// we won't change out the player's components later
|
// we won't change out the player's components later
|
||||||
dead_things.push_back(ent);
|
dead_things.push_back(ent);
|
||||||
}
|
}
|
||||||
// we need to send this event for everything that dies
|
// we need to send this event for everything that dies
|
||||||
world.send<game::Event>(game::Event::DEATH, ent, {});
|
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 enemies are below 50% health they are marked with bad health
|
||||||
if(world.has<ai::EntityAI>(ent)) {
|
if(world.has<ai::EntityAI>(ent)) {
|
||||||
auto& enemy_ai = world.get<ai::EntityAI>(ent);
|
auto& enemy_ai = world.get<ai::EntityAI>(ent);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue