Now have an autowalker class that allows me to drive the game from an external source.
This commit is contained in:
parent
1aba26831b
commit
d4355a608d
6 changed files with 137 additions and 121 deletions
112
autowalker.cpp
Normal file
112
autowalker.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include "autowalker.hpp"
|
||||
|
||||
|
||||
void Autowalker::autowalk() {
|
||||
fmt::println("I'M WALKIN' HEAR!");
|
||||
|
||||
fsm.$window.handleEvents(
|
||||
[&](const sf::Event::KeyPressed &) {
|
||||
fsm.autowalking = false;
|
||||
fmt::println("ABORT AUTOWALK");
|
||||
},
|
||||
[&](const sf::Event::MouseButtonPressed &) {
|
||||
fsm.autowalking = false;
|
||||
fmt::println("ABORT AUTOWALK");
|
||||
}
|
||||
);
|
||||
|
||||
if(!fsm.autowalking) return;
|
||||
|
||||
while(fsm.in_state(gui::State::IN_COMBAT) || fsm.in_state(gui::State::ATTACKING)) {
|
||||
if(fsm.in_state(gui::State::ATTACKING)) {
|
||||
fsm.event(gui::Event::TICK);
|
||||
} else {
|
||||
fsm.event(gui::Event::ATTACK);
|
||||
}
|
||||
|
||||
fsm.handle_world_events();
|
||||
}
|
||||
|
||||
auto& player_position = fsm.$level.world->get<components::Position>(fsm.$level.player);
|
||||
auto current = player_position.location;
|
||||
Point target = current;
|
||||
bool found = fsm.$level.map->neighbors(target, false, -1);
|
||||
if(!found) {
|
||||
dbc::log("no neighbor found, aborting autowalk");
|
||||
fsm.autowalking = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!fsm.$level.map->can_move(target)) {
|
||||
dbc::log("neighbors is telling me to go to a bad spot.");
|
||||
fsm.autowalking = false;
|
||||
return;
|
||||
}
|
||||
|
||||
int delta_x = int(target.x) - int(current.x);
|
||||
int delta_y = int(target.y) - int(current.y);
|
||||
|
||||
int facing = fsm.$main_ui.$compass_dir;
|
||||
int target_facing = 0;
|
||||
|
||||
if(delta_x == -1 && delta_y == 0) {
|
||||
// west
|
||||
fmt::println("WEST: {}, {}", target.x, target.y);
|
||||
target_facing = 4;
|
||||
} else if(delta_x == 1 && delta_y == 0) {
|
||||
// east
|
||||
fmt::println("EAST: {}, {}", target.x, target.y);
|
||||
target_facing = 0;
|
||||
} else if(delta_x == 0 && delta_y == 1) {
|
||||
fmt::println("SOUTH: {}, {}", target.x, target.y);
|
||||
// south
|
||||
target_facing = 2;
|
||||
} else if(delta_x == 0 && delta_y == -1) {
|
||||
fmt::println("NORTH: {}, {}", target.x, target.y);
|
||||
// north
|
||||
target_facing = 6;
|
||||
} else {
|
||||
dbc::sentinel(
|
||||
fmt::format("got more than 4 direction result: "
|
||||
"current={},{} "
|
||||
"target={},{} "
|
||||
"delta={},{} ",
|
||||
current.x, current.y,
|
||||
target.x, target.y,
|
||||
delta_x, delta_y));
|
||||
}
|
||||
|
||||
auto dir = facing < target_facing ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
|
||||
|
||||
while(facing != target_facing) {
|
||||
fsm.event(dir);
|
||||
fsm.render();
|
||||
fsm.handle_world_events();
|
||||
facing = fsm.$main_ui.$compass_dir;
|
||||
}
|
||||
|
||||
dbc::check(fsm.$main_ui.$compass_dir == target_facing,
|
||||
"player isn't facing the correct direction");
|
||||
|
||||
if(!fsm.in_state(gui::State::IN_COMBAT)) {
|
||||
if(fsm.in_state(gui::State::ATTACKING)) {
|
||||
fsm.event(gui::Event::TICK);
|
||||
} else {
|
||||
fsm.event(gui::Event::MOVE_FORWARD);
|
||||
}
|
||||
|
||||
fsm.handle_world_events();
|
||||
|
||||
do {
|
||||
fmt::println("IN WHILE MOVING");
|
||||
fsm.event(gui::Event::TICK);
|
||||
fsm.event(gui::Event::ATTACK);
|
||||
fsm.render();
|
||||
fsm.handle_world_events();
|
||||
} while(fsm.in_state(gui::State::MOVING));
|
||||
}
|
||||
}
|
||||
|
||||
void Autowalker::start_autowalk() {
|
||||
fsm.autowalking = true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue