Now have an autowalker class that allows me to drive the game from an external source.

This commit is contained in:
Zed A. Shaw 2025-02-26 13:39:25 -05:00
parent 1aba26831b
commit d4355a608d
6 changed files with 137 additions and 121 deletions

View file

@ -235,11 +235,6 @@ namespace gui {
}
void FSM::keyboard_mouse() {
if($autowalking) {
autowalk();
return;
}
while(const auto ev = $window.pollEvent()) {
if(ev->is<sf::Event::Closed>()) {
event(Event::QUIT);
@ -293,7 +288,8 @@ namespace gui {
$main_ui.debug();
break;
case KEY::O:
start_autowalk(0.06);
autowalking = true;
break;
default:
break; // ignored
}
@ -406,116 +402,4 @@ namespace gui {
run_systems();
}
void FSM::autowalk() {
fmt::println("I'M WALKIN' HEAR!");
$window.handleEvents(
[&](const sf::Event::KeyPressed &) {
$autowalking = false;
fmt::println("ABORT AUTOWALK");
},
[&](const sf::Event::MouseButtonPressed &) {
$autowalking = false;
fmt::println("ABORT AUTOWALK");
}
);
if(!$autowalking) return;
while(in_state(State::IN_COMBAT) || in_state(State::ATTACKING)) {
if(in_state(State::ATTACKING)) {
event(Event::TICK);
} else {
event(Event::ATTACK);
}
handle_world_events();
}
auto& player_position = $level.world->get<Position>($level.player);
auto current = player_position.location;
Point target = current;
bool found = $level.map->neighbors(target, false, -1);
if(!found) {
dbc::log("no neighbor found, aborting autowalk");
$autowalking = false;
return;
}
if(!$level.map->can_move(target)) {
dbc::log("neighbors is telling me to go to a bad spot.");
$autowalking = false;
return;
}
int delta_x = int(target.x) - int(current.x);
int delta_y = int(target.y) - int(current.y);
int facing = $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 ? Event::ROTATE_LEFT : Event::ROTATE_RIGHT;
while(facing != target_facing) {
event(dir);
render();
handle_world_events();
facing = $main_ui.$compass_dir;
}
dbc::check($main_ui.$compass_dir == target_facing,
"player isn't facing the correct direction");
if(!in_state(State::IN_COMBAT)) {
if(in_state(State::ATTACKING)) {
event(Event::TICK);
} else {
event(Event::MOVE_FORWARD);
}
handle_world_events();
do {
fmt::println("IN WHILE MOVING");
event(Event::TICK);
event(Event::ATTACK);
render();
handle_world_events();
} while(in_state(State::MOVING));
}
}
void FSM::start_autowalk(double rot_speed) {
$autowalking = true;
$main_ui.$camera.rot_speed = rot_speed;
}
}