fix components order & add skeleton:get_matrix overload & add skeleton:reset_pose

This commit is contained in:
MihailRis 2026-07-26 15:08:55 +03:00
parent 8a9b4a9e9c
commit 8242d299ed
3 changed files with 29 additions and 8 deletions

View file

@ -48,8 +48,9 @@ end
local Skeleton = {__index={
get_model=function(self, i) return __skeleton.get_model(self.eid, i) end,
set_model=function(self, i, s) return __skeleton.set_model(self.eid, i, s) end,
get_matrix=function(self, i) return __skeleton.get_matrix(self.eid, i) end,
get_matrix=function(self, i, ...) return __skeleton.get_matrix(self.eid, i, ...) end,
set_matrix=function(self, i, m) return __skeleton.set_matrix(self.eid, i, m) end,
reset_pose=function(self) return __skeleton.reset_pose(self.eid) end,
get_texture=function(self, s) return __skeleton.get_texture(self.eid, s) end,
set_texture=function(self, s, s2) return __skeleton.set_texture(self.eid, s, s2) end,
index=function(self, s) return __skeleton.index(self.eid, s) end,
@ -84,7 +85,7 @@ local Entity = {__index={
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,
set_enabled=function(self, name, flag)
local comp = self.components[name]
local comp = self.components[name]
if comp then
if flag then
if comp.__disabled and comp.on_enable then
@ -110,6 +111,7 @@ return {
entity.rigidbody = new_Rigidbody(eid)
entity.skeleton = new_Skeleton(eid)
entity.components = {}
entity.ordered_components = {}
entities[eid] = entity;
return entity
end,
@ -120,6 +122,7 @@ return {
local entity = entities[eid]
if entity then
entity.components = nil
entity.ordered_components = nil
entities[eid] = nil;
end
end,
@ -128,10 +131,10 @@ return {
if uid % parts ~= part then
goto continue
end
for _, component in pairs(entity.components) do
for _, component in ipairs(entity.ordered_components) do
local callback = component.on_update
if not component.__disabled and callback then
local result, err = pcall(callback, tps)
local _, err = pcall(callback, tps)
if err then
debug.error(err)
end
@ -142,7 +145,7 @@ return {
end,
physics_update = function(delta)
for uid, entity in pairs(entities) do
for _, component in pairs(entity.components) do
for _, component in ipairs(entity.ordered_components) do
local callback = component.on_physics_update
if not component.__disabled and callback then
local result, err = pcall(callback, delta)
@ -155,10 +158,10 @@ return {
end,
render = function(delta)
for _,entity in pairs(entities) do
for _, component in pairs(entity.components) do
for _, component in ipairs(entity.ordered_components) do
local callback = component.on_render
if not component.__disabled and callback then
local result, err = pcall(callback, delta)
local _, err = pcall(callback, delta)
if err then
debug.error(err)
end

View file

@ -66,7 +66,11 @@ static int l_set_model(lua::State* L) {
static int l_get_matrix(lua::State* L) {
if (auto skeleton = get_skeleton(L)) {
auto index = index_range_check(*skeleton, lua::tointeger(L, 2));
return lua::pushmat4(L, skeleton->pose.matrices[index]);
if (lua::istable(L, 3)) {
lua::setmat4(L, 3, skeleton->pose.matrices[index]);
} else {
return lua::pushmat4(L, skeleton->pose.matrices[index]);
}
}
return 0;
}
@ -79,6 +83,14 @@ static int l_set_matrix(lua::State* L) {
return 0;
}
static int l_reset_pose(lua::State* L) {
if (auto skeleton = get_skeleton(L)) {
auto& pose = skeleton->pose;
std::fill(pose.matrices.begin(), pose.matrices.end(), glm::mat4(1.0f));
}
return 0;
}
static int l_get_texture(lua::State* L) {
if (auto skeleton = get_skeleton(L)) {
const auto& found = skeleton->textures.find(lua::require_string(L, 2));
@ -169,6 +181,7 @@ const luaL_Reg skeletonlib[] = {
{"set_model", lua::wrap<l_set_model>},
{"get_matrix", lua::wrap<l_get_matrix>},
{"set_matrix", lua::wrap<l_set_matrix>},
{"reset_pose", lua::wrap<l_reset_pose>},
{"get_texture", lua::wrap<l_get_texture>},
{"set_texture", lua::wrap<l_set_texture>},
{"index", lua::wrap<l_index>},

View file

@ -37,6 +37,11 @@ static inline const std::string STDCOMP = "stdcomp";
lua::setfield(L, name);
lua::pop(L);
}
if (lua::getfield(L, "ordered_components")) {
lua::pushenv(L, id);
lua::rawseti(L, lua::objlen(L, -2) + 1);
lua::pop(L);
}
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508