diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 8245a22a..22d03672 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -4,20 +4,20 @@ local DROP_INIT_VEL = {0, 3, 0} function on_hud_open() input.add_callback("player.drop", function () for i=1,80 do - local pid = hud.get_player() - local pvel = {player.get_vel(pid)} - local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) - local throw_force = vec3.mul(vec3.add(player.get_dir(pid), - { - (math.random() - 0.5) * 1, - (math.random() - 0.5) * 1, - (math.random() - 0.5) * 1 - }), DROP_FORCE) + local pid = hud.get_player() + local pvel = {player.get_vel(pid)} + local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) + local throw_force = vec3.mul(vec3.add(player.get_dir(pid), + { + (math.random() - 0.5) * 1, + (math.random() - 0.5) * 1, + (math.random() - 0.5) * 1 + }), DROP_FORCE) - local drop = entity.spawn("base:drop", ppos) - drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) - drop.transform:set_rot(mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360), - {1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360)) + local drop = entities.spawn("base:drop", ppos) + drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) + drop.transform:set_rot(mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360), + {1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360)) end end) end diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index eb7a41b6..0719d932 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -24,6 +24,7 @@ function new_Rigidbody(eid) end local Modeltree = {__index={ + get_model=function(self) return __modeltree.get_model(self.eid) end, }} function new_Modeltree(eid) @@ -33,7 +34,7 @@ end -- Entity class local Entity = {__index={ - despawn=function(self) return entity.despawn(self.eid) end, + despawn=function(self) return entities.despawn(self.eid) end, }} local entities = {} diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 5b709c8d..5287813d 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -21,15 +21,18 @@ extern const luaL_Reg jsonlib []; extern const luaL_Reg mat4lib []; extern const luaL_Reg packlib []; extern const luaL_Reg playerlib []; -extern const luaL_Reg rigidbodylib []; extern const luaL_Reg timelib []; extern const luaL_Reg tomllib []; -extern const luaL_Reg transformlib []; extern const luaL_Reg vec2lib []; extern const luaL_Reg vec3lib []; extern const luaL_Reg vec4lib []; extern const luaL_Reg worldlib []; +// Components +extern const luaL_Reg modeltreelib []; +extern const luaL_Reg rigidbodylib []; +extern const luaL_Reg transformlib []; + // Lua Overrides extern int l_print(lua::State* L); diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 3cb3de87..db858742 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -4,6 +4,7 @@ #include "../../../world/Level.hpp" #include "../../../objects/Player.hpp" #include "../../../objects/Entities.hpp" +#include "../../../objects/rigging.hpp" #include "../../../physics/Hitbox.hpp" #include "../../../window/Camera.hpp" #include "../../../frontend/hud.hpp" @@ -110,6 +111,16 @@ static int l_get_size(lua::State* L) { return 0; } +static int l_modeltree_get_model(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + auto* rigConfig = rig.config; + auto index = lua::tointeger(L, 2); + return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); + } + return 0; +} + const luaL_Reg entitylib [] = { {"exists", lua::wrap}, {"spawn", lua::wrap}, @@ -117,13 +128,18 @@ const luaL_Reg entitylib [] = { {NULL, NULL} }; - const luaL_Reg transformlib [] = { +const luaL_Reg modeltreelib [] = { + {"get_model", lua::wrap}, + {NULL, NULL} +}; + +const luaL_Reg transformlib [] = { {"get_pos", lua::wrap}, {"set_pos", lua::wrap}, {"get_rot", lua::wrap}, {"set_rot", lua::wrap}, {NULL, NULL} - }; +}; const luaL_Reg rigidbodylib [] = { {"is_enabled", lua::wrap}, diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index fab28a7b..d99aa603 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -22,8 +22,8 @@ static void remove_lib_funcs(lua::State* L, const char* libname, const char* fun pushnil(L); setfield(L, funcs[i], -2); } + pop(L); } - pop(L); } static void create_libs(lua::State* L) { @@ -31,7 +31,6 @@ static void create_libs(lua::State* L) { openlib(L, "block", blocklib); openlib(L, "console", consolelib); openlib(L, "core", corelib); - openlib(L, "entity", entitylib); openlib(L, "file", filelib); openlib(L, "gui", guilib); openlib(L, "input", inputlib); @@ -48,7 +47,10 @@ static void create_libs(lua::State* L) { openlib(L, "vec4", vec4lib); openlib(L, "world", worldlib); + openlib(L, "entities", entitylib); + // components + openlib(L, "__modeltree", modeltreelib); openlib(L, "__rigidbody", rigidbodylib); openlib(L, "__transform", transformlib); diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index d46f3472..a62b03d5 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -28,6 +28,10 @@ void Entity::destroy() { } } +rigging::Rig& Entity::getModeltree() const { + return registry.get(entity); +} + Entities::Entities(Level* level) : level(level) { } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 35811875..953470fa 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -69,7 +69,8 @@ class LineBatch; class ModelBatch; class Frustum; class Entities; -namespace riggining { + +namespace rigging { struct Rig; class RigConfig; } @@ -112,6 +113,8 @@ public: return registry.get(entity); } + rigging::Rig& getModeltree() const; + entityid_t getUID() const { return registry.get(entity).uid; } diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index e84bb42e..c1028890 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -101,6 +101,10 @@ namespace rigging { std::string_view src, std::string_view file ); + + const std::vector& getNodes() const { + return nodes; + } }; };