add entity:set_enabled(...)

This commit is contained in:
MihailRis 2025-01-21 05:31:07 +03:00
parent 13fde2116d
commit f2101f6504
6 changed files with 32 additions and 3 deletions

View File

@ -26,6 +26,9 @@ entity:get_uid() -> int
entity:get_component(name: str) -> component or nil entity:get_component(name: str) -> component or nil
-- Checks for the presence of a component by name -- Checks for the presence of a component by name
entity:has_component(name: str) -> bool entity:has_component(name: str) -> bool
-- Enables/disables the component
entity:set_enabled(name: str, enable: bool)
``` ```
## Built-in components ## Built-in components

View File

@ -26,6 +26,9 @@ entity:get_uid() -> int
entity:get_component(name: str) -> компонент или nil entity:get_component(name: str) -> компонент или nil
-- Проверяет наличие компонента по имени -- Проверяет наличие компонента по имени
entity:has_component(name: str) -> bool entity:has_component(name: str) -> bool
-- Включает/выключает компонент по имени
entity:set_enabled(name: str, enable: bool)
``` ```
## Встроенные компоненты ## Встроенные компоненты

View File

@ -68,6 +68,22 @@ local Entity = {__index={
def_index=function(self) return entities.get_def(self.eid) end, def_index=function(self) return entities.get_def(self.eid) end,
def_name=function(self) return entities.def_name(entities.get_def(self.eid)) end, def_name=function(self) return entities.def_name(entities.get_def(self.eid)) end,
get_player=function(self) return entities.get_player(self.eid) end, get_player=function(self) return entities.get_player(self.eid) end,
set_enabled=function(self, name, flag)
local comp = self.components[name]
if comp then
if flag then
if comp.__disabled and comp.on_enable then
comp.on_enable()
end
comp.__disabled = nil
else
if not comp.__disabled and comp.on_disable then
comp.on_disable()
end
comp.__disabled = true
end
end
end,
}} }}
local entities = {} local entities = {}
@ -99,7 +115,7 @@ return {
end end
for _, component in pairs(entity.components) do for _, component in pairs(entity.components) do
local callback = component.on_update local callback = component.on_update
if callback then if not component.__disabled and callback then
local result, err = pcall(callback, tps) local result, err = pcall(callback, tps)
if err then if err then
debug.error(err) debug.error(err)
@ -113,7 +129,7 @@ return {
for _,entity in pairs(entities) do for _,entity in pairs(entities) do
for _, component in pairs(entity.components) do for _, component in pairs(entity.components) do
local callback = component.on_render local callback = component.on_render
if callback then if not component.__disabled and callback then
local result, err = pcall(callback, delta) local result, err = pcall(callback, delta)
if err then if err then
debug.error(err) debug.error(err)

View File

@ -104,6 +104,9 @@ namespace dv {
} }
boolean_t value::asBoolean() const { boolean_t value::asBoolean() const {
if (type == value_type::none) {
return false;
}
check_type(type, value_type::boolean); check_type(type, value_type::boolean);
return val.boolean; return val.boolean;
} }

View File

@ -614,6 +614,10 @@ static void process_entity_callback(
) { ) {
auto L = lua::get_main_state(); auto L = lua::get_main_state();
lua::pushenv(L, *env); lua::pushenv(L, *env);
if (lua::hasfield(L, "__disabled")) {
lua::pop(L);
return;
}
if (lua::getfield(L, name)) { if (lua::getfield(L, name)) {
if (args) { if (args) {
lua::call_nothrow(L, args(L), 0); lua::call_nothrow(L, args(L), 0);