Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass scriptable subsystems that has the same lifetime of the engine #90

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 84 additions & 4 deletions Nuake/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "src/Threading/JobSystem.h"
#include "src/Core/RegisterCoreTypes.h"
#include "src/Modules/Modules.h"
#include "src/Subsystems/EngineSubsystemScriptable.h"

#include <GLFW/glfw3.h>
#include <imgui/imgui_impl_glfw.h>
Expand All @@ -39,6 +40,8 @@ namespace Nuake

void Engine::Init()
{
ScriptingEngineNet::Get().AddListener<ScriptingEngineNet::GameAssemblyLoadedDelegate>(&Engine::OnScriptingEngineGameAssemblyLoaded);

AudioManager::Get().Initialize();
PhysicsManager::Get().Init();
NavManager::Get().Initialize();
Expand All @@ -53,6 +56,8 @@ namespace Nuake
RegisterCoreTypes::RegisterCoreComponents();

Modules::StartupModules();

InitializeCoreSubsystems();
}

void Engine::Tick()
Expand Down Expand Up @@ -91,11 +96,27 @@ namespace Nuake

}
}

float scaledTimeStep = timeStep * timeScale;

// Tick all subsystems
if (Engine::IsPlayMode())
{
for (auto subsystem : subsystems)
{
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.
Expand Down Expand Up @@ -124,11 +145,13 @@ namespace Nuake
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;
}

SetGameState(GameState::Loading);

PhysicsManager::Get().ReInit();

Expand Down Expand Up @@ -216,6 +239,63 @@ namespace Nuake
return currentProject;
}

Ref<EngineSubsystemScriptable> Engine::GetScriptedSubsystem(const std::string& subsystemName)
{
if (scriptedSubsystemMap.contains(subsystemName))
{
return scriptedSubsystemMap[subsystemName];
}
return nullptr;
}

Ref<EngineSubsystemScriptable> Engine::GetScriptedSubsystem(const int subsystemId)
{
if (subsystemId >= subsystems.size())
{
return nullptr;
}
return std::reinterpret_pointer_cast<EngineSubsystemScriptable>(subsystems[subsystemId]);
}

void Engine::InitializeCoreSubsystems()
{
}

void Engine::OnScriptingEngineGameAssemblyLoaded()
{
if (!Engine::IsPlayMode() && Engine::GetGameState() != GameState::Loading)
{
return;
}

subsystems.clear();
scriptedSubsystemMap.clear();

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)
{
// 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();
scriptedSubsystem.SetPropertyValue("EngineSubsystemID", subsystems.size());
Ref<EngineSubsystemScriptable> subsystemScript = CreateRef<EngineSubsystemScriptable>(scriptedSubsystem);
subsystems.push_back(subsystemScript);

scriptedSubsystemMap[typeName] = subsystemScript;

subsystemScript->Initialize();
}
}
}

bool Engine::LoadProject(Ref<Project> project)
{
currentProject = project;
Expand All @@ -236,4 +316,4 @@ namespace Nuake
{
return currentWindow;
}
}
}
14 changes: 14 additions & 0 deletions Nuake/Engine.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "src/Core/Core.h"
#include "src/Core/Logger.h"
#include "src/Window.h"
Expand All @@ -8,9 +9,12 @@ namespace Nuake
{
class Project;
class Scene;
class EngineSubsystem;
class EngineSubsystemScriptable;

enum GameState
{
Loading,
Playing,
Paused,
Stopped
Expand Down Expand Up @@ -50,12 +54,22 @@ namespace Nuake
static bool LoadProject(Ref<Project> project);
static Ref<Project> GetProject();

static Ref<EngineSubsystemScriptable> GetScriptedSubsystem(const std::string& subsystemName);
static Ref<EngineSubsystemScriptable> GetScriptedSubsystem(const int subsystemId);

protected:
static void InitializeCoreSubsystems();
static void OnScriptingEngineGameAssemblyLoaded();

private:
static Ref<Window> currentWindow;
static Ref<Project> currentProject;
static Ref<Scene> currentScene;
static std::string queuedScene;

static inline std::vector<Ref<EngineSubsystem>> subsystems;
static inline std::unordered_map<std::string, Ref<EngineSubsystemScriptable>> scriptedSubsystemMap;

static GameState gameState;

static float lastFrameTime;
Expand Down
25 changes: 21 additions & 4 deletions Nuake/src/Scripting/NetModules/EngineNetAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "EngineNetAPI.h"
#include <Coral/String.hpp>
#include <src/Core/Maths.h>
#include <Engine.h>

#include "src/Core/Maths.h"
#include "src/Rendering/SceneRenderer.h"
#include "Engine.h"
#include "src/Physics/PhysicsManager.h"

#include <Coral/String.hpp>
#include <Coral/ManagedObject.hpp>
#include <Coral/Array.hpp>
#include <src/Physics/PhysicsManager.h>
#include "Coral/Type.hpp"
#include "..\..\Subsystems\EngineSubsystemScriptable.h"

namespace Nuake {

Expand Down Expand Up @@ -112,10 +117,22 @@ namespace Nuake {
Engine::QueueSceneSwitch(std::string(path));
}

Coral::ManagedObject GetEngineSubsystemByName(Coral::String subsystemName)
{
const Ref<EngineSubsystemScriptable> 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);
Expand Down
37 changes: 37 additions & 0 deletions Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions Nuake/src/Scripting/NetModules/EngineSubsystemNetAPI.h
Original file line number Diff line number Diff line change
@@ -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;
};
}

16 changes: 16 additions & 0 deletions Nuake/src/Scripting/ScriptingEngineNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -53,6 +54,7 @@ namespace Nuake
modules =
{
CreateRef<EngineNetAPI>(),
CreateRef<EngineSubsystemNetAPI>(),
CreateRef<InputNetAPI>(),
CreateRef<SceneNetAPI>(),
CreateRef<UINetAPI>()
Expand Down Expand Up @@ -324,6 +326,15 @@ namespace Nuake
return widgetUUIDToManagedObjects[std::make_pair(canvasUUID, uuid)];
}

template<class T>
void ScriptingEngineNet::AddListener(const T& delegate) {}

template <>
void ScriptingEngineNet::AddListener<ScriptingEngineNet::GameAssemblyLoadedDelegate>(const GameAssemblyLoadedDelegate& delegate)
{
listenersGameAssemblyLoaded.push_back(delegate);
}

std::vector<CompilationError> ScriptingEngineNet::BuildProjectAssembly(Ref<Project> project)
{
const std::string sanitizedProjectName = String::Sanitize(project->Name);
Expand Down Expand Up @@ -525,6 +536,11 @@ namespace Nuake
}
}
}

for (auto& delegate : listenersGameAssemblyLoaded)
{
delegate();
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion Nuake/src/Scripting/ScriptingEngineNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ namespace Nuake

class ScriptingEngineNet
{
public:
using GameAssemblyLoadedDelegate = std::function<void()>;

public:
static ScriptingEngineNet& Get();

Expand All @@ -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);

Expand Down Expand Up @@ -110,6 +114,9 @@ namespace Nuake
std::unordered_map<std::string, NetGameScriptObject> GetPointEntities() const { return pointEntityTypes; }
std::unordered_map<std::string, UIWidgetObject> GetUIWidgets() const { return uiWidgets; }

template<class T> 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";
Expand Down Expand Up @@ -140,6 +147,8 @@ namespace Nuake

std::unordered_map<uint32_t, Coral::ManagedObject> entityToManagedObjects;
std::map<std::pair<UUID, UUID>, Coral::ManagedObject> widgetUUIDToManagedObjects;

std::vector<GameAssemblyLoadedDelegate> listenersGameAssemblyLoaded;

ScriptingEngineNet();
~ScriptingEngineNet();
Expand All @@ -148,4 +157,4 @@ namespace Nuake
std::string GenerateGUID();
std::vector<CompilationError> ExtractErrors(const std::string& input);
};
}
}
11 changes: 11 additions & 0 deletions Nuake/src/Subsystems/EngineSubsystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "EngineSubsystem.h"

void Nuake::EngineSubsystem::SetCanTick(bool canTick)
{
canEverTick = canTick;
}

bool Nuake::EngineSubsystem::CanEverTick() const
{
return canEverTick;
}
Loading