Created a combat system to start with and also added a 'HEARING' mechanic where enemies can hear you from a certain distance before moving to you.

This commit is contained in:
Zed A. Shaw 2024-10-27 18:44:54 -04:00
parent 753bc70b77
commit 9102bdc8ad
9 changed files with 70 additions and 24 deletions

View file

@ -10,6 +10,8 @@ using namespace fmt;
using namespace Components;
#define HEARING_DISTANCE 8
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
const auto &player_position = world.component<Position>(player.entity);
game_map.set_target(player_position.location);
@ -17,9 +19,11 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player
world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
if(ent != player.entity) {
Point out = position.location;
game_map.neighbors(out, false);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
Point out = position.location; // copy
if(game_map.distance(out) < HEARING_DISTANCE) {
game_map.neighbors(out, false);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
}
}
});
game_map.clear_target(player_position.location);
@ -64,6 +68,22 @@ void System::motion(DinkyECS::World &world, Map &game_map) {
});
}
void System::death(DinkyECS::World &world) {
auto &collider = world.get<spatial_map>();
world.system<Position, Combat>([&](const auto &ent, auto &position, auto &combat) {
// bring out yer dead
if(combat.hp <= 0 && !combat.dead) {
combat.dead = true;
// take them out of collision map
collider.remove(position.location);
// remove their motion so they're dead
world.remove<Motion>(ent);
}
});
}
void System::combat(DinkyECS::World &world, Player &player) {
auto& collider = world.get<spatial_map>();
@ -77,25 +97,24 @@ void System::combat(DinkyECS::World &world, Player &player) {
if(found) {
for(auto entity : nearby) {
auto& enemy_combat = world.component<Combat>(entity);
int player_dmg = Random::uniform<int>(1, enemy_combat.damage);
enemy_combat.hp -= player_dmg;
log.log(format("YOU HIT {} damage! Enemy has {} HP left.",
player_dmg, enemy_combat.hp));
int player_dmg = player_combat.fight(enemy_combat);
if(enemy_combat.hp <= 0) {
log.log("--- ENEMY DEAD!---");
auto enemy_position = world.component<Position>(entity);
collider.remove(enemy_position.location);
world.remove<Motion>(entity);
if(player_dmg > 0) {
log.log(format("You HIT for {} damage, HP left {}.", player_dmg, enemy_combat.hp));
} else {
int attack = Random::uniform<int>(0,1);
if(attack) {
int dmg = Random::uniform<int>(1, enemy_combat.damage);
player_combat.hp -= dmg;
log.log(format("HIT! You took {} damage.", dmg));
log.log("You missed! They're quick!");
}
if(enemy_combat.hp > 0) {
int enemy_dmg = enemy_combat.fight(player_combat);
if(enemy_dmg > 0) {
log.log(format("Enemy HIT YOU for {} damage.", enemy_dmg));
} else {
log.log("You dodged! Run!");
log.log("Enemy MISSED, you dodged it.");
}
} else {
log.log("ENEMY DEAD! YOU WIN!");
}
}
}