diff --git a/src/logic/scripting/lua/lib__modeltree.cpp b/src/logic/scripting/lua/lib__modeltree.cpp new file mode 100644 index 00000000..5fb49e79 --- /dev/null +++ b/src/logic/scripting/lua/lib__modeltree.cpp @@ -0,0 +1,56 @@ +#include "libentity.hpp" + +#include "../../../objects/rigging.hpp" + +static int index_range_check(const rigging::Rig& rig, lua::Integer index) { + if (static_cast(index) >= rig.pose.matrices.size()) { + throw std::runtime_error("index out of range [0, " + + std::to_string(rig.pose.matrices.size()) + + "]"); + } + return static_cast(index); +} + +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 = index_range_check(rig, lua::tointeger(L, 2)); + return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); + } + return 0; +} + +static int l_modeltree_get_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + auto index = index_range_check(rig, lua::tointeger(L, 2)); + return lua::pushmat4(L, rig.pose.matrices[index]); + } + return 0; +} + +static int l_modeltree_set_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + auto index = index_range_check(rig, lua::tointeger(L, 2)); + rig.pose.matrices[index] = lua::tomat4(L, 3); + } + return 0; +} + +static int l_modeltree_set_texture(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + rig.textures[lua::require_string(L, 2)] = lua::require_string(L, 3); + } + return 0; +} + +const luaL_Reg modeltreelib [] = { + {"get_model", lua::wrap}, + {"get_matrix", lua::wrap}, + {"set_matrix", lua::wrap}, + {"set_texture", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lib__rigidbody.cpp b/src/logic/scripting/lua/lib__rigidbody.cpp new file mode 100644 index 00000000..d7f68cd6 --- /dev/null +++ b/src/logic/scripting/lua/lib__rigidbody.cpp @@ -0,0 +1,69 @@ +#include "libentity.hpp" + +static int l_rigidbody_get_vel(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.velocity); + } + return 0; +} + +static int l_rigidbody_set_vel(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getRigidbody().hitbox.velocity = lua::tovec3(L, 2); + } + return 0; +} + +static int l_rigidbody_is_enabled(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + lua::pushboolean(L, entity->getRigidbody().enabled); + } + return 0; +} + +static int l_rigidbody_set_enabled(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getRigidbody().enabled = lua::toboolean(L, 2); + } + return 0; +} + +static int l_rigidbody_get_size(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.halfsize * 2.0f); + } + return 0; +} + +static int l_rigidbody_set_size(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getRigidbody().hitbox.halfsize = lua::tovec3(L, 2) * 0.5f; + } + return 0; +} + +static int l_rigidbody_get_gravity_scale(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushnumber(L, entity->getRigidbody().hitbox.gravityScale); + } + return 0; +} + +static int l_rigidbody_set_gravity_scale(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getRigidbody().hitbox.gravityScale = lua::tonumber(L, 2); + } + return 0; +} + +const luaL_Reg rigidbodylib [] = { + {"is_enabled", lua::wrap}, + {"set_enabled", lua::wrap}, + {"get_vel", lua::wrap}, + {"set_vel", lua::wrap}, + {"get_size", lua::wrap}, + {"set_size", lua::wrap}, + {"get_gravity_scale", lua::wrap}, + {"set_gravity_scale", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lib__transform.cpp b/src/logic/scripting/lua/lib__transform.cpp new file mode 100644 index 00000000..cd0ae15f --- /dev/null +++ b/src/logic/scripting/lua/lib__transform.cpp @@ -0,0 +1,53 @@ +#include "libentity.hpp" + +static int l_transform_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_transform_set_pos(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getTransform().setPos(lua::tovec3(L, 2)); + } + return 0; +} + +static int l_transform_get_size(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3_arr(L, entity->getTransform().size); + } + return 0; +} + +static int l_transform_set_size(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getTransform().setSize(lua::tovec3(L, 2)); + } + return 0; +} + +static int l_transform_get_rot(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushmat4(L, entity->getTransform().rot); + } + return 0; +} + +static int l_transform_set_rot(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getTransform().setRot(lua::tomat4(L, 2)); + } + return 0; +} + +const luaL_Reg transformlib [] = { + {"get_pos", lua::wrap}, + {"set_pos", lua::wrap}, + {"get_size", lua::wrap}, + {"set_size", lua::wrap}, + {"get_rot", lua::wrap}, + {"set_rot", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index db363463..2cd5da71 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -1,29 +1,13 @@ -#include "api_lua.hpp" +#include "libentity.hpp" -#include "../../LevelController.hpp" -#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" #include "../../../content/Content.hpp" #include "../../../engine.hpp" -#include - -namespace scripting { - extern Hud* hud; -} using namespace scripting; -static std::optional get_entity(lua::State* L, int idx) { - auto id = lua::tointeger(L, idx); - auto level = controller->getLevel(); - return level->entities->get(id); -} - static int l_entity_exists(lua::State* L) { return lua::pushboolean(L, get_entity(L, 1).has_value()); } @@ -60,149 +44,6 @@ static int l_entity_set_rig(lua::State* L) { return 0; } -static int l_transform_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_transform_set_pos(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getTransform().setPos(lua::tovec3(L, 2)); - } - return 0; -} - -static int l_transform_get_size(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - return lua::pushvec3_arr(L, entity->getTransform().size); - } - return 0; -} - -static int l_transform_set_size(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getTransform().setSize(lua::tovec3(L, 2)); - } - return 0; -} - -static int l_transform_get_rot(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - return lua::pushmat4(L, entity->getTransform().rot); - } - return 0; -} - -static int l_transform_set_rot(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getTransform().setRot(lua::tomat4(L, 2)); - } - return 0; -} - -static int l_rigidbody_get_vel(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.velocity); - } - return 0; -} - -static int l_rigidbody_set_vel(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getRigidbody().hitbox.velocity = lua::tovec3(L, 2); - } - return 0; -} - -static int l_rigidbody_is_enabled(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - lua::pushboolean(L, entity->getRigidbody().enabled); - } - return 0; -} - -static int l_rigidbody_set_enabled(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getRigidbody().enabled = lua::toboolean(L, 2); - } - return 0; -} - -static int l_rigidbody_get_size(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.halfsize * 2.0f); - } - return 0; -} - -static int l_rigidbody_set_size(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getRigidbody().hitbox.halfsize = lua::tovec3(L, 2) * 0.5f; - } - return 0; -} - -static int l_rigidbody_get_gravity_scale(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - return lua::pushnumber(L, entity->getRigidbody().hitbox.gravityScale); - } - return 0; -} - -static int l_rigidbody_set_gravity_scale(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - entity->getRigidbody().hitbox.gravityScale = lua::tonumber(L, 2); - } - return 0; -} - -static int index_range_check(const rigging::Rig& rig, lua::Integer index) { - if (static_cast(index) >= rig.pose.matrices.size()) { - throw std::runtime_error("index out of range [0, " + - std::to_string(rig.pose.matrices.size()) + - "]"); - } - return static_cast(index); -} - -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 = index_range_check(rig, lua::tointeger(L, 2)); - return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); - } - return 0; -} - -static int l_modeltree_get_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - auto index = index_range_check(rig, lua::tointeger(L, 2)); - return lua::pushmat4(L, rig.pose.matrices[index]); - } - return 0; -} - -static int l_modeltree_set_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - auto index = index_range_check(rig, lua::tointeger(L, 2)); - rig.pose.matrices[index] = lua::tomat4(L, 3); - } - return 0; -} - -static int l_modeltree_set_texture(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - rig.textures[lua::require_string(L, 2)] = lua::require_string(L, 3); - } - return 0; -} - const luaL_Reg entitylib [] = { {"exists", lua::wrap}, {"spawn", lua::wrap}, @@ -210,33 +51,3 @@ const luaL_Reg entitylib [] = { {"set_rig", lua::wrap}, {NULL, NULL} }; - -const luaL_Reg modeltreelib [] = { - {"get_model", lua::wrap}, - {"get_matrix", lua::wrap}, - {"set_matrix", lua::wrap}, - {"set_texture", lua::wrap}, - {NULL, NULL} -}; - -const luaL_Reg transformlib [] = { - {"get_pos", lua::wrap}, - {"set_pos", lua::wrap}, - {"get_size", lua::wrap}, - {"set_size", lua::wrap}, - {"get_rot", lua::wrap}, - {"set_rot", lua::wrap}, - {NULL, NULL} -}; - -const luaL_Reg rigidbodylib [] = { - {"is_enabled", lua::wrap}, - {"set_enabled", lua::wrap}, - {"get_vel", lua::wrap}, - {"set_vel", lua::wrap}, - {"get_size", lua::wrap}, - {"set_size", lua::wrap}, - {"get_gravity_scale", lua::wrap}, - {"set_gravity_scale", lua::wrap}, - {NULL, NULL} -}; diff --git a/src/logic/scripting/lua/libentity.hpp b/src/logic/scripting/lua/libentity.hpp new file mode 100644 index 00000000..f2fa38e0 --- /dev/null +++ b/src/logic/scripting/lua/libentity.hpp @@ -0,0 +1,23 @@ +#ifndef LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_ +#define LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_ + +#include "api_lua.hpp" + +#include "../../LevelController.hpp" +#include "../../../frontend/hud.hpp" +#include "../../../world/Level.hpp" +#include "../../../objects/Entities.hpp" + +#include + +namespace scripting { + extern Hud* hud; +} + +inline std::optional get_entity(lua::State* L, int idx) { + auto id = lua::tointeger(L, idx); + auto level = scripting::controller->getLevel(); + return level->entities->get(id); +} + +#endif // LOGIC_SCRIPTING_LUA_LIBENTITY_HPP_