Initial battle engine is now integrated in the systems so now I can finally get the turn based combat to work the way I envision.

This commit is contained in:
Zed A. Shaw 2025-04-06 00:45:51 -04:00
parent e18aeaf05c
commit 1f90367f51
10 changed files with 80 additions and 81 deletions

View file

@ -4,46 +4,41 @@
namespace combat {
void BattleEngine::add_enemy(DinkyECS::Entity enemy_id, ai::EntityAI& enemy) {
combatants.insert_or_assign(enemy_id, enemy);
void BattleEngine::add_enemy(BattleAction enemy) {
combatants.try_emplace(enemy.entity, enemy);
}
bool BattleEngine::plan() {
int active = 0;
for(auto& [entity, enemy_ai] : combatants) {
enemy_ai.set_state("enemy_found", true);
enemy_ai.set_state("in_combat", true);
enemy_ai.update();
for(auto& [entity, enemy] : combatants) {
enemy.ai.set_state("enemy_found", true);
enemy.ai.set_state("in_combat", true);
enemy.ai.update();
active += enemy_ai.active();
active += enemy.ai.active();
// yes, copy it out of the combatants list
pending_actions.push_back(enemy);
}
return active > 0;
}
void BattleEngine::fight(std::function<void(DinkyECS::Entity, ai::EntityAI &)> cb) {
for(auto& [entity, enemy_ai] : combatants) {
if(enemy_ai.wants_to("kill_enemy")) {
cb(entity, enemy_ai);
} else if(!enemy_ai.active()) {
enemy_ai.dump();
dbc::sentinel("enemy AI ended early, fix your ai.json");
} else {
dbc::log("enemy doesn't want to fight");
enemy_ai.dump();
}
}
std::optional<BattleAction> BattleEngine::next() {
if(pending_actions.size() == 0) return std::nullopt;
auto ba = pending_actions.back();
pending_actions.pop_back();
return std::make_optional(ba);
}
void BattleEngine::dump() {
for(auto& [entity, enemy_ai] : combatants) {
for(auto& [entity, enemy] : combatants) {
fmt::println("\n\n###### ENTITY #{}", entity);
enemy_ai.dump();
enemy.ai.dump();
}
}
RitualEngine::RitualEngine(std::string config_path) :
$config(config_path)
{