AI is now mostly working. Enemies will attack the player, and some of them are marked as not tough so they'll run away when they get low health.

This commit is contained in:
Zed A. Shaw 2025-03-15 11:43:46 -04:00
parent c4e01775bc
commit 75db188dc6
6 changed files with 56 additions and 27 deletions

View file

@ -48,20 +48,24 @@ void System::enemy_ai_initialize(GameLevel &level) {
auto &world = *level.world;
auto &map = *level.map;
world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
world.query<Position, EnemyAI>([&](const auto ent, auto& pos, auto& config) {
if(world.has<ai::EntityAI>(ent)) {
auto&enemy = world.get<ai::EntityAI>(ent);
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
auto&personality = world.get<Personality>(ent);
enemy.set_state("detect_enemy", map.distance(pos.location) < personality.hearing_distance);
enemy.update();
} else {
auto ai_start = ai::load_state(config.ai_start_name);
auto ai_goal = ai::load_state(config.ai_goal_name);
ai::EntityAI enemy(config.ai_script, ai_start, ai_goal);
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
auto&personality = world.get<Personality>(ent);
enemy.set_state("tough_personality", personality.tough);
enemy.set_state("detect_enemy", map.distance(pos.location) < personality.hearing_distance);
enemy.update();
ai::dump_script("\n\n\n-----ENEMY SCRIPT", enemy.start, enemy.plan.script);
world.set<ai::EntityAI>(ent, enemy);
}
});
@ -77,18 +81,18 @@ void System::enemy_pathing(GameLevel &level) {
world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
if(ent != player.entity) {
auto& enemy_ai = world.get<ai::EntityAI>(ent);
Point out = position.location; // copy
if(enemy_ai.wants_to("find_enemy")) {
Point out = position.location; // copy
map.neighbors(out, motion.random);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
map.neighbors(out, motion.random, PATHING_TOWARD);
}
fmt::println("------- ARE THEY SCARED? {}", ent);
enemy_ai.dump();
if(enemy_ai.wants_to("run_away")) {
dbc::log("ENEMY IS SCARED");
fmt::println("ENEMY {} wants to run away", ent);
map.neighbors(out, motion.random, PATHING_AWAY);
}
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
}
});
@ -173,7 +177,8 @@ void System::death(GameLevel &level, components::ComponentMap& components) {
// remove their enemy setting
world.remove<Motion>(ent);
world.remove<Combat>(ent);
world.remove<EnemyConfig>(ent);
world.remove<EnemyAI>(ent);
world.remove<Personality>(ent);
world.remove<ai::EntityAI>(ent);
world.remove<Animation>(ent);
@ -214,12 +219,13 @@ void System::combat(GameLevel &level) {
player_combat.attack(enemy_combat), 0
};
if(!enemy_combat.dead && world.has<ai::EntityAI>(entity)) {
if(world.has<ai::EntityAI>(entity)) {
auto& enemy_ai = world.get<ai::EntityAI>(entity);
enemy_ai.set_state("in_combat", true);
enemy_ai.update();
}
enemy_ai.dump();
if(enemy_ai.wants_to("kill_enemy")) {
result.enemy_did = enemy_combat.attack(player_combat);