Merge in the refactor from earlier.

This commit is contained in:
Zed A. Shaw 2024-10-17 21:49:57 -04:00
commit 3dae65fe82
6 changed files with 50 additions and 35 deletions

31
gui.cpp
View file

@ -26,6 +26,7 @@ using std::string;
using namespace fmt;
using namespace std::chrono_literals;
using namespace ftxui;
using namespace Components;
std::array<sf::Color, 10> VALUES{
sf::Color{1, 4, 2}, // black
@ -96,8 +97,9 @@ void GUI::create_renderer() {
});
}
void GUI::handle_events() {
bool GUI::handle_events() {
sf::Event event;
bool event_happened = false;
while($window.pollEvent(event)) {
if(event.type == sf::Event::Closed) {
@ -105,27 +107,31 @@ void GUI::handle_events() {
} else if(event.type == sf::Event::KeyPressed) {
auto player = $world.get<Player>();
auto& player_motion = $world.component<Motion>(player.entity);
auto& player_position = $world.component<Position>(player.entity);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player_motion.dx = -1;
event_happened = true;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
player_motion.dx = 1;
event_happened = true;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
player_motion.dy = -1;
event_happened = true;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
player_motion.dy = 1;
event_happened = true;
}
$game_map.set_target(player_position.location);
$game_map.make_paths();
// COMPOSE system? You create a bunch of callbacks and then combine them into
// a single run over the data?
System::enemy_pathing($world, $game_map, player);
System::motion($world, $game_map);
System::combat($world, player);
}
}
return event_happened;
}
void GUI::run_systems() {
auto player = $world.get<Player>();
System::enemy_pathing($world, $game_map, player);
System::motion($world, $game_map);
System::combat($world, player);
}
void GUI::burn() {
@ -209,7 +215,10 @@ int GUI::main() {
while($window.isOpen()) {
render_scene();
handle_events();
if(handle_events()) {
run_systems();
}
std::this_thread::sleep_for(10ms);
}