Enemies will now fight back if they're cornered. Was actually way easier than I thought.
This commit is contained in:
parent
7ffa6025ce
commit
586343a614
3 changed files with 37 additions and 25 deletions
|
@ -9,7 +9,8 @@
|
||||||
"have_item": 6,
|
"have_item": 6,
|
||||||
"have_healing": 7,
|
"have_healing": 7,
|
||||||
"detect_enemy": 8,
|
"detect_enemy": 8,
|
||||||
"tough_personality": 9
|
"tough_personality": 9,
|
||||||
|
"cant_move": 10
|
||||||
},
|
},
|
||||||
"actions": [
|
"actions": [
|
||||||
{
|
{
|
||||||
|
@ -32,7 +33,8 @@
|
||||||
"tough_personality": false,
|
"tough_personality": false,
|
||||||
"in_combat": true,
|
"in_combat": true,
|
||||||
"have_healing": false,
|
"have_healing": false,
|
||||||
"health_good": false
|
"health_good": false,
|
||||||
|
"cant_move": false
|
||||||
},
|
},
|
||||||
"effects": {
|
"effects": {
|
||||||
"in_combat": false
|
"in_combat": false
|
||||||
|
|
|
@ -418,8 +418,8 @@ namespace gui {
|
||||||
System::generate_paths();
|
System::generate_paths();
|
||||||
System::enemy_ai_initialize();
|
System::enemy_ai_initialize();
|
||||||
System::enemy_pathing();
|
System::enemy_pathing();
|
||||||
System::collision();
|
|
||||||
System::motion();
|
System::motion();
|
||||||
|
System::collision();
|
||||||
System::lighting();
|
System::lighting();
|
||||||
System::death();
|
System::death();
|
||||||
}
|
}
|
||||||
|
|
40
systems.cpp
40
systems.cpp
|
@ -107,6 +107,9 @@ void System::enemy_pathing() {
|
||||||
map.random_walk(out, motion.random, PATHING_TOWARD);
|
map.random_walk(out, motion.random, PATHING_TOWARD);
|
||||||
} else if(enemy_ai.wants_to("run_away")) {
|
} else if(enemy_ai.wants_to("run_away")) {
|
||||||
map.random_walk(out, motion.random, PATHING_AWAY);
|
map.random_walk(out, motion.random, PATHING_AWAY);
|
||||||
|
} else {
|
||||||
|
motion = {0,0};
|
||||||
|
return; // enemy doesn't want to move
|
||||||
}
|
}
|
||||||
|
|
||||||
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
|
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
|
||||||
|
@ -117,31 +120,38 @@ void System::enemy_pathing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void move_entity(SpatialMap &collider, Map &game_map, Position &position, Motion &motion, Entity ent) {
|
void System::motion() {
|
||||||
|
auto& level = GameDB::current_level();
|
||||||
|
auto world = level.world;
|
||||||
|
auto map = level.map;
|
||||||
|
auto collider = level.collision;
|
||||||
|
|
||||||
|
world->query<Position, Motion>(
|
||||||
|
[&](auto ent, auto &position, auto &motion) {
|
||||||
|
// skip enemies that aren't moving
|
||||||
|
if(motion.dx == 0 && motion.dy == 0) return;
|
||||||
|
|
||||||
Point move_to = {
|
Point move_to = {
|
||||||
position.location.x + motion.dx,
|
position.location.x + motion.dx,
|
||||||
position.location.y + motion.dy
|
position.location.y + motion.dy
|
||||||
};
|
};
|
||||||
|
|
||||||
motion = {0,0}; // clear it after getting it
|
motion = {0,0}; // clear it after getting it
|
||||||
|
|
||||||
|
dbc::check(map->can_move(move_to), "Enemy pathing failed, move_to is wall.");
|
||||||
|
|
||||||
|
bool cant_move = collider->occupied(move_to);
|
||||||
|
|
||||||
|
if(auto enemy_ai = world->get_if<ai::EntityAI>(ent)) {
|
||||||
|
enemy_ai->set_state("cant_move", cant_move);
|
||||||
|
}
|
||||||
|
|
||||||
// it's a wall, skip
|
// it's a wall, skip
|
||||||
if(!game_map.can_move(move_to)) return;
|
if(cant_move) return;
|
||||||
// there's collision skip
|
|
||||||
if(collider.occupied(move_to)) return;
|
|
||||||
|
|
||||||
// all good, do the move
|
// all good, do the move
|
||||||
collider.move(position.location, move_to, ent);
|
collider->move(position.location, move_to, ent);
|
||||||
position.location = move_to;
|
position.location = move_to;
|
||||||
}
|
|
||||||
|
|
||||||
void System::motion() {
|
|
||||||
auto& level = GameDB::current_level();
|
|
||||||
level.world->query<Position, Motion>(
|
|
||||||
[&](auto ent, auto &position, auto &motion) {
|
|
||||||
// don't process entities that don't move
|
|
||||||
if(motion.dx != 0 || motion.dy != 0) {
|
|
||||||
move_entity(*level.collision, *level.map, position, motion, ent);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue