I think I've got my head around what ECS does and am slowly reshaping the engine to use it better.

This commit is contained in:
Zed A. Shaw 2024-10-16 20:31:00 -04:00
parent da04c5ec54
commit e42647d727
5 changed files with 93 additions and 76 deletions

82
gui.cpp
View file

@ -19,38 +19,14 @@
#include "dbc.hpp"
#include "gui.hpp"
#include "rand.hpp"
#include "components.hpp"
#include "systems.hpp"
using std::string;
using namespace fmt;
using namespace std::chrono_literals;
using namespace ftxui;
struct Player {
DinkyECS::Entity entity;
};
struct Position {
Point location;
};
struct Motion {
int dx;
int dy;
};
struct Combat {
int hp;
int damage;
};
struct Treasure {
int amount;
};
struct Tile {
std::string chr = "!";
};
std::array<sf::Color, 10> VALUES{
sf::Color{1, 4, 2}, // black
sf::Color{9, 29, 16}, // dark dark
@ -121,10 +97,7 @@ void GUI::create_renderer() {
}
}
$world.system<Position, Tile>([&](const auto &ent, auto &pos, auto &tile) {
$canvas.DrawText(pos.location.x*2, pos.location.y*4, tile.chr);
});
System::draw_entities($world, $canvas);
return canvas($canvas);
});
@ -164,42 +137,9 @@ void GUI::handle_events() {
// COMPOSE system? You create a bunch of callbacks and then combine them into
// a single run over the data?
// move enemies system
$world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
if(ent != player.entity) {
Point out = position.location;
$game_map.neighbors(out, false);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
}
});
// motion system
$world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
Point move_to = {
position.location.x + motion.dx,
position.location.y + motion.dy
};
motion = {0,0}; // clear it after getting it
if($game_map.inmap(move_to.x, move_to.y) && !$game_map.iswall(move_to.x,move_to.y)) {
$game_map.clear_target(position.location);
position.location = move_to;
}
});
// combat system
auto combatSystem = [&]() {
const auto& player_position = $world.component<Position>(player.entity);
$world.system<Position, Combat>([&](const auto &ent, auto &pos, auto &combat) {
if(ent != player.entity && pos.location.x == player_position.location.x &&
pos.location.y == player_position.location.y) {
$burn_baby_burn = true;
}
});
};
combatSystem();
System::enemy_pathing($world, $game_map, player);
System::motion($world, $game_map);
System::combat($world, player);
}
}
}
@ -276,16 +216,6 @@ void GUI::render_scene() {
std::wstring map_screen_utf8 = $converter.from_bytes($map_screenout);
$map_text.setString(map_screen_utf8);
if($shake_it) {
shake();
$shake_it = false;
}
if($burn_baby_burn) {
burn();
$burn_baby_burn = false;
}
draw_screen();
}