From 7f9e200abe1c7f1ec177e9125f5098aa1390431c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 15 Feb 2025 13:43:41 -0500 Subject: [PATCH] LEL can now do hit detection on squares. --- combat_ui.cpp | 3 ++- lel.cpp | 11 +++++++++++ lel.hpp | 2 ++ tests/lel.cpp | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/combat_ui.cpp b/combat_ui.cpp index 044a60b..a8c808b 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -28,8 +28,9 @@ namespace gui { auto inner_cell = lel::center(30, 40, cell); inner.setPosition({float(inner_cell.x), float(inner_cell.y)}); inner.setSize({float(inner_cell.w), float(inner_cell.h)}); - inner.setOutlineColor({100, 100, 100}); + inner.setOutlineColor({100, 0, 0}); inner.setOutlineThickness(5); + inner.setFillColor({50, 50, 50}); $shapes.push_back(inner); } } diff --git a/lel.cpp b/lel.cpp index d2297b7..6eeb192 100644 --- a/lel.cpp +++ b/lel.cpp @@ -69,6 +69,17 @@ namespace lel { cur = {0, 0}; } + std::optional 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 copy = parent; diff --git a/lel.hpp b/lel.hpp index eba8e90..63f73ec 100644 --- a/lel.hpp +++ b/lel.hpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace lel { struct Cell { @@ -39,6 +40,7 @@ namespace lel { void reset(); bool parse(std::string input); void finalize(); + std::optional hit(int x, int y); }; Cell center(int width, int height, Cell &parent); diff --git a/tests/lel.cpp b/tests/lel.cpp index f888150..e94b2be 100644 --- a/tests/lel.cpp +++ b/tests/lel.cpp @@ -35,4 +35,11 @@ TEST_CASE("test basic ops", "[lel]") { REQUIRE(cell.row < parser.rows); 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); }