add entities.get_all(...), .get_all_in_box(...), .get_all_in_radius(...)
This commit is contained in:
parent
b499714d4b
commit
1eac343619
@ -14,6 +14,22 @@ entities.spawn(name: str, pos: vec3, [optional] args: table)
|
||||
|
||||
-- Checks the existence of an entity by a unique identifier.
|
||||
entities.exists(uid: int) -> bool
|
||||
|
||||
-- Returns a table of all loaded entities
|
||||
entities.get_all() -> table
|
||||
|
||||
-- Returns a table of loaded entities based on the passed list of UIDs
|
||||
entities.get_all(uids: array<int>) -> table
|
||||
|
||||
-- Returns a list of UIDs of entities inside the rectangular area
|
||||
-- pos - minimal area corner
|
||||
-- size - area size
|
||||
entities.get_all_in_box(pos: vec3, size: vec3) -> array<int>
|
||||
|
||||
-- Returns a list of UIDs of entities inside the radius
|
||||
-- center - center of the area
|
||||
-- radius - radius of the area
|
||||
entities.get_all_in_radius(center: vec3, radius: number) -> array<int>
|
||||
```
|
||||
|
||||
```lua
|
||||
|
||||
@ -14,6 +14,22 @@ entities.spawn(name: str, pos: vec3, [optional] args: table)
|
||||
|
||||
-- Проверяет наличие сущности по уникальному идентификатору.
|
||||
entities.exists(uid: int) -> bool
|
||||
|
||||
-- Возвращает таблицу всех загруженных сущностей
|
||||
entities.get_all() -> table
|
||||
|
||||
-- Возвращает таблицу загруженных сущностей по переданному списку UID
|
||||
entities.get_all(uids: array<int>) -> table
|
||||
|
||||
-- Возвращает список UID сущностей, попадающих в прямоугольную область
|
||||
-- pos - минимальный угол области
|
||||
-- size - размер области
|
||||
entities.get_all_in_box(pos: vec3, size: vec3) -> array<int>
|
||||
|
||||
-- Возвращает список UID сущностей, попадающих в радиус
|
||||
-- center - центр области
|
||||
-- radius - радиус области
|
||||
entities.get_all_in_radius(center: vec3, radius: number) -> array<int>
|
||||
```
|
||||
|
||||
```lua
|
||||
|
||||
@ -111,5 +111,16 @@ return {
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_all = function(uids)
|
||||
if uids == nil then
|
||||
return entities
|
||||
else
|
||||
local values = {}
|
||||
for _, uid in ipairs(uids) do
|
||||
values[uid] = entities[uid]
|
||||
end
|
||||
return values
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
@ -307,6 +307,17 @@ end
|
||||
|
||||
stdcomp = require "core:internal/stdcomp"
|
||||
entities.get = stdcomp.get_Entity
|
||||
entities.get_all = function(uids)
|
||||
if uids == nil then
|
||||
local values = {}
|
||||
for k,v in pairs(stdcomp.get_all()) do
|
||||
values[k] = v
|
||||
end
|
||||
return values
|
||||
else
|
||||
return stdcomp.get_all(uids)
|
||||
end
|
||||
end
|
||||
|
||||
math.randomseed(time.uptime()*1536227939)
|
||||
|
||||
|
||||
@ -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}
|
||||
};
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user