Now also combat works no matter what's on the ground. Closes #81.

master
Zed A. Shaw 1 day ago
parent f84b63f0e6
commit 05d54ff661
  1. 2
      Makefile
  2. 2
      gui/combat_ui.cpp
  3. 33
      spatialmap.cpp
  4. 1
      spatialmap.hpp
  5. 4
      tests/spatialmap.cpp

@ -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'

@ -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 ]"
);
}

@ -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};

@ -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<bool(CollisionData)> cb) const;
void find_neighbor(EntityList &result, Point at, int dy, int dx) const;
FoundEntities neighbors(Point position, bool diag=false) const;

@ -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);

Loading…
Cancel
Save