LEL can now do hit detection on squares.

Zed A. Shaw 3 weeks ago
parent aa149b6574
commit 7f9e200abe
  1. 3
      combat_ui.cpp
  2. 11
      lel.cpp
  3. 2
      lel.hpp
  4. 7
      tests/lel.cpp

@ -28,8 +28,9 @@ namespace gui {
auto inner_cell = lel::center(30, 40, cell); auto inner_cell = lel::center(30, 40, cell);
inner.setPosition({float(inner_cell.x), float(inner_cell.y)}); inner.setPosition({float(inner_cell.x), float(inner_cell.y)});
inner.setSize({float(inner_cell.w), float(inner_cell.h)}); inner.setSize({float(inner_cell.w), float(inner_cell.h)});
inner.setOutlineColor({100, 100, 100}); inner.setOutlineColor({100, 0, 0});
inner.setOutlineThickness(5); inner.setOutlineThickness(5);
inner.setFillColor({50, 50, 50});
$shapes.push_back(inner); $shapes.push_back(inner);
} }
} }

@ -69,6 +69,17 @@ namespace lel {
cur = {0, 0}; cur = {0, 0};
} }
std::optional<std::string> Parser::hit(int x, int y) {
for(auto& [name, cell] : cells) {
if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h)) {
return name;
}
}
return std::nullopt;
}
Cell center(int width, int height, Cell &parent) { Cell center(int width, int height, Cell &parent) {
Cell copy = parent; Cell copy = parent;

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <functional> #include <functional>
#include <optional>
namespace lel { namespace lel {
struct Cell { struct Cell {
@ -39,6 +40,7 @@ namespace lel {
void reset(); void reset();
bool parse(std::string input); bool parse(std::string input);
void finalize(); void finalize();
std::optional<std::string> hit(int x, int y);
}; };
Cell center(int width, int height, Cell &parent); Cell center(int width, int height, Cell &parent);

@ -35,4 +35,11 @@ TEST_CASE("test basic ops", "[lel]") {
REQUIRE(cell.row < parser.rows); REQUIRE(cell.row < parser.rows);
REQUIRE(cell.col < parser.columns); REQUIRE(cell.col < parser.columns);
} }
auto hit = parser.hit(250, 250);
REQUIRE(*hit == "people");
auto nohit = parser.hit(1000, 1000);
REQUIRE(!nohit);
REQUIRE(nohit == std::nullopt);
} }

Loading…
Cancel
Save