Clean up the DinkyECSso that it's easier to understand and more obvious what a fact vs. component is.

This commit is contained in:
Zed A. Shaw 2024-10-29 07:33:00 -04:00
parent 4ed06b10b1
commit 143fe7784c
7 changed files with 82 additions and 105 deletions

56
gui.cpp
View file

@ -72,7 +72,7 @@ GUI::GUI() :
}
void GUI::create_renderer() {
auto player = $world.get<Player>();
auto player = $world.get_the<Player>();
$map_view = Renderer([&] {
System::draw_map($world, $game_map, $canvas, $view_port.x, $view_port.y);
@ -80,8 +80,8 @@ void GUI::create_renderer() {
});
$document = Renderer([&, player]{
const auto& player_combat = $world.component<Combat>(player.entity);
const auto& log = $world.get<ActionLog>();
const auto& player_combat = $world.get<Combat>(player.entity);
const auto& log = $world.get_the<ActionLog>();
$status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!";
@ -114,8 +114,8 @@ bool GUI::handle_events() {
if(event.type == sf::Event::Closed) {
$window.close();
} else if(event.type == sf::Event::KeyPressed) {
auto player = $world.get<Player>();
auto& player_motion = $world.component<Motion>(player.entity);
auto player = $world.get_the<Player>();
auto& player_motion = $world.get<Motion>(player.entity);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player_motion.dx = -1;
@ -157,7 +157,7 @@ sf::Sprite &GUI::get_text_sprite(wchar_t tile) {
}
void GUI::run_systems() {
auto player = $world.get<Player>();
auto player = $world.get_the<Player>();
System::enemy_pathing($world, $game_map, player);
System::motion($world, $game_map);
System::combat($world, player);
@ -268,38 +268,42 @@ void GUI::shake() {
}
void GUI::configure_world() {
SoundManager sounds("./assets");
sounds.load("hit", "hit.wav");
$world.set_the<SoundManager>(sounds);
dbc::check($game_map.room_count() > 1, "not enough rooms in map.");
// configure a player as a fact of the world
Player player{$world.entity()};
$world.set<Player>(player);
$world.set_the<Player>(player);
ActionLog log{{"Welcome to the game!"}};
$world.set<ActionLog>(log);
$world.set_the<ActionLog>(log);
spatial_map collider;
$world.set<spatial_map>(collider);
$world.set_the<spatial_map>(collider);
$world.assign<Position>(player.entity, {$game_map.place_entity(0)});
$world.assign<Motion>(player.entity, {0, 0});
$world.assign<Combat>(player.entity, {100, 10});
$world.assign<Tile>(player.entity, {PLAYER_TILE});
$world.set<Position>(player.entity, {$game_map.place_entity(0)});
$world.set<Motion>(player.entity, {0, 0});
$world.set<Combat>(player.entity, {100, 10});
$world.set<Tile>(player.entity, {PLAYER_TILE});
auto enemy = $world.entity();
$world.assign<Position>(enemy, {$game_map.place_entity(1)});
$world.assign<Motion>(enemy, {0,0});
$world.assign<Combat>(enemy, {20, 10});
$world.assign<Tile>(enemy, {ENEMY_TILE});
$world.set<Position>(enemy, {$game_map.place_entity(1)});
$world.set<Motion>(enemy, {0,0});
$world.set<Combat>(enemy, {20, 10});
$world.set<Tile>(enemy, {ENEMY_TILE});
auto enemy2 = $world.entity();
$world.assign<Position>(enemy2, {$game_map.place_entity(2)});
$world.assign<Motion>(enemy2, {0,0});
$world.assign<Combat>(enemy2, {20, 10});
$world.assign<Tile>(enemy2, {"*"});
$world.set<Position>(enemy2, {$game_map.place_entity(2)});
$world.set<Motion>(enemy2, {0,0});
$world.set<Combat>(enemy2, {20, 10});
$world.set<Tile>(enemy2, {"*"});
auto gold = $world.entity();
$world.assign<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
$world.assign<Treasure>(gold, {100});
$world.assign<Tile>(gold, {"$"});
$world.set<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
$world.set<Treasure>(gold, {100});
$world.set<Tile>(gold, {"$"});
System::init_positions($world);
}
@ -314,10 +318,6 @@ void GUI::render_scene() {
}
int GUI::main() {
SoundManager sounds("./assets");
sounds.load("hit", "hit.wav");
$world.set<SoundManager>(sounds);
configure_world();
create_renderer();
run_systems();