From 5a6494acf5b3ee85c7eec9a2df293997316ca2d5 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 19 Dec 2024 20:46:47 -0500 Subject: [PATCH] Fixing a stupid bug where it would crash because a fact wasn't in the world. --- dinkyecs.hpp | 6 +++++- gui.cpp | 1 + main.cpp | 1 - status.txt | 1 + tests/lighting.cpp | 4 ++-- tests/matrix.cpp | 3 +-- tests/render.cpp | 1 + 7 files changed, 11 insertions(+), 6 deletions(-) diff --git a/dinkyecs.hpp b/dinkyecs.hpp index 6c31120..f0cf6d1 100644 --- a/dinkyecs.hpp +++ b/dinkyecs.hpp @@ -8,6 +8,7 @@ #include #include #include "tser.hpp" +#include "dbc.hpp" namespace DinkyECS { @@ -56,8 +57,11 @@ namespace DinkyECS { template Comp &get_the() { + auto comp_id = std::type_index(typeid(Comp)); + dbc::check($facts.contains(comp_id), "!!!! ATTEMPT to access world fact that hasn't been set yet."); + // use .at to get std::out_of_range if fact not set - std::any &res = $facts.at(std::type_index(typeid(Comp))); + std::any &res = $facts.at(comp_id); return std::any_cast(res); } diff --git a/gui.cpp b/gui.cpp index 440d896..db0fb7d 100644 --- a/gui.cpp +++ b/gui.cpp @@ -262,6 +262,7 @@ void GUI::render_scene() { } int GUI::main(bool run_once) { + $world.set_the({}); create_renderer(); run_systems(); diff --git a/main.cpp b/main.cpp index ce63be5..6e9af5e 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,6 @@ namespace fs = std::filesystem; */ void configure_world(DinkyECS::World &world, Map &game_map) { const auto &config = world.get_the(); - world.set_the({}); // configure a player as a fact of the world Player player{world.entity()}; world.set_the(player); diff --git a/status.txt b/status.txt index 2bee408..4f0e736 100644 --- a/status.txt +++ b/status.txt @@ -5,6 +5,7 @@ TODAY'S GOAL: * Flame pillars icon \u2e3e * Room should always be found. * matrix::in_box needs a rectangle alternative +* DinkyECS needs to detect when a requested fact is missing and make a default. * Study https://github.com/hirdrac/gx_lib/blob/main/gx/Unicode.hh * Study this https://en.cppreference.com/w/cpp/language/explicit diff --git a/tests/lighting.cpp b/tests/lighting.cpp index ae71cec..f150d21 100644 --- a/tests/lighting.cpp +++ b/tests/lighting.cpp @@ -37,8 +37,8 @@ TEST_CASE("lighting a map works", "[lighting]") { Matrix &lighting = lr.lighting(); - matrix::dump("WALLS=====", map.walls()); - matrix::dump("LIGHT PATHS======", lr.$paths.$paths); + //matrix::dump("WALLS=====", map.walls()); + //matrix::dump("LIGHT PATHS======", lr.$paths.$paths); // confirm light is set at least at and around the two points REQUIRE(lighting[light1.y][light1.x] == lighting::LEVELS[source1.strength]); diff --git a/tests/matrix.cpp b/tests/matrix.cpp index 7d9c94b..0045062 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -237,12 +237,11 @@ TEST_CASE("prototype circle algorithm", "[matrix:circle]") { Matrix result = map.walls(); for(matrix::circle it{start, radius}; it.next();) { - println("y={}, x0={}, x1={}", it.y, it.x0, it.x1); for(int i = it.x0; i < it.x1; i++) { result[it.y][i] += 1; } } - matrix::dump("RESULT AFTER CIRCLE", result, start.x, start.y); + // matrix::dump("RESULT AFTER CIRCLE", result, start.x, start.y); } } diff --git a/tests/render.cpp b/tests/render.cpp index b2488c0..93d1325 100644 --- a/tests/render.cpp +++ b/tests/render.cpp @@ -55,6 +55,7 @@ TEST_CASE("can render a text", "[render]") { builder.generate(); Player player{world.entity()}; + world.set_the({}); world.set_the(player); world.set(player.entity, {config.PLAYER_TILE}); world.set(player.entity, {6,1});