update entities library semantics

This commit is contained in:
MihailRis 2024-07-18 11:54:37 +03:00
parent d1696eea12
commit d285ffb2f7
5 changed files with 55 additions and 9 deletions

View File

@ -20,8 +20,17 @@ entities.spawn(name: str, pos: vec3, [optional] args: table)
-- Checks the existence of an entity by a unique identifier. -- Checks the existence of an entity by a unique identifier.
entities.exists(uid: int) -> bool entities.exists(uid: int) -> bool
-- Returns entity name (string ID). -- Returns entity definition index by UID
entities.name(uid: int) -> str entities.get_def(uid: int) -> int
-- Returns entity definition name by index (string ID).
entities.def_name(id: int) -> str
-- Returns entity definition index by name (integer ID).
entities.def_index(name: str) -> int
-- Returns number of available entity definitions
entities.defs_count() -> int
-- Returns a table of all loaded entities -- Returns a table of all loaded entities
entities.get_all() -> table entities.get_all() -> table

View File

@ -20,8 +20,17 @@ entities.spawn(name: str, pos: vec3, [optional] args: table)
-- Проверяет наличие сущности по уникальному идентификатору. -- Проверяет наличие сущности по уникальному идентификатору.
entities.exists(uid: int) -> bool entities.exists(uid: int) -> bool
-- Возвращает имя сущности (строковый ID). -- Возвращает индекс определения сущности по UID
entities.name(uid: int) -> str entities.get_def(uid: int) -> int
-- Возвращает имя определения сущности по индексу (строковый ID).
entities.def_name(id: int) -> str
-- Возвращает индекс определения сущности по имени (числовой ID).
entities.def_index(name: str) -> int
-- Возвращает число доступных определений сущностей
entities.defs_count() -> int
-- Возвращает таблицу всех загруженных сущностей -- Возвращает таблицу всех загруженных сущностей
entities.get_all() -> table entities.get_all() -> table

View File

@ -62,7 +62,7 @@ local Entity = {__index={
get_component=function(self, name) return self.components[name] end, get_component=function(self, name) return self.components[name] end,
has_component=function(self, name) return self.components[name] ~= nil end, has_component=function(self, name) return self.components[name] ~= nil end,
get_uid=function(self) return self.eid end, get_uid=function(self) return self.eid end,
get_name=function(self) return entities.name(self.eid) end, get_def=function(self) return entities.get_def(self.eid) end,
}} }}
local entities = {} local entities = {}

View File

@ -21,7 +21,7 @@ static Block* require_block(lua::State* L) {
return indices->blocks.get(id); return indices->blocks.get(id);
} }
static int l_name(lua::State* L) { static int l_get_def(lua::State* L) {
if (auto def = require_block(L)) { if (auto def = require_block(L)) {
return lua::pushstring(L, def->name); return lua::pushstring(L, def->name);
} }
@ -382,7 +382,7 @@ static int l_raycast(lua::State* L) {
const luaL_Reg blocklib [] = { const luaL_Reg blocklib [] = {
{"index", lua::wrap<l_index>}, {"index", lua::wrap<l_index>},
{"name", lua::wrap<l_name>}, {"name", lua::wrap<l_get_def>},
{"material", lua::wrap<l_material>}, {"material", lua::wrap<l_material>},
{"caption", lua::wrap<l_caption>}, {"caption", lua::wrap<l_caption>},
{"defs_count", lua::wrap<l_count>}, {"defs_count", lua::wrap<l_count>},

View File

@ -12,11 +12,36 @@
using namespace scripting; using namespace scripting;
static EntityDef* require_entity_def(lua::State* L) {
auto indices = content->getIndices();
auto id = lua::tointeger(L, 1);
if (static_cast<size_t>(id) >= indices->entities.count()) {
return nullptr;
}
return indices->entities.get(id);
}
static int l_exists(lua::State* L) { static int l_exists(lua::State* L) {
return lua::pushboolean(L, get_entity(L, 1).has_value()); return lua::pushboolean(L, get_entity(L, 1).has_value());
} }
static int l_name(lua::State* L) { static int l_def_index(lua::State* L) {
auto name = lua::require_string(L, 1);
return lua::pushinteger(L, content->entities.require(name).rt.id);
}
static int l_def_name(lua::State* L) {
if (auto def = require_entity_def(L)) {
return lua::pushstring(L, def->name);
}
return 0;
}
static int l_defs_count(lua::State* L) {
return lua::pushinteger(L, indices->entities.count());
}
static int l_get_def(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
return lua::pushstring(L, entity->getDef().name); return lua::pushstring(L, entity->getDef().name);
} }
@ -158,7 +183,10 @@ static int l_raycast(lua::State* L) {
const luaL_Reg entitylib [] = { const luaL_Reg entitylib [] = {
{"exists", lua::wrap<l_exists>}, {"exists", lua::wrap<l_exists>},
{"name", lua::wrap<l_name>}, {"def_index", lua::wrap<l_def_index>},
{"def_name", lua::wrap<l_def_name>},
{"get_def", lua::wrap<l_get_def>},
{"defs_count", lua::wrap<l_defs_count>},
{"spawn", lua::wrap<l_spawn>}, {"spawn", lua::wrap<l_spawn>},
{"despawn", lua::wrap<l_despawn>}, {"despawn", lua::wrap<l_despawn>},
{"get_skeleton", lua::wrap<l_get_skeleton>}, {"get_skeleton", lua::wrap<l_get_skeleton>},