From 94dd69350de0fd1d9629d4bdf69fb89fb74765b4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 27 Jun 2024 07:43:38 +0300 Subject: [PATCH] add Transform and Rigidbody components (test) --- res/content/base/scripts/hud.lua | 4 ++-- src/logic/scripting/lua/api_lua.hpp | 2 ++ src/logic/scripting/lua/libentity.cpp | 28 ++++++++++++++++++++++++-- src/logic/scripting/lua/lua_engine.cpp | 4 ++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index d9f27bc0..09e83f6b 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -7,7 +7,7 @@ function on_hud_open() local pvel = {player.get_vel(pid)} local eid = entity.spawn("base:drop") local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE) - entity.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) - entity.set_rot(eid, mat4.rotate({0, 1, 0}, math.random() * 360)) + Rigidbody.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) + Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random() * 360)) end) end diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 7026325d..5b709c8d 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -21,8 +21,10 @@ 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 []; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index fae0054b..36676655 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -31,6 +31,20 @@ static int l_spawn(lua::State* L) { return lua::pushinteger(L, id); } +static int l_get_pos(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3_arr(L, entity->getTransform().pos); + } + return 0; +} + +static int l_set_pos(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getTransform().pos = lua::tovec3(L, 2); + } + return 0; +} + static int l_get_vel(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushvec3_arr(L, entity->getHitbox().velocity); @@ -61,9 +75,19 @@ static int l_set_rot(lua::State* L) { const luaL_Reg entitylib [] = { {"spawn", lua::wrap}, - {"get_vel", lua::wrap}, - {"set_vel", 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 [] = { + {"get_vel", lua::wrap}, + {"set_vel", lua::wrap}, + {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 6d5facfb..3339204d 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -48,6 +48,10 @@ static void create_libs(lua::State* L) { openlib(L, "vec4", vec4lib); openlib(L, "world", worldlib); + // components + openlib(L, "Rigidbody", rigidbodylib); + openlib(L, "Transform", transformlib); + addfunc(L, "print", lua::wrap); }