diff --git a/battle.cpp b/battle.cpp new file mode 100644 index 0000000..4d46d88 --- /dev/null +++ b/battle.cpp @@ -0,0 +1,39 @@ +#include "rituals.hpp" +#include "battle.hpp" + +namespace combat { + void BattleEngine::add_enemy(BattleAction enemy) { + combatants.try_emplace(enemy.entity, enemy); + } + + bool BattleEngine::plan() { + int active = 0; + + 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(); + // yes, copy it out of the combatants list + pending_actions.push_back(enemy); + } + + return active > 0; + } + + std::optional 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] : combatants) { + fmt::println("\n\n###### ENTITY #{}", entity); + enemy.ai.dump(); + } + } +} diff --git a/battle.hpp b/battle.hpp new file mode 100644 index 0000000..d9ebb64 --- /dev/null +++ b/battle.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "rituals.hpp" +#include "config.hpp" +#include "dinkyecs.hpp" +#include +#include "components.hpp" + +namespace combat { + + struct BattleAction { + DinkyECS::Entity entity; + ai::EntityAI &ai; + components::Combat &combat; + }; + + struct BattleEngine { + std::unordered_map combatants; + std::vector pending_actions; + + void add_enemy(BattleAction ba); + bool plan(); + std::optional next(); + void dump(); + }; +} diff --git a/meson.build b/meson.build index 911c118..a72a6bf 100644 --- a/meson.build +++ b/meson.build @@ -84,6 +84,7 @@ sources = [ 'animation.cpp', 'animation.cpp', 'autowalker.cpp', + 'battle.cpp', 'boss_fight_ui.cpp', 'camera.cpp', 'combat.cpp', @@ -126,7 +127,9 @@ sources = [ executable('runtests', sources + [ 'tests/ai.cpp', + 'tests/animation.cpp', 'tests/base.cpp', + 'tests/battle.cpp', 'tests/components.cpp', 'tests/config.cpp', 'tests/dbc.cpp', @@ -142,10 +145,8 @@ executable('runtests', sources + [ 'tests/matrix.cpp', 'tests/pathing.cpp', 'tests/rituals.cpp', - 'tests/combat.cpp', 'tests/sound.cpp', 'tests/spatialmap.cpp', - 'tests/animation.cpp', 'tests/stats.cpp', 'tests/textures.cpp', 'tests/tilemap.cpp', diff --git a/rituals.cpp b/rituals.cpp index bb4cfaf..ab58008 100644 --- a/rituals.cpp +++ b/rituals.cpp @@ -3,42 +3,6 @@ #include "ai.hpp" namespace combat { - - void BattleEngine::add_enemy(BattleAction enemy) { - combatants.try_emplace(enemy.entity, enemy); - } - - bool BattleEngine::plan() { - int active = 0; - - 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(); - // yes, copy it out of the combatants list - pending_actions.push_back(enemy); - } - - return active > 0; - } - - std::optional 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] : combatants) { - fmt::println("\n\n###### ENTITY #{}", entity); - enemy.ai.dump(); - } - } - RitualEngine::RitualEngine(std::string config_path) : $config(config_path) { diff --git a/rituals.hpp b/rituals.hpp index 599f0c7..559e3e7 100644 --- a/rituals.hpp +++ b/rituals.hpp @@ -2,29 +2,10 @@ #include "goap.hpp" #include "ai.hpp" #include "config.hpp" -#include #include "dinkyecs.hpp" -#include #include "components.hpp" namespace combat { - - struct BattleAction { - DinkyECS::Entity entity; - ai::EntityAI &ai; - components::Combat &combat; - }; - - struct BattleEngine { - std::unordered_map combatants; - std::vector pending_actions; - - void add_enemy(BattleAction ba); - bool plan(); - std::optional next(); - void dump(); - }; - struct RitualAI { std::string script; ai::State start; diff --git a/systems.cpp b/systems.cpp index 54e4828..cf92fbf 100644 --- a/systems.cpp +++ b/systems.cpp @@ -13,6 +13,7 @@ #include "ai_debug.hpp" #include "shiterator.hpp" #include "rituals.hpp" +#include "battle.hpp" #include using std::string; diff --git a/tests/combat.cpp b/tests/battle.cpp similarity index 98% rename from tests/combat.cpp rename to tests/battle.cpp index 7c025b7..6eb4f4b 100644 --- a/tests/combat.cpp +++ b/tests/battle.cpp @@ -1,6 +1,7 @@ #include #include #include "rituals.hpp" +#include "battle.hpp" #include "fsm.hpp" #include "dinkyecs.hpp"