add entities.get_all(...), .get_all_in_box(...), .get_all_in_radius(...)

This commit is contained in:
MihailRis
2024-07-17 14:51:55 +03:00
parent b499714d4b
commit 1eac343619
7 changed files with 100 additions and 0 deletions
+28
View File
@@ -57,6 +57,32 @@ static int l_set_skeleton(lua::State* L) {
return 0;
}
static int l_get_all_in_box(lua::State* L) {
auto pos = lua::tovec<3>(L, 1);
auto size = lua::tovec<3>(L, 2);
auto found = level->entities->getAllInside(AABB(pos, pos + size));
lua::createtable(L, found.size(), 0);
for (size_t i = 0; i < found.size(); i++) {
const auto& entity = found[i];
lua::pushinteger(L, entity.getUID());
lua::rawseti(L, i+1);
}
return 1;
}
static int l_get_all_in_radius(lua::State* L) {
auto pos = lua::tovec<3>(L, 1);
auto radius = lua::tonumber(L, 2);
auto found = level->entities->getAllInRadius(pos, radius);
lua::createtable(L, found.size(), 0);
for (size_t i = 0; i < found.size(); i++) {
const auto& entity = found[i];
lua::pushinteger(L, entity.getUID());
lua::rawseti(L, i+1);
}
return 1;
}
static int l_raycast(lua::State* L) {
auto start = lua::tovec<3>(L, 1);
auto dir = lua::tovec<3>(L, 2);
@@ -128,6 +154,8 @@ const luaL_Reg entitylib [] = {
{"despawn", lua::wrap<l_despawn>},
{"get_skeleton", lua::wrap<l_get_skeleton>},
{"set_skeleton", lua::wrap<l_set_skeleton>},
{"get_all_in_box", lua::wrap<l_get_all_in_box>},
{"get_all_in_radius", lua::wrap<l_get_all_in_radius>},
{"raycast", lua::wrap<l_raycast>},
{NULL, NULL}
};
+17
View File
@@ -503,3 +503,20 @@ std::vector<Entity> Entities::getAllInside(AABB aabb) {
}
return collected;
}
std::vector<Entity> Entities::getAllInRadius(glm::vec3 center, float radius) {
std::vector<Entity> collected;
auto view = registry.view<Transform>();
for (auto [entity, transform] : view.each()) {
if (glm::distance2(transform.pos, center) <= radius*radius) {
const auto& found = uids.find(entity);
if (found == uids.end()) {
continue;
}
if (auto wrapper = get(found->second)) {
collected.push_back(*wrapper);
}
}
}
return collected;
}
+1
View File
@@ -210,6 +210,7 @@ public:
void onSave(const Entity& entity);
bool hasBlockingInside(AABB aabb);
std::vector<Entity> getAllInside(AABB aabb);
std::vector<Entity> getAllInRadius(glm::vec3 center, float radius);
void despawn(entityid_t id);
dynamic::Value serialize(const Entity& entity);