From 510b577f49c4dda9f5d20b12b3888b1295847dee Mon Sep 17 00:00:00 2001 From: WiggleWizard <1405402+WiggleWizard@users.noreply.github.com> Date: Wed, 18 Sep 2024 12:44:46 +0100 Subject: [PATCH 1/3] First pass scriptable subsystems that has the same lifetime of the engine --- Nuake/Engine.cpp | 57 ++++++++++++++++++- Nuake/Engine.h | 15 +++++ .../src/Scripting/NetModules/EngineNetAPI.cpp | 25 ++++++-- Nuake/src/Scripting/ScriptingEngineNet.cpp | 14 +++++ Nuake/src/Scripting/ScriptingEngineNet.h | 11 +++- Nuake/src/Subsystems/EngineSubsystem.cpp | 1 + Nuake/src/Subsystems/EngineSubsystem.h | 14 +++++ .../src/Subsystems/EngineSubsystemScript.cpp | 18 ++++++ Nuake/src/Subsystems/EngineSubsystemScript.h | 21 +++++++ Nuake/src/Subsystems/SceneSubsystem.cpp | 1 + Nuake/src/Subsystems/SceneSubsystem.h | 9 +++ .../Subsystems/TickableEngineSubsystem.cpp | 1 + .../src/Subsystems/TickableEngineSubsystem.h | 13 +++++ NuakeNet/src/EngineSubsystem.cs | 6 ++ NuakeNet/src/EngineTickableSubsystem.cs | 6 ++ NuakeNet/src/main.cs | 11 ++++ premake5.lua | 2 + 17 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 Nuake/src/Subsystems/EngineSubsystem.cpp create mode 100644 Nuake/src/Subsystems/EngineSubsystem.h create mode 100644 Nuake/src/Subsystems/EngineSubsystemScript.cpp create mode 100644 Nuake/src/Subsystems/EngineSubsystemScript.h create mode 100644 Nuake/src/Subsystems/SceneSubsystem.cpp create mode 100644 Nuake/src/Subsystems/SceneSubsystem.h create mode 100644 Nuake/src/Subsystems/TickableEngineSubsystem.cpp create mode 100644 Nuake/src/Subsystems/TickableEngineSubsystem.h create mode 100644 NuakeNet/src/EngineSubsystem.cs create mode 100644 NuakeNet/src/EngineTickableSubsystem.cs diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 041dae87..7c07d71f 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -20,6 +20,8 @@ #include #include +#include "src/Subsystems/EngineSubsystemScript.h" +#include "src/Subsystems/TickableEngineSubsystem.h" namespace Nuake @@ -39,6 +41,8 @@ namespace Nuake void Engine::Init() { + ScriptingEngineNet::Get().AddListener(&Engine::OnScriptingEngineGameAssemblyLoaded); + AudioManager::Get().Initialize(); PhysicsManager::Get().Init(); NavManager::Get().Initialize(); @@ -53,6 +57,8 @@ namespace Nuake RegisterCoreTypes::RegisterCoreComponents(); Modules::StartupModules(); + + InitializeCoreSubsystems(); } void Engine::Tick() @@ -92,6 +98,15 @@ namespace Nuake } } + // Tick all subsystems + for (auto subsystem : tickableSubsystems) + { + if (subsystem != nullptr) + { + subsystem->Tick(); + } + } + // Dont update if no scene is loaded. if (currentWindow->GetScene()) { @@ -216,6 +231,46 @@ namespace Nuake return currentProject; } + Ref Engine::GetScriptedSubsystem(const std::string& subsystemName) + { + if (scriptedSubsystemMap.contains(subsystemName)) + { + return scriptedSubsystemMap[subsystemName]; + } + return nullptr; + } + + void Engine::InitializeCoreSubsystems() + { + } + + void Engine::OnScriptingEngineGameAssemblyLoaded() + { + subsystems.clear(); + scriptedSubsystemMap.clear(); + + auto& gameAssembly = ScriptingEngineNet::Get().GetGameAssembly(); + + auto scriptTypeEngineSubsystem = gameAssembly.GetType("Nuake.Net.EngineSubsystem"); + + const auto& types = gameAssembly.GetTypes(); + for (const auto& type : types) + { + // Initialize all subsystems + if (type->IsSubclassOf(scriptTypeEngineSubsystem)) + { + const std::string typeName = std::string(type->GetFullName()); + Logger::Log("Creating Scripted Subsystem " + typeName); + + Coral::ManagedObject scriptedSubsystem = type->CreateInstance(); + Ref subsystemScript = CreateRef(scriptedSubsystem); + subsystems.push_back(subsystemScript); + + scriptedSubsystemMap[typeName] = subsystemScript; + } + } + } + bool Engine::LoadProject(Ref project) { currentProject = project; @@ -236,4 +291,4 @@ namespace Nuake { return currentWindow; } -} \ No newline at end of file +} diff --git a/Nuake/Engine.h b/Nuake/Engine.h index 07bb1969..83072cfa 100644 --- a/Nuake/Engine.h +++ b/Nuake/Engine.h @@ -1,4 +1,5 @@ #pragma once + #include "src/Core/Core.h" #include "src/Core/Logger.h" #include "src/Window.h" @@ -8,6 +9,9 @@ namespace Nuake { class Project; class Scene; + class EngineSubsystem; + class TickableEngineSubsystem; + class EngineSubsystemScript; enum GameState { @@ -50,12 +54,23 @@ namespace Nuake static bool LoadProject(Ref project); static Ref GetProject(); + static Ref GetScriptedSubsystem(const std::string& subsystemName); + + protected: + static void InitializeCoreSubsystems(); + static void OnScriptingEngineGameAssemblyLoaded(); + private: static Ref currentWindow; static Ref currentProject; static Ref currentScene; static std::string queuedScene; + static inline std::vector> subsystems; + static inline std::vector> tickableSubsystems; + + static inline std::unordered_map> scriptedSubsystemMap; + static GameState gameState; static float lastFrameTime; diff --git a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp index 3c9a13d3..9569aed6 100644 --- a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp @@ -1,10 +1,15 @@ #include "EngineNetAPI.h" -#include -#include -#include + +#include "src/Core/Maths.h" #include "src/Rendering/SceneRenderer.h" +#include "Engine.h" +#include "src/Physics/PhysicsManager.h" + +#include +#include #include -#include +#include "Coral/Type.hpp" +#include "src/Subsystems/EngineSubsystemScript.h" namespace Nuake { @@ -112,10 +117,22 @@ namespace Nuake { Engine::QueueSceneSwitch(std::string(path)); } + Coral::ManagedObject GetEngineSubsystemByName(Coral::String subsystemName) + { + const Ref scriptedSubsystem = Engine::GetScriptedSubsystem(subsystemName); + if (scriptedSubsystem == nullptr) + { + return {}; + } + + return scriptedSubsystem->GetManagedObjectInstance(); + } + void EngineNetAPI::RegisterMethods() { RegisterMethod("Engine.LoadSceneIcall", &LoadScene); RegisterMethod("Engine.LoggerLogIcall", (void*)(&Log)); + RegisterMethod("Engine.GetSubsystemByNameIcall", &GetEngineSubsystemByName); // Debug renderer RegisterMethod("Debug.DrawLineIcall", &DrawLine); diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index 04c32f3b..080665a1 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -324,6 +324,15 @@ namespace Nuake return widgetUUIDToManagedObjects[std::make_pair(canvasUUID, uuid)]; } + template + void ScriptingEngineNet::AddListener(const T& delegate) {} + + template <> + void ScriptingEngineNet::AddListener(const GameAssemblyLoadedDelegate& delegate) + { + listenersGameAssemblyLoaded.push_back(delegate); + } + std::vector ScriptingEngineNet::BuildProjectAssembly(Ref project) { const std::string sanitizedProjectName = String::Sanitize(project->Name); @@ -525,6 +534,11 @@ namespace Nuake } } } + + for (auto& delegate : listenersGameAssemblyLoaded) + { + delegate(); + } } } diff --git a/Nuake/src/Scripting/ScriptingEngineNet.h b/Nuake/src/Scripting/ScriptingEngineNet.h index 2eb7a596..fc347104 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.h +++ b/Nuake/src/Scripting/ScriptingEngineNet.h @@ -73,6 +73,9 @@ namespace Nuake class ScriptingEngineNet { + public: + using GameAssemblyLoadedDelegate = std::function; + public: static ScriptingEngineNet& Get(); @@ -83,6 +86,7 @@ namespace Nuake Coral::HostInstance* GetHostInstance() { return hostInstance; } Coral::AssemblyLoadContext& GetLoadContext() { return loadContext; } Coral::ManagedAssembly GetNuakeAssembly() const { return nuakeAssembly; } + Coral::ManagedAssembly& GetGameAssembly() { return gameAssembly; } Coral::ManagedAssembly ReloadEngineAPI(Coral::AssemblyLoadContext & context); @@ -110,6 +114,9 @@ namespace Nuake std::unordered_map GetPointEntities() const { return pointEntityTypes; } std::unordered_map GetUIWidgets() const { return uiWidgets; } + template void AddListener(const T& delegate); + template<> void AddListener(const GameAssemblyLoadedDelegate& delegate); + private: const std::string m_Scope = "Nuake.Net"; const std::string m_EngineAssemblyName = "NuakeNet.dll"; @@ -140,6 +147,8 @@ namespace Nuake std::unordered_map entityToManagedObjects; std::map, Coral::ManagedObject> widgetUUIDToManagedObjects; + + std::vector listenersGameAssemblyLoaded; ScriptingEngineNet(); ~ScriptingEngineNet(); @@ -148,4 +157,4 @@ namespace Nuake std::string GenerateGUID(); std::vector ExtractErrors(const std::string& input); }; -} \ No newline at end of file +} diff --git a/Nuake/src/Subsystems/EngineSubsystem.cpp b/Nuake/src/Subsystems/EngineSubsystem.cpp new file mode 100644 index 00000000..08b50654 --- /dev/null +++ b/Nuake/src/Subsystems/EngineSubsystem.cpp @@ -0,0 +1 @@ +#include "EngineSubsystem.h" diff --git a/Nuake/src/Subsystems/EngineSubsystem.h b/Nuake/src/Subsystems/EngineSubsystem.h new file mode 100644 index 00000000..4b53a1ab --- /dev/null +++ b/Nuake/src/Subsystems/EngineSubsystem.h @@ -0,0 +1,14 @@ +#pragma once + +/** + * Specific type of subsystem that runs within the context of the engine, being created at the start of the + * engine's lifetime and destroyed just before the engine shuts down. + */ +namespace Nuake +{ + class EngineSubsystem + { + public: + + }; +} diff --git a/Nuake/src/Subsystems/EngineSubsystemScript.cpp b/Nuake/src/Subsystems/EngineSubsystemScript.cpp new file mode 100644 index 00000000..6074293d --- /dev/null +++ b/Nuake/src/Subsystems/EngineSubsystemScript.cpp @@ -0,0 +1,18 @@ +#include "EngineSubsystemScript.h" + +namespace Nuake +{ + +EngineSubsystemScript::EngineSubsystemScript(const Coral::ManagedObject& object) + : cSharpObjectInstance(object) +{ + +} + +Coral::ManagedObject& EngineSubsystemScript::GetManagedObjectInstance() +{ + return cSharpObjectInstance; +} +} + + diff --git a/Nuake/src/Subsystems/EngineSubsystemScript.h b/Nuake/src/Subsystems/EngineSubsystemScript.h new file mode 100644 index 00000000..ed52970f --- /dev/null +++ b/Nuake/src/Subsystems/EngineSubsystemScript.h @@ -0,0 +1,21 @@ +#pragma once + +#include "EngineSubsystem.h" +#include "Coral/ManagedObject.hpp" + +/** + * Essentially just a wrapper for C# subsystems + */ +namespace Nuake +{ + class EngineSubsystemScript : public EngineSubsystem + { + public: + EngineSubsystemScript(const Coral::ManagedObject& object); + + Coral::ManagedObject& GetManagedObjectInstance(); + + private: + Coral::ManagedObject cSharpObjectInstance; + }; +} diff --git a/Nuake/src/Subsystems/SceneSubsystem.cpp b/Nuake/src/Subsystems/SceneSubsystem.cpp new file mode 100644 index 00000000..a3dfd458 --- /dev/null +++ b/Nuake/src/Subsystems/SceneSubsystem.cpp @@ -0,0 +1 @@ +#include "SceneSubsystem.h" diff --git a/Nuake/src/Subsystems/SceneSubsystem.h b/Nuake/src/Subsystems/SceneSubsystem.h new file mode 100644 index 00000000..24e01684 --- /dev/null +++ b/Nuake/src/Subsystems/SceneSubsystem.h @@ -0,0 +1,9 @@ +#pragma once + +// Currently unused, but it's meant to be the base for all ECS "systems" at some point +// that can be extended with a script. +class SceneSubsystem +{ +public: + +}; diff --git a/Nuake/src/Subsystems/TickableEngineSubsystem.cpp b/Nuake/src/Subsystems/TickableEngineSubsystem.cpp new file mode 100644 index 00000000..32fa1396 --- /dev/null +++ b/Nuake/src/Subsystems/TickableEngineSubsystem.cpp @@ -0,0 +1 @@ +#include "TickableEngineSubsystem.h" diff --git a/Nuake/src/Subsystems/TickableEngineSubsystem.h b/Nuake/src/Subsystems/TickableEngineSubsystem.h new file mode 100644 index 00000000..4482cf41 --- /dev/null +++ b/Nuake/src/Subsystems/TickableEngineSubsystem.h @@ -0,0 +1,13 @@ +#pragma once + +#include "EngineSubsystem.h" + +namespace Nuake +{ + class TickableEngineSubsystem : public EngineSubsystem + { + public: + virtual void Tick() {} + }; +} + diff --git a/NuakeNet/src/EngineSubsystem.cs b/NuakeNet/src/EngineSubsystem.cs new file mode 100644 index 00000000..a1ffaf17 --- /dev/null +++ b/NuakeNet/src/EngineSubsystem.cs @@ -0,0 +1,6 @@ +namespace Nuake.Net; + +public class EngineSubsystem +{ + +} \ No newline at end of file diff --git a/NuakeNet/src/EngineTickableSubsystem.cs b/NuakeNet/src/EngineTickableSubsystem.cs new file mode 100644 index 00000000..6a430c1c --- /dev/null +++ b/NuakeNet/src/EngineTickableSubsystem.cs @@ -0,0 +1,6 @@ +namespace Nuake.Net; + +public class EngineTickableSubsystem +{ + +} \ No newline at end of file diff --git a/NuakeNet/src/main.cs b/NuakeNet/src/main.cs index af203984..733334de 100644 --- a/NuakeNet/src/main.cs +++ b/NuakeNet/src/main.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Numerics; +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -18,6 +19,8 @@ namespace Nuake.Net { internal static unsafe delegate* LoggerLogIcall; internal static unsafe delegate* LoadSceneIcall; + internal static unsafe delegate*> GetSubsystemByNameIcall; + public Engine() { } public static void LoadScene(string path) @@ -33,6 +36,14 @@ namespace Nuake.Net { unsafe { LoggerLogIcall(input); } } + + public static T? GetSubsystem() where T : EngineSubsystem + { + unsafe + { + return (T?)GetSubsystemByNameIcall(typeof(T).FullName); + } + } } public struct AABB diff --git a/premake5.lua b/premake5.lua index 5f0e97b1..28aa39f1 100644 --- a/premake5.lua +++ b/premake5.lua @@ -127,6 +127,8 @@ project "Nuake" "%{prj.name}/src/Threading/**.cpp", "%{prj.name}/src/UI/**.h", "%{prj.name}/src/UI/**.cpp", + "%{prj.name}/src/Subsystems/**.h", + "%{prj.name}/src/Subsystems/**.cpp", "%{prj.name}/src/Vendors/**.h", "%{prj.name}/src/Vendors/**.cpp", From 066b2998ac52dd858c5f91386df16b190985fd11 Mon Sep 17 00:00:00 2001 From: WiggleWizard <1405402+WiggleWizard@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:20:14 +0100 Subject: [PATCH 2/3] Much more stable and simplified subsystem architecture --- Nuake/Engine.cpp | 54 ++++++++++++++----- Nuake/Engine.h | 11 ++-- .../src/Scripting/NetModules/EngineNetAPI.cpp | 4 +- .../NetModules/EngineSubsystemNetAPI.cpp | 37 +++++++++++++ .../NetModules/EngineSubsystemNetAPI.h | 14 +++++ Nuake/src/Scripting/ScriptingEngineNet.cpp | 2 + Nuake/src/Subsystems/EngineSubsystem.cpp | 10 ++++ Nuake/src/Subsystems/EngineSubsystem.h | 7 +++ .../src/Subsystems/EngineSubsystemScript.cpp | 18 ------- .../Subsystems/EngineSubsystemScriptable.cpp | 35 ++++++++++++ ...emScript.h => EngineSubsystemScriptable.h} | 10 ++-- .../Subsystems/TickableEngineSubsystem.cpp | 1 - .../src/Subsystems/TickableEngineSubsystem.h | 13 ----- NuakeNet/src/EngineSubsystem.cs | 27 ++++++++-- NuakeNet/src/EngineTickableSubsystem.cs | 6 --- 15 files changed, 183 insertions(+), 66 deletions(-) create mode 100644 Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.cpp create mode 100644 Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.h delete mode 100644 Nuake/src/Subsystems/EngineSubsystemScript.cpp create mode 100644 Nuake/src/Subsystems/EngineSubsystemScriptable.cpp rename Nuake/src/Subsystems/{EngineSubsystemScript.h => EngineSubsystemScriptable.h} (50%) delete mode 100644 Nuake/src/Subsystems/TickableEngineSubsystem.cpp delete mode 100644 Nuake/src/Subsystems/TickableEngineSubsystem.h delete mode 100644 NuakeNet/src/EngineTickableSubsystem.cs diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 7c07d71f..6465b58e 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -14,14 +14,13 @@ #include "src/Threading/JobSystem.h" #include "src/Core/RegisterCoreTypes.h" #include "src/Modules/Modules.h" +#include "src/Subsystems/EngineSubsystemScriptable.h" #include #include #include #include -#include "src/Subsystems/EngineSubsystemScript.h" -#include "src/Subsystems/TickableEngineSubsystem.h" namespace Nuake @@ -97,20 +96,27 @@ namespace Nuake } } + + float scaledTimeStep = timeStep * timeScale; // Tick all subsystems - for (auto subsystem : tickableSubsystems) + if (Engine::IsPlayMode()) { - if (subsystem != nullptr) + for (auto subsystem : subsystems) { - subsystem->Tick(); + if (subsystem == nullptr) + continue; + + if (subsystem->CanEverTick()) + { + subsystem->Tick(scaledTimeStep); + } } } // Dont update if no scene is loaded. if (currentWindow->GetScene()) { - float scaledTimeStep = timeStep * timeScale; currentWindow->Update(scaledTimeStep); // Play mode update all the entities, Editor does not. @@ -136,12 +142,14 @@ namespace Nuake void Engine::EnterPlayMode() { + SetGameState(GameState::Loading); + lastFrameTime = (float)glfwGetTime(); // Reset timestep timer. // Dont trigger init if already in player mode. - if (GetGameState() == GameState::Playing) + if (GetGameState() == GameState::Playing || GetGameState() == GameState::Loading) { - Logger::Log("Cannot enter play mode if is already in play mode.", "engine", WARNING); + Logger::Log("Cannot enter play mode if is already in play mode or is loading.", "engine", WARNING); return; } @@ -155,6 +163,9 @@ namespace Nuake { Logger::Log("Cannot enter play mode. Scene OnInit failed", "engine", CRITICAL); GetCurrentScene()->OnExit(); + + // Precautionary measure + SetGameState(GameState::Stopped); } } @@ -231,7 +242,7 @@ namespace Nuake return currentProject; } - Ref Engine::GetScriptedSubsystem(const std::string& subsystemName) + Ref Engine::GetScriptedSubsystem(const std::string& subsystemName) { if (scriptedSubsystemMap.contains(subsystemName)) { @@ -240,18 +251,32 @@ namespace Nuake return nullptr; } + Ref Engine::GetScriptedSubsystem(const int subsystemId) + { + if (subsystemId >= subsystems.size()) + { + return nullptr; + } + return std::reinterpret_pointer_cast(subsystems[subsystemId]); + } + void Engine::InitializeCoreSubsystems() { } void Engine::OnScriptingEngineGameAssemblyLoaded() { + if (!Engine::IsPlayMode() && Engine::GetGameState() != GameState::Loading) + { + return; + } + subsystems.clear(); scriptedSubsystemMap.clear(); - - auto& gameAssembly = ScriptingEngineNet::Get().GetGameAssembly(); - auto scriptTypeEngineSubsystem = gameAssembly.GetType("Nuake.Net.EngineSubsystem"); + const Coral::ManagedAssembly& gameAssembly = ScriptingEngineNet::Get().GetGameAssembly(); + + const auto scriptTypeEngineSubsystem = gameAssembly.GetType("Nuake.Net.EngineSubsystem"); const auto& types = gameAssembly.GetTypes(); for (const auto& type : types) @@ -263,10 +288,13 @@ namespace Nuake Logger::Log("Creating Scripted Subsystem " + typeName); Coral::ManagedObject scriptedSubsystem = type->CreateInstance(); - Ref subsystemScript = CreateRef(scriptedSubsystem); + scriptedSubsystem.SetPropertyValue("EngineSubsystemID", subsystems.size()); + Ref subsystemScript = CreateRef(scriptedSubsystem); subsystems.push_back(subsystemScript); scriptedSubsystemMap[typeName] = subsystemScript; + + subsystemScript->Initialize(); } } } diff --git a/Nuake/Engine.h b/Nuake/Engine.h index 83072cfa..d20ca984 100644 --- a/Nuake/Engine.h +++ b/Nuake/Engine.h @@ -10,11 +10,11 @@ namespace Nuake class Project; class Scene; class EngineSubsystem; - class TickableEngineSubsystem; - class EngineSubsystemScript; + class EngineSubsystemScriptable; enum GameState { + Loading, Playing, Paused, Stopped @@ -54,7 +54,8 @@ namespace Nuake static bool LoadProject(Ref project); static Ref GetProject(); - static Ref GetScriptedSubsystem(const std::string& subsystemName); + static Ref GetScriptedSubsystem(const std::string& subsystemName); + static Ref GetScriptedSubsystem(const int subsystemId); protected: static void InitializeCoreSubsystems(); @@ -67,9 +68,7 @@ namespace Nuake static std::string queuedScene; static inline std::vector> subsystems; - static inline std::vector> tickableSubsystems; - - static inline std::unordered_map> scriptedSubsystemMap; + static inline std::unordered_map> scriptedSubsystemMap; static GameState gameState; diff --git a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp index 9569aed6..551fb7eb 100644 --- a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp @@ -9,7 +9,7 @@ #include #include #include "Coral/Type.hpp" -#include "src/Subsystems/EngineSubsystemScript.h" +#include "..\..\Subsystems\EngineSubsystemScriptable.h" namespace Nuake { @@ -119,7 +119,7 @@ namespace Nuake { Coral::ManagedObject GetEngineSubsystemByName(Coral::String subsystemName) { - const Ref scriptedSubsystem = Engine::GetScriptedSubsystem(subsystemName); + const Ref scriptedSubsystem = Engine::GetScriptedSubsystem(subsystemName); if (scriptedSubsystem == nullptr) { return {}; diff --git a/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.cpp b/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.cpp new file mode 100644 index 00000000..eedc766b --- /dev/null +++ b/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.cpp @@ -0,0 +1,37 @@ +#include "EngineSubsystemNetAPI.h" + +#include "Engine.h" +#include "src/Subsystems/EngineSubsystemScriptable.h" + +namespace Nuake +{ + void SetCanTick(int subsystemId, bool tick) + { + auto subsystem = Engine::GetScriptedSubsystem(subsystemId); + if (subsystem == nullptr) + { + Logger::Log("Subsystem isn't a valid scripted subsystem", "EngineSubsystemNetAPI", WARNING); + return; + } + + subsystem->SetCanTick(tick); + } + + bool GetCanTick(int subsystemId) + { + auto subsystem = Engine::GetScriptedSubsystem(subsystemId); + if (subsystem == nullptr) + { + Logger::Log("Subsystem isn't a valid scripted subsystem", "EngineSubsystemNetAPI", WARNING); + return false; + } + + return subsystem->CanEverTick(); + } + + void EngineSubsystemNetAPI::RegisterMethods() + { + RegisterMethod("EngineSubsystem.SetCanTickIcall", &SetCanTick); + RegisterMethod("EngineSubsystem.GetCanTickIcall", &GetCanTick); + } +} diff --git a/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.h b/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.h new file mode 100644 index 00000000..f827118c --- /dev/null +++ b/Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.h @@ -0,0 +1,14 @@ +#pragma once + +#include "NetAPIModule.h" + +namespace Nuake +{ + class EngineSubsystemNetAPI : public Nuake::NetAPIModule + { + public: + virtual const std::string GetModuleName() const override { return "EngineSubsystem"; } + virtual void RegisterMethods() override; + }; +} + diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index 080665a1..6a7d0716 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -8,6 +8,7 @@ #include "src/Scene/Components/NetScriptComponent.h" #include "NetModules/EngineNetAPI.h" +#include "NetModules/EngineSubsystemNetAPI.h" #include "NetModules/InputNetAPI.h" #include "NetModules/SceneNetAPI.h" #include "NetModules/UINetAPI.h" @@ -53,6 +54,7 @@ namespace Nuake modules = { CreateRef(), + CreateRef(), CreateRef(), CreateRef(), CreateRef() diff --git a/Nuake/src/Subsystems/EngineSubsystem.cpp b/Nuake/src/Subsystems/EngineSubsystem.cpp index 08b50654..d3dc51a1 100644 --- a/Nuake/src/Subsystems/EngineSubsystem.cpp +++ b/Nuake/src/Subsystems/EngineSubsystem.cpp @@ -1 +1,11 @@ #include "EngineSubsystem.h" + +void Nuake::EngineSubsystem::SetCanTick(bool canTick) +{ + canEverTick = canTick; +} + +bool Nuake::EngineSubsystem::CanEverTick() const +{ + return canEverTick; +} diff --git a/Nuake/src/Subsystems/EngineSubsystem.h b/Nuake/src/Subsystems/EngineSubsystem.h index 4b53a1ab..9dbc228c 100644 --- a/Nuake/src/Subsystems/EngineSubsystem.h +++ b/Nuake/src/Subsystems/EngineSubsystem.h @@ -9,6 +9,13 @@ namespace Nuake class EngineSubsystem { public: + void SetCanTick(bool canTick); + bool CanEverTick() const; + virtual void Initialize() {} + virtual void Tick(float deltaTime) {} + + private: + bool canEverTick = true; }; } diff --git a/Nuake/src/Subsystems/EngineSubsystemScript.cpp b/Nuake/src/Subsystems/EngineSubsystemScript.cpp deleted file mode 100644 index 6074293d..00000000 --- a/Nuake/src/Subsystems/EngineSubsystemScript.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "EngineSubsystemScript.h" - -namespace Nuake -{ - -EngineSubsystemScript::EngineSubsystemScript(const Coral::ManagedObject& object) - : cSharpObjectInstance(object) -{ - -} - -Coral::ManagedObject& EngineSubsystemScript::GetManagedObjectInstance() -{ - return cSharpObjectInstance; -} -} - - diff --git a/Nuake/src/Subsystems/EngineSubsystemScriptable.cpp b/Nuake/src/Subsystems/EngineSubsystemScriptable.cpp new file mode 100644 index 00000000..4076ae5f --- /dev/null +++ b/Nuake/src/Subsystems/EngineSubsystemScriptable.cpp @@ -0,0 +1,35 @@ +#include "EngineSubsystemScriptable.h" + +namespace Nuake +{ + +EngineSubsystemScriptable::EngineSubsystemScriptable(const Coral::ManagedObject& object) + : cSharpObjectInstance(object) +{ + +} + +Coral::ManagedObject& EngineSubsystemScriptable::GetManagedObjectInstance() +{ + return cSharpObjectInstance; +} + +void EngineSubsystemScriptable::Initialize() +{ + if (!cSharpObjectInstance.IsValid()) + return; + + cSharpObjectInstance.InvokeMethod("Initialize"); +} + +void EngineSubsystemScriptable::Tick(float deltaTime) +{ + if (!cSharpObjectInstance.IsValid()) + return; + + cSharpObjectInstance.InvokeMethod("OnTick", deltaTime); +} + +} + + diff --git a/Nuake/src/Subsystems/EngineSubsystemScript.h b/Nuake/src/Subsystems/EngineSubsystemScriptable.h similarity index 50% rename from Nuake/src/Subsystems/EngineSubsystemScript.h rename to Nuake/src/Subsystems/EngineSubsystemScriptable.h index ed52970f..69acae01 100644 --- a/Nuake/src/Subsystems/EngineSubsystemScript.h +++ b/Nuake/src/Subsystems/EngineSubsystemScriptable.h @@ -1,20 +1,24 @@ #pragma once #include "EngineSubsystem.h" -#include "Coral/ManagedObject.hpp" + +#include /** * Essentially just a wrapper for C# subsystems */ namespace Nuake { - class EngineSubsystemScript : public EngineSubsystem + class EngineSubsystemScriptable : public EngineSubsystem { public: - EngineSubsystemScript(const Coral::ManagedObject& object); + EngineSubsystemScriptable(const Coral::ManagedObject& object); Coral::ManagedObject& GetManagedObjectInstance(); + virtual void Initialize() override; + virtual void Tick(float deltaTime) override; + private: Coral::ManagedObject cSharpObjectInstance; }; diff --git a/Nuake/src/Subsystems/TickableEngineSubsystem.cpp b/Nuake/src/Subsystems/TickableEngineSubsystem.cpp deleted file mode 100644 index 32fa1396..00000000 --- a/Nuake/src/Subsystems/TickableEngineSubsystem.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "TickableEngineSubsystem.h" diff --git a/Nuake/src/Subsystems/TickableEngineSubsystem.h b/Nuake/src/Subsystems/TickableEngineSubsystem.h deleted file mode 100644 index 4482cf41..00000000 --- a/Nuake/src/Subsystems/TickableEngineSubsystem.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "EngineSubsystem.h" - -namespace Nuake -{ - class TickableEngineSubsystem : public EngineSubsystem - { - public: - virtual void Tick() {} - }; -} - diff --git a/NuakeNet/src/EngineSubsystem.cs b/NuakeNet/src/EngineSubsystem.cs index a1ffaf17..dfb2eb67 100644 --- a/NuakeNet/src/EngineSubsystem.cs +++ b/NuakeNet/src/EngineSubsystem.cs @@ -1,6 +1,25 @@ -namespace Nuake.Net; - -public class EngineSubsystem +namespace Nuake.Net { + public class EngineSubsystem + { + internal static unsafe delegate* SetCanTickIcall; + internal static unsafe delegate* GetCanTickIcall; -} \ No newline at end of file + public int EngineSubsystemID { get; protected set; } + + public bool CanTick + { + set + { + unsafe { SetCanTickIcall(EngineSubsystemID, value); } + } + get + { + unsafe { return GetCanTickIcall(EngineSubsystemID); } + } + } + + public virtual void Initialize() {} + public virtual void OnTick(float deltaTime) {} + } +} diff --git a/NuakeNet/src/EngineTickableSubsystem.cs b/NuakeNet/src/EngineTickableSubsystem.cs deleted file mode 100644 index 6a430c1c..00000000 --- a/NuakeNet/src/EngineTickableSubsystem.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Nuake.Net; - -public class EngineTickableSubsystem -{ - -} \ No newline at end of file From ca645e20267b3f48fb76718808cde7d0f11d9484 Mon Sep 17 00:00:00 2001 From: WiggleWizard <1405402+WiggleWizard@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:48:14 +0100 Subject: [PATCH 3/3] Fixed a few things --- Nuake/Engine.cpp | 7 ++----- Nuake/src/Subsystems/EngineSubsystem.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 6465b58e..52e4b895 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -142,8 +142,6 @@ namespace Nuake void Engine::EnterPlayMode() { - SetGameState(GameState::Loading); - lastFrameTime = (float)glfwGetTime(); // Reset timestep timer. // Dont trigger init if already in player mode. @@ -152,6 +150,8 @@ namespace Nuake Logger::Log("Cannot enter play mode if is already in play mode or is loading.", "engine", WARNING); return; } + + SetGameState(GameState::Loading); PhysicsManager::Get().ReInit(); @@ -163,9 +163,6 @@ namespace Nuake { Logger::Log("Cannot enter play mode. Scene OnInit failed", "engine", CRITICAL); GetCurrentScene()->OnExit(); - - // Precautionary measure - SetGameState(GameState::Stopped); } } diff --git a/Nuake/src/Subsystems/EngineSubsystem.h b/Nuake/src/Subsystems/EngineSubsystem.h index 9dbc228c..f1c25b30 100644 --- a/Nuake/src/Subsystems/EngineSubsystem.h +++ b/Nuake/src/Subsystems/EngineSubsystem.h @@ -16,6 +16,6 @@ namespace Nuake virtual void Tick(float deltaTime) {} private: - bool canEverTick = true; + bool canEverTick = false; }; }