LEL can now do hit detection on squares.
This commit is contained in:
parent
aa149b6574
commit
7f9e200abe
4 changed files with 22 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
11
lel.cpp
11
lel.cpp
|
@ -69,6 +69,17 @@ namespace lel {
|
|||
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 copy = parent;
|
||||
|
||||
|
|
2
lel.hpp
2
lel.hpp
|
@ -2,6 +2,7 @@
|
|||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
namespace lel {
|
||||
struct Cell {
|
||||
|
@ -39,6 +40,7 @@ namespace lel {
|
|||
void reset();
|
||||
bool parse(std::string input);
|
||||
void finalize();
|
||||
std::optional<std::string> hit(int x, int y);
|
||||
};
|
||||
|
||||
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.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…
Add table
Add a link
Reference in a new issue