Basic autowalking feature is working. Not every smart but hit O and it'll walk around and kill everything, hit any other key to stop it.

master
Zed A. Shaw 2 weeks ago
parent a876229e18
commit 14a96d0b63
  1. 112
      gui_fsm.cpp
  2. 2
      gui_fsm.hpp

@ -235,6 +235,11 @@ namespace gui {
} }
void FSM::keyboard_mouse() { void FSM::keyboard_mouse() {
if($autowalking) {
autowalk();
return;
}
while(const auto ev = $window.pollEvent()) { while(const auto ev = $window.pollEvent()) {
if(ev->is<sf::Event::Closed>()) { if(ev->is<sf::Event::Closed>()) {
event(Event::QUIT); event(Event::QUIT);
@ -251,6 +256,7 @@ namespace gui {
if(const auto* key = ev->getIf<sf::Event::KeyPressed>()) { if(const auto* key = ev->getIf<sf::Event::KeyPressed>()) {
using KEY = sf::Keyboard::Scan; using KEY = sf::Keyboard::Scan;
switch(key->scancode) { switch(key->scancode) {
case KEY::W: case KEY::W:
event(Event::MOVE_FORWARD); event(Event::MOVE_FORWARD);
@ -286,6 +292,8 @@ namespace gui {
sound::mute(false); sound::mute(false);
$main_ui.debug(); $main_ui.debug();
break; break;
case KEY::O:
$autowalking = true;
default: default:
break; // ignored break; // ignored
} }
@ -398,4 +406,108 @@ namespace gui {
run_systems(); run_systems();
} }
void FSM::autowalk() {
fmt::println("I'M WALKIN' HEAR!");
$window.handleEvents(
[&](const sf::Event::KeyPressed &) {
$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));
}
}
} }

@ -46,6 +46,7 @@ namespace gui {
public: public:
sf::RenderWindow $window; sf::RenderWindow $window;
bool $draw_stats = false; bool $draw_stats = false;
bool $autowalking = false;
LevelManager $levels; LevelManager $levels;
MainUI $main_ui; MainUI $main_ui;
SFMLRender $renderer; SFMLRender $renderer;
@ -58,6 +59,7 @@ namespace gui {
FSM(); FSM();
void event(Event ev); void event(Event ev);
void autowalk();
void START(Event ); void START(Event );
void MOVING(Event ); void MOVING(Event );

Loading…
Cancel
Save