diff --git a/Makefile b/Makefile index ca4ba17..9319a13 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ tracy_build: meson compile -j 10 -C builddir test: asset_build build - ./builddir/runtests + ./builddir/runtests "[spatialmap-neighbors]" run: build test ifeq '$(OS)' 'Windows_NT' diff --git a/gui/combat_ui.cpp b/gui/combat_ui.cpp index 305715a..9675aa5 100644 --- a/gui/combat_ui.cpp +++ b/gui/combat_ui.cpp @@ -14,7 +14,7 @@ namespace gui { $gui.position(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT); $gui.layout( "[button_0 | button_1 | button_2 | button_3" - "|button_4 | button_5 | button_6 | healing_button | hp_gauge ]" + "|button_4 | button_5 | button_6 | =healing_button | =hp_gauge ]" ); } diff --git a/spatialmap.cpp b/spatialmap.cpp index dcdbf1d..5a26306 100644 --- a/spatialmap.cpp +++ b/spatialmap.cpp @@ -53,12 +53,7 @@ Entity SpatialMap::get(Point at) const { return begin->second.entity; } -/* - * Avoid doing work by using the dy,dx and confirming that - * at.x or at.y is > 0. If either is 0 then there can't be - * a neighbor since that's out of bounds. - */ -inline void find_neighbor(const PointEntityMap &table, EntityList &result, Point at, int dy, int dx) { +void SpatialMap::find_neighbor(EntityList &result, Point at, int dy, int dx) const { // don't bother checking for cells out of bounds if((dx < 0 && at.x <= 0) || (dy < 0 && at.y <= 0)) { return; @@ -66,11 +61,11 @@ inline void find_neighbor(const PointEntityMap &table, EntityList &result, Point Point cell = {at.x + dx, at.y + dy}; - // Bug #81, should actually for-loop through these and only add ones with collision - auto it = table.find(cell); - if (it != table.end()) { - result.insert(result.end(), it->second.entity); - } + auto entity = find(cell, [&](auto data) { + return data.collision; + }); + + if(entity != DinkyECS::NONE) result.push_back(entity); } FoundEntities SpatialMap::neighbors(Point cell, bool diag) const { @@ -78,16 +73,16 @@ FoundEntities SpatialMap::neighbors(Point cell, bool diag) const { // just unroll the loop since we only check four directions // this also solves the problem that it was detecting that the cell was automatically included as a "neighbor" but it's not - find_neighbor($collision, result, cell, 0, 1); // north - find_neighbor($collision, result, cell, 0, -1); // south - find_neighbor($collision, result, cell, 1, 0); // east - find_neighbor($collision, result, cell, -1, 0); // west + find_neighbor(result, cell, 0, 1); // north + find_neighbor(result, cell, 0, -1); // south + find_neighbor(result, cell, 1, 0); // east + find_neighbor(result, cell, -1, 0); // west if(diag) { - find_neighbor($collision, result, cell, 1, -1); // south east - find_neighbor($collision, result, cell, -1, -1); // south west - find_neighbor($collision, result, cell, 1, 1); // north east - find_neighbor($collision, result, cell, -1, 1); // north west + find_neighbor(result, cell, 1, -1); // south east + find_neighbor(result, cell, -1, -1); // south west + find_neighbor(result, cell, 1, 1); // north east + find_neighbor(result, cell, -1, 1); // north west } return {!result.empty(), result}; diff --git a/spatialmap.hpp b/spatialmap.hpp index 2740552..363c0a1 100644 --- a/spatialmap.hpp +++ b/spatialmap.hpp @@ -39,6 +39,7 @@ class SpatialMap { bool something_there(Point at) const; DinkyECS::Entity get(Point at) const; DinkyECS::Entity find(Point at, std::function cb) const; + void find_neighbor(EntityList &result, Point at, int dy, int dx) const; FoundEntities neighbors(Point position, bool diag=false) const; diff --git a/tests/spatialmap.cpp b/tests/spatialmap.cpp index 70af120..c4168ec 100644 --- a/tests/spatialmap.cpp +++ b/tests/spatialmap.cpp @@ -186,7 +186,7 @@ TEST_CASE("SpatialMap::find", "[spatialmap-find]") { REQUIRE(no_collide != should_collide); } -TEST_CASE("SpatialMap::neighbors", "[spatialmap]") { +TEST_CASE("SpatialMap::neighbors", "[spatialmap-neighbors]") { DinkyECS::World world; SpatialMap map; @@ -203,7 +203,7 @@ TEST_CASE("SpatialMap::neighbors", "[spatialmap]") { auto result = map.neighbors(at, true); REQUIRE(result.found); - REQUIRE(result.nearby.size() > 0); + REQUIRE(result.nearby.size() == 2); bool maybe = result.nearby[0] == enemy1 || result.nearby[1] == enemy1; REQUIRE(maybe);