Merge in the refactor from earlier.

main
Zed A. Shaw 1 month ago
commit 3dae65fe82
  1. 26
      components.hpp
  2. 27
      gui.cpp
  3. 5
      gui.hpp
  4. 3
      scripts/reset_build.sh
  5. 2
      systems.cpp
  6. 2
      systems.hpp

@ -1,28 +1,30 @@
#pragma once #pragma once
#include "dinkyecs.hpp" #include "dinkyecs.hpp"
struct Player { namespace Components {
struct Player {
DinkyECS::Entity entity; DinkyECS::Entity entity;
}; };
struct Position { struct Position {
Point location; Point location;
}; };
struct Motion { struct Motion {
int dx; int dx;
int dy; int dy;
}; };
struct Combat { struct Combat {
int hp; int hp;
int damage; int damage;
}; };
struct Treasure { struct Treasure {
int amount; int amount;
}; };
struct Tile { struct Tile {
std::string chr = "!"; std::string chr = "!";
}; };
}

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

@ -46,8 +46,6 @@ class GUI {
sf::Font $font; sf::Font $font;
sf::Text $ui_text; sf::Text $ui_text;
sf::Text $map_text; sf::Text $map_text;
bool $shake_it = false;
bool $burn_baby_burn = false;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
sf::RenderWindow $window; sf::RenderWindow $window;
Screen $screen; Screen $screen;
@ -63,11 +61,12 @@ public:
sf::Color color(int val); sf::Color color(int val);
void create_renderer(); void create_renderer();
void render_scene(); void render_scene();
void handle_events(); bool handle_events();
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
void shake(); void shake();
void burn(); void burn();
void configure_world(); void configure_world();
void run_systems();
int main(); int main();
}; };

@ -7,4 +7,5 @@ mkdir subprojects
mv packagecache ./subprojects/ mv packagecache ./subprojects/
mkdir builddir mkdir builddir
cp wraps/*.wrap subprojects/ cp wraps/*.wrap subprojects/
meson setup --default-library=static --prefer-static builddir # on OSX you can't do this with static
meson setup builddir

@ -5,6 +5,8 @@
using std::string; using std::string;
using namespace fmt; using namespace fmt;
using namespace Components;
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) { void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
// move enemies system // move enemies system
world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) { world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {

@ -4,6 +4,8 @@
#include "components.hpp" #include "components.hpp"
#include <ftxui/dom/canvas.hpp> #include <ftxui/dom/canvas.hpp>
using namespace Components;
namespace System { namespace System {
void motion(DinkyECS::World &world, Map &game_map); void motion(DinkyECS::World &world, Map &game_map);
void combat(DinkyECS::World &world, Player &player); void combat(DinkyECS::World &world, Player &player);

Loading…
Cancel
Save