mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-15 20:08:54 +03:00
Instancing prefab through code should instance body and scripts immediatly
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "Prefab.h"
|
||||
#include "src/Scene/Components/ParentComponent.h"
|
||||
#include <src/Scene/Components/PrefabComponent.h>
|
||||
#include <src/Scene/Components/NetScriptComponent.h>
|
||||
#include <src/Scene/Components/CharacterControllerComponent.h>
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
|
||||
@@ -52,10 +52,13 @@ namespace Nuake
|
||||
m_EditorCamera = CreateRef<EditorCamera>();
|
||||
m_Environement = CreateRef<Environment>();
|
||||
|
||||
physicsSystem = CreateRef<PhysicsSystem>(this);
|
||||
scriptingSystem = CreateRef<ScriptingSystem>(this);
|
||||
|
||||
// Adding systems - Order is important
|
||||
m_Systems.push_back(CreateRef<PhysicsSystem>(this));
|
||||
m_Systems.push_back(physicsSystem);
|
||||
m_Systems.push_back(CreateRef<UISystem>(this));
|
||||
m_Systems.push_back(CreateRef<ScriptingSystem>(this));
|
||||
m_Systems.push_back(scriptingSystem);
|
||||
m_Systems.push_back(CreateRef<AnimationSystem>(this));
|
||||
m_Systems.push_back(CreateRef<TransformSystem>(this));
|
||||
m_Systems.push_back(CreateRef<ParticleSystem>(this));
|
||||
|
||||
@@ -22,6 +22,9 @@ namespace Nuake
|
||||
DECLARE_MULTICAST_DELEGATE(PreInitializeDelegate)
|
||||
DECLARE_MULTICAST_DELEGATE(PostInitializeDelegate)
|
||||
|
||||
class PhysicsSystem;
|
||||
class ScriptingSystem;
|
||||
|
||||
class Scene : public ISerializable
|
||||
{
|
||||
friend Entity;
|
||||
@@ -43,6 +46,9 @@ namespace Nuake
|
||||
std::unordered_map<std::string, Entity> m_EntitiesNameMap;
|
||||
std::string Path = "";
|
||||
|
||||
Ref<PhysicsSystem> physicsSystem;
|
||||
Ref<ScriptingSystem> scriptingSystem;
|
||||
|
||||
Ref<SceneRenderer> m_SceneRenderer;
|
||||
|
||||
static Ref<Scene> New();
|
||||
@@ -98,6 +104,9 @@ namespace Nuake
|
||||
PreInitializeDelegate& OnPreInitialize() { return preInitializeDelegate; }
|
||||
PostInitializeDelegate& OnPostInitialize() { return postInitializeDelegate; }
|
||||
|
||||
Ref<PhysicsSystem> GetPhysicsSystem() const { return physicsSystem; }
|
||||
Ref<ScriptingSystem> GetScriptingSystem() const { return scriptingSystem; }
|
||||
|
||||
protected:
|
||||
PreInitializeDelegate preInitializeDelegate;
|
||||
PostInitializeDelegate postInitializeDelegate;
|
||||
|
||||
@@ -109,6 +109,13 @@ namespace Nuake
|
||||
PhysicsManager::Get().Reset();
|
||||
}
|
||||
|
||||
void PhysicsSystem::InitializeNewBodies()
|
||||
{
|
||||
InitializeShapes();
|
||||
InitializeRigidbodies();
|
||||
InitializeCharacterControllers();
|
||||
}
|
||||
|
||||
void PhysicsSystem::InitializeQuakeMap()
|
||||
{
|
||||
auto quakeBrushesView = m_Scene->m_Registry.view<TransformComponent, BSPBrushComponent>();
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Nuake
|
||||
void FixedUpdate(Timestep ts) override;
|
||||
void Exit() override;
|
||||
|
||||
void InitializeNewBodies();
|
||||
private:
|
||||
void InitializeShapes();
|
||||
void InitializeQuakeMap();
|
||||
|
||||
@@ -82,41 +82,10 @@ namespace Nuake
|
||||
if (!Engine::IsPlayMode())
|
||||
return;
|
||||
|
||||
InitializeNewScripts();
|
||||
|
||||
auto& scriptingEngineNet = ScriptingEngineNet::Get();
|
||||
auto netEntities = m_Scene->m_Registry.view<NetScriptComponent>();
|
||||
|
||||
// Instance all the new entities
|
||||
std::vector<Entity> entityJustInstanced;
|
||||
for (auto& e : netEntities)
|
||||
{
|
||||
auto& netScriptComponent = netEntities.get<NetScriptComponent>(e);
|
||||
if (!netScriptComponent.Initialized)
|
||||
{
|
||||
if (netScriptComponent.ScriptPath.empty())
|
||||
continue;
|
||||
|
||||
auto entity = Entity{ e, m_Scene };
|
||||
|
||||
// Creates an instance of the entity script in C#
|
||||
scriptingEngineNet.RegisterEntityScript(entity);
|
||||
|
||||
netScriptComponent.Initialized = true;
|
||||
entityJustInstanced.push_back(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// Call init on the newly created instance
|
||||
for (auto& e : entityJustInstanced)
|
||||
{
|
||||
auto& netScriptComponent = e.GetComponent<NetScriptComponent>();
|
||||
if (e.IsValid() && scriptingEngineNet.HasEntityScriptInstance(e))
|
||||
{
|
||||
// We can now call on init on it.
|
||||
auto scriptInstance = scriptingEngineNet.GetEntityScript(e);
|
||||
scriptInstance.InvokeMethod("OnInit");
|
||||
}
|
||||
}
|
||||
|
||||
// Then call update on all the other scripts
|
||||
for (auto& e : netEntities)
|
||||
{
|
||||
@@ -187,6 +156,44 @@ namespace Nuake
|
||||
ScriptingEngineNet::Get().Uninitialize();
|
||||
}
|
||||
|
||||
void ScriptingSystem::InitializeNewScripts()
|
||||
{
|
||||
auto& scriptingEngineNet = ScriptingEngineNet::Get();
|
||||
auto netEntities = m_Scene->m_Registry.view<NetScriptComponent>();
|
||||
|
||||
// Instance all the new entities
|
||||
std::vector<Entity> entityJustInstanced;
|
||||
for (auto& e : netEntities)
|
||||
{
|
||||
auto& netScriptComponent = netEntities.get<NetScriptComponent>(e);
|
||||
if (!netScriptComponent.Initialized)
|
||||
{
|
||||
if (netScriptComponent.ScriptPath.empty())
|
||||
continue;
|
||||
|
||||
auto entity = Entity{ e, m_Scene };
|
||||
|
||||
// Creates an instance of the entity script in C#
|
||||
scriptingEngineNet.RegisterEntityScript(entity);
|
||||
|
||||
netScriptComponent.Initialized = true;
|
||||
entityJustInstanced.push_back(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// Call init on the newly created instance
|
||||
for (auto& e : entityJustInstanced)
|
||||
{
|
||||
auto& netScriptComponent = e.GetComponent<NetScriptComponent>();
|
||||
if (e.IsValid() && scriptingEngineNet.HasEntityScriptInstance(e))
|
||||
{
|
||||
// We can now call on init on it.
|
||||
auto scriptInstance = scriptingEngineNet.GetEntityScript(e);
|
||||
scriptInstance.InvokeMethod("OnInit");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptingSystem::DispatchPhysicCallbacks()
|
||||
{
|
||||
auto& scriptingEngineNet = ScriptingEngineNet::Get();
|
||||
|
||||
@@ -15,6 +15,8 @@ namespace Nuake {
|
||||
void FixedUpdate(Timestep ts) override;
|
||||
void Exit() override;
|
||||
|
||||
void InitializeNewScripts();
|
||||
|
||||
private:
|
||||
void DispatchPhysicCallbacks();
|
||||
};
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
#include "src/Scene/Components/QuakeMap.h"
|
||||
|
||||
#include "src/Physics/PhysicsManager.h"
|
||||
#include "src/Scene/Systems/PhysicsSystem.h"
|
||||
#include "src/Scene/Systems/ScriptingSystem.h"
|
||||
#include "src/Scripting/ScriptingEngineNet.h"
|
||||
|
||||
#include <Coral/Array.hpp>
|
||||
@@ -293,6 +295,10 @@ namespace Nuake {
|
||||
transformComponent.SetLocalPosition(position);
|
||||
transformComponent.SetLocalRotation(Quat(qw, qx, qy, qz));
|
||||
|
||||
Ref<Scene> scene = Engine::GetCurrentScene();
|
||||
scene->GetPhysicsSystem()->InitializeNewBodies();
|
||||
scene->GetScriptingSystem()->InitializeNewScripts();
|
||||
|
||||
return root.GetHandle();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user