Added in a new art for a 'gold savior' and refined the battle engine more but it's not quite what I want.

master
Zed A. Shaw 2 weeks ago
parent ca328e10dc
commit 0e2f213871
  1. 4
      assets/ai.json
  2. 5
      assets/config.json
  3. BIN
      assets/gold_savior-256.png
  4. 35
      battle.cpp
  5. 22
      battle.hpp
  6. 16
      systems.cpp
  7. 36
      tests/battle.cpp

@ -79,7 +79,6 @@
], ],
"states": { "states": {
"Host::initial_state": { "Host::initial_state": {
"tough_personality": true,
"enemy_found": false, "enemy_found": false,
"enemy_dead": false, "enemy_dead": false,
"health_good": true, "health_good": true,
@ -88,7 +87,8 @@
"in_combat": false, "in_combat": false,
"have_item": false, "have_item": false,
"have_healing": false, "have_healing": false,
"detect_enemy": true "detect_enemy": true,
"tough_personality": true
}, },
"Host::final_state": { "Host::final_state": {
"enemy_found": true, "enemy_found": true,

@ -20,6 +20,11 @@
"ambient_1": "assets/sounds/ambient_1.ogg" "ambient_1": "assets/sounds/ambient_1.ogg"
}, },
"sprites": { "sprites": {
"gold_savior":
{"path": "assets/gold_savior-256.png",
"frame_width": 256,
"frame_height": 256
},
"armored_knight": "armored_knight":
{"path": "assets/armored_knight_1-256.png", {"path": "assets/armored_knight_1-256.png",
"frame_width": 256, "frame_width": 256,

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

@ -2,7 +2,7 @@
#include "battle.hpp" #include "battle.hpp"
namespace combat { namespace combat {
void BattleEngine::add_enemy(BattleAction enemy) { void BattleEngine::add_enemy(Combatant enemy) {
combatants.try_emplace(enemy.entity, enemy); combatants.try_emplace(enemy.entity, enemy);
} }
@ -10,19 +10,22 @@ namespace combat {
int active = 0; int active = 0;
for(auto& [entity, enemy] : combatants) { for(auto& [entity, enemy] : combatants) {
enemy.ai.set_state("enemy_found", true);
enemy.ai.set_state("in_combat", true);
enemy.ai.update(); enemy.ai.update();
active += enemy.ai.active(); active += enemy.ai.active();
// yes, copy it out of the combatants list
pending_actions.push_back(enemy); if(enemy.ai.active()) {
if(enemy.ai.wants_to("kill_enemy")) {
pending_actions.emplace_back(enemy, BattleAction::ATTACK);
} else if(enemy.ai.wants_to("run_away")) {
pending_actions.emplace_back(enemy, BattleAction::ESCAPE);
}
}
} }
return active > 0; return active > 0;
} }
std::optional<BattleAction> BattleEngine::next() { std::optional<BattleResult> BattleEngine::next() {
if(pending_actions.size() == 0) return std::nullopt; if(pending_actions.size() == 0) return std::nullopt;
auto ba = pending_actions.back(); auto ba = pending_actions.back();
@ -36,4 +39,22 @@ namespace combat {
enemy.ai.dump(); enemy.ai.dump();
} }
} }
void BattleEngine::set(DinkyECS::Entity entity, std::string state, bool setting) {
dbc::check(combatants.contains(entity), "invalid combatant given to BattleEngine");
auto& action = combatants.at(entity);
action.ai.set_state(state, setting);
}
void BattleEngine::set_all(std::string state, bool setting) {
for(auto& [ent, action] : combatants) {
action.ai.set_state(state, setting);
}
}
void BattleEngine::queue(DinkyECS::Entity entity, BattleAction action) {
dbc::check(combatants.contains(entity), "invalid combatant given to BattleEngine");
auto& enemy = combatants.at(entity);
pending_actions.emplace_back(enemy, action);
}
} }

@ -7,19 +7,31 @@
namespace combat { namespace combat {
struct BattleAction { struct Combatant {
DinkyECS::Entity entity; DinkyECS::Entity entity;
ai::EntityAI &ai; ai::EntityAI &ai;
components::Combat &combat; components::Combat &combat;
}; };
enum class BattleAction {
ATTACK, BLOCK, ESCAPE
};
struct BattleResult {
Combatant &state;
BattleAction action;
};
struct BattleEngine { struct BattleEngine {
std::unordered_map<DinkyECS::Entity, BattleAction> combatants; std::unordered_map<DinkyECS::Entity, Combatant> combatants;
std::vector<BattleAction> pending_actions; std::vector<BattleResult> pending_actions;
void add_enemy(BattleAction ba); void add_enemy(Combatant ba);
bool plan(); bool plan();
std::optional<BattleAction> next(); std::optional<BattleResult> next();
void dump(); void dump();
void set(DinkyECS::Entity entity, std::string state, bool setting);
void set_all(std::string state, bool setting);
void queue(DinkyECS::Entity entity, BattleAction action);
}; };
} }

@ -223,20 +223,24 @@ void System::combat(GameLevel &level) {
} }
} }
battle.set_all("enemy_found", true);
battle.set_all("in_combat", true);
battle.plan(); battle.plan();
} }
while(auto enemy = battle.next()) { while(auto act = battle.next()) {
auto [enemy, action] = *act;
Events::Combat result { Events::Combat result {
player_combat.attack(enemy->combat), 0 player_combat.attack(enemy.combat), 0
}; };
if(enemy->ai.wants_to("kill_enemy")) { if(enemy.ai.wants_to("kill_enemy")) {
result.enemy_did = enemy->combat.attack(player_combat); result.enemy_did = enemy.combat.attack(player_combat);
animate_entity(world, enemy->entity); animate_entity(world, enemy.entity);
} }
world.send<Events::GUI>(Events::GUI::COMBAT, enemy->entity, result); world.send<Events::GUI>(Events::GUI::COMBAT, enemy.entity, result);
} }
} }

@ -13,31 +13,45 @@ TEST_CASE("battle operations fantasy", "[combat-battle]") {
auto ai_start = ai::load_state("Enemy::initial_state"); auto ai_start = ai::load_state("Enemy::initial_state");
auto ai_goal = ai::load_state("Enemy::final_state"); auto ai_goal = ai::load_state("Enemy::final_state");
auto host_start = ai::load_state("Host::initial_state");
auto host_goal = ai::load_state("Host::final_state");
BattleEngine battle; BattleEngine battle;
DinkyECS::Entity axe_ranger = 0; DinkyECS::Entity player = 0;
ai::EntityAI player_ai("Host::actions", host_start, host_goal);
components::Combat player_combat{100, 100, 20};
battle.add_enemy({player, player_ai, player_combat});
DinkyECS::Entity axe_ranger = 1;
ai::EntityAI axe_ai("Enemy::actions", ai_start, ai_goal); ai::EntityAI axe_ai("Enemy::actions", ai_start, ai_goal);
axe_ai.set_state("tough_personality", true);
axe_ai.set_state("health_good", true);
components::Combat axe_combat{100, 100, 20}; components::Combat axe_combat{100, 100, 20};
battle.add_enemy({axe_ranger, axe_ai, axe_combat}); battle.add_enemy({axe_ranger, axe_ai, axe_combat});
DinkyECS::Entity rat = 1; DinkyECS::Entity rat = 2;
ai::EntityAI rat_ai("Enemy::actions", ai_start, ai_goal); ai::EntityAI rat_ai("Enemy::actions", ai_start, ai_goal);
rat_ai.set_state("tough_personality", false);
rat_ai.set_state("health_good", true);
components::Combat rat_combat{10, 10, 2}; components::Combat rat_combat{10, 10, 2};
battle.add_enemy({rat, rat_ai, rat_combat}); battle.add_enemy({rat, rat_ai, rat_combat});
battle.set_all("enemy_found", true);
battle.set_all("in_combat", true);
battle.set_all("tough_personality", true);
battle.set_all("health_good", true);
battle.set(rat, "tough_personality", false);
battle.queue(player, BattleAction::ATTACK);
battle.queue(player, BattleAction::BLOCK);
battle.queue(player, BattleAction::ESCAPE);
battle.plan(); battle.plan();
while(auto act = battle.next()) { while(auto act = battle.next()) {
auto& [entity, enemy_ai, combat] = *act; auto& [enemy, action] = *act;
fmt::println("entity: {} wants to {} and has {} HP and {} damage", fmt::println("entity: {} wants to {} action={} and has {} HP and {} damage",
entity, enemy.entity, enemy.ai.wants_to(),
enemy_ai.wants_to(), int(action), enemy.combat.hp,
combat.hp, combat.damage); enemy.combat.damage);
} }
REQUIRE(!battle.next()); REQUIRE(!battle.next());

Loading…
Cancel
Save