From e4278de6d85d21ed0a99455d0b3739948d373463 Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sun, 4 Jan 2026 16:11:48 +0000 Subject: [PATCH] refactor: Remove PhysicsBridge integration from script engine and services for improved modularity --- src/script/lua_bindings.cpp | 84 +++----------------- src/script/script_engine.cpp | 5 -- src/script/script_engine.hpp | 5 -- src/services/impl/audio_command_service.cpp | 1 + src/services/impl/lua_script_service.cpp | 8 -- src/services/impl/lua_script_service.hpp | 2 - src/services/impl/script_engine_service.cpp | 1 + src/services/interfaces/i_script_service.hpp | 6 +- 8 files changed, 16 insertions(+), 96 deletions(-) diff --git a/src/script/lua_bindings.cpp b/src/script/lua_bindings.cpp index 70ec424..c0edcd2 100644 --- a/src/script/lua_bindings.cpp +++ b/src/script/lua_bindings.cpp @@ -108,38 +108,11 @@ int LuaBindings::LoadMeshFromFileWithServices(lua_State* L) { int LuaBindings::PhysicsCreateBox(lua_State* L) { sdl3cpp::logging::TraceGuard trace;; - auto* engine = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - const char* name = luaL_checkstring(L, 1); - sdl3cpp::logging::Logger::GetInstance().TraceVariable("name", name); - - if (!lua_istable(L, 2) || !lua_istable(L, 4) || !lua_istable(L, 5)) { - luaL_error(L, "physics_create_box expects vector tables for half extents, origin, and rotation"); - } - - std::array halfExtents = ReadVector3(L, 2); - float mass = static_cast(luaL_checknumber(L, 3)); - std::array origin = ReadVector3(L, 4); - std::array rotation = ReadQuaternion(L, 5); - - btTransform transform; - transform.setIdentity(); - transform.setOrigin(btVector3(origin[0], origin[1], origin[2])); - transform.setRotation(btQuaternion(rotation[0], rotation[1], rotation[2], rotation[3])); - - std::string error; - if (!engine->GetPhysicsBridge().addBoxRigidBody( - name, - btVector3(halfExtents[0], halfExtents[1], halfExtents[2]), - mass, - transform, - error)) { - lua_pushnil(L); - lua_pushstring(L, error.c_str()); - return 2; - } - - lua_pushboolean(L, 1); - return 1; + (void)lua_touserdata(L, lua_upvalueindex(1)); + (void)luaL_checkstring(L, 1); + lua_pushnil(L); + lua_pushstring(L, "Physics service not available"); + return 2; } int LuaBindings::PhysicsCreateBoxWithServices(lua_State* L) { @@ -185,10 +158,9 @@ int LuaBindings::PhysicsCreateBoxWithServices(lua_State* L) { } int LuaBindings::PhysicsStepSimulation(lua_State* L) { - auto* engine = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - float deltaTime = static_cast(luaL_checknumber(L, 1)); - int steps = engine->GetPhysicsBridge().stepSimulation(deltaTime); - lua_pushinteger(L, steps); + (void)lua_touserdata(L, lua_upvalueindex(1)); + (void)luaL_checknumber(L, 1); + lua_pushinteger(L, 0); return 1; } @@ -205,41 +177,11 @@ int LuaBindings::PhysicsStepSimulationWithServices(lua_State* L) { } int LuaBindings::PhysicsGetTransform(lua_State* L) { - auto* engine = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - const char* name = luaL_checkstring(L, 1); - - btTransform transform; - std::string error; - if (!engine->GetPhysicsBridge().getRigidBodyTransform(name, transform, error)) { - lua_pushnil(L); - lua_pushstring(L, error.c_str()); - return 2; - } - - lua_newtable(L); - lua_newtable(L); - const btVector3& origin = transform.getOrigin(); - lua_pushnumber(L, origin.x()); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, origin.y()); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, origin.z()); - lua_rawseti(L, -2, 3); - lua_setfield(L, -2, "position"); - - lua_newtable(L); - const btQuaternion& orientation = transform.getRotation(); - lua_pushnumber(L, orientation.x()); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, orientation.y()); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, orientation.z()); - lua_rawseti(L, -2, 3); - lua_pushnumber(L, orientation.w()); - lua_rawseti(L, -2, 4); - lua_setfield(L, -2, "rotation"); - - return 1; + (void)lua_touserdata(L, lua_upvalueindex(1)); + (void)luaL_checkstring(L, 1); + lua_pushnil(L); + lua_pushstring(L, "Physics service not available"); + return 2; } int LuaBindings::PhysicsGetTransformWithServices(lua_State* L) { diff --git a/src/script/script_engine.cpp b/src/script/script_engine.cpp index 2b5215e..f360a26 100644 --- a/src/script/script_engine.cpp +++ b/src/script/script_engine.cpp @@ -22,7 +22,6 @@ ScriptEngine::ScriptEngine(const std::filesystem::path& scriptPath, LuaBindingCo : L_(luaL_newstate()), scriptDirectory_(scriptPath.parent_path()), debugEnabled_(debugEnabled), - physicsBridge_(std::make_unique()), sceneManager_(std::make_unique(L_)), shaderManager_(std::make_unique(L_)), guiManager_(std::make_unique(L_)) { @@ -95,10 +94,6 @@ std::unordered_map ScriptEngine::Lo return shaderManager_->LoadShaderPathsMap(); } -PhysicsBridge& ScriptEngine::GetPhysicsBridge() { - return *physicsBridge_; -} - std::vector ScriptEngine::LoadGuiCommands() { return guiManager_->LoadGuiCommands(); } diff --git a/src/script/script_engine.hpp b/src/script/script_engine.hpp index 1428801..4616f78 100644 --- a/src/script/script_engine.hpp +++ b/src/script/script_engine.hpp @@ -12,7 +12,6 @@ #include "core/vertex.hpp" #include "script/gui_types.hpp" -#include "script/physics_bridge.hpp" #include "script/scene_manager.hpp" #include "script/shader_manager.hpp" #include "script/gui_manager.hpp" @@ -38,12 +37,9 @@ public: void UpdateGuiInput(const GuiInputSnapshot& input); bool HasGuiCommands() const; std::filesystem::path GetScriptDirectory() const; - PhysicsBridge& GetPhysicsBridge(); std::string GetLuaError(); private: - std::filesystem::path ResolveScriptPath(const std::string& requested) const; - static std::vector ReadVertexArray(lua_State* L, int index); static std::vector ReadIndexArray(lua_State* L, int index); static std::string LuaErrorMessage(lua_State* L); @@ -56,7 +52,6 @@ private: int guiCommandsFnRef_ = LUA_REFNIL; std::filesystem::path scriptDirectory_; bool debugEnabled_ = false; - std::unique_ptr physicsBridge_; std::unique_ptr sceneManager_; std::unique_ptr shaderManager_; std::unique_ptr guiManager_; diff --git a/src/services/impl/audio_command_service.cpp b/src/services/impl/audio_command_service.cpp index 0721e9d..b47e309 100644 --- a/src/services/impl/audio_command_service.cpp +++ b/src/services/impl/audio_command_service.cpp @@ -1,5 +1,6 @@ #include "audio_command_service.hpp" #include +#include #include namespace sdl3cpp::services::impl { diff --git a/src/services/impl/lua_script_service.cpp b/src/services/impl/lua_script_service.cpp index 36d19f2..a267512 100644 --- a/src/services/impl/lua_script_service.cpp +++ b/src/services/impl/lua_script_service.cpp @@ -102,14 +102,6 @@ bool LuaScriptService::HasGuiCommands() const { return engineService_->GetEngine().HasGuiCommands(); } -script::PhysicsBridge& LuaScriptService::GetPhysicsBridge() { - if (!engineService_ || !engineService_->IsInitialized()) { - throw std::runtime_error("Script service not initialized"); - } - - return engineService_->GetEngine().GetPhysicsBridge(); -} - std::filesystem::path LuaScriptService::GetScriptDirectory() const { if (!engineService_ || !engineService_->IsInitialized()) { return {}; diff --git a/src/services/impl/lua_script_service.hpp b/src/services/impl/lua_script_service.hpp index a1ea194..55c9e56 100644 --- a/src/services/impl/lua_script_service.hpp +++ b/src/services/impl/lua_script_service.hpp @@ -37,8 +37,6 @@ public: void UpdateGuiInput(const script::GuiInputSnapshot& input) override; bool HasGuiCommands() const override; - script::PhysicsBridge& GetPhysicsBridge() override; - std::filesystem::path GetScriptDirectory() const override; std::string GetLuaError() override; diff --git a/src/services/impl/script_engine_service.cpp b/src/services/impl/script_engine_service.cpp index 649fc09..7e9dd97 100644 --- a/src/services/impl/script_engine_service.cpp +++ b/src/services/impl/script_engine_service.cpp @@ -51,6 +51,7 @@ void ScriptEngineService::Shutdown() noexcept { logger_->TraceFunction(__func__); engine_.reset(); + bindingContext_.reset(); initialized_ = false; logger_->Info("Script engine service shutdown"); diff --git a/src/services/interfaces/i_script_service.hpp b/src/services/interfaces/i_script_service.hpp index 0d73dea..c6c9220 100644 --- a/src/services/interfaces/i_script_service.hpp +++ b/src/services/interfaces/i_script_service.hpp @@ -3,7 +3,6 @@ #include "../../script/scene_manager.hpp" #include "../../script/shader_manager.hpp" #include "../../script/gui_types.hpp" -#include "../../script/physics_bridge.hpp" #include #include #include @@ -15,7 +14,7 @@ namespace sdl3cpp::services { /** * @brief Script service interface. * - * Provides Lua script execution and integration with scene, shaders, GUI, physics, and audio. + * Provides Lua script execution and integration with scene, shaders, and GUI. */ class IScriptService { public: @@ -34,9 +33,6 @@ public: virtual void UpdateGuiInput(const script::GuiInputSnapshot& input) = 0; virtual bool HasGuiCommands() const = 0; - // Physics bridge access - virtual script::PhysicsBridge& GetPhysicsBridge() = 0; - // Utility virtual std::filesystem::path GetScriptDirectory() const = 0; virtual std::string GetLuaError() = 0;