Skip to content
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
7 changes: 7 additions & 0 deletions Resources/Engine/Lua/Resources/Prefab.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---@meta

--- Represents a Prefab asset reference
---@class Prefab
---@field path string
---@overload fun(): Prefab
Prefab = {}
6 changes: 6 additions & 0 deletions Resources/Engine/Lua/Scene/Scene.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ function Scene:FindActorsByTag(tag) end
---@overload fun(self: Scene, name: string, tag: string): Actor
---@return Actor
function Scene:CreateActor(...) end

--- Instantiates a prefab in the scene
---@overload fun(self: Scene, prefab: Prefab): Actor|nil
---@overload fun(self: Scene, prefab: Prefab, parent: Actor): Actor|nil
---@return Actor|nil
function Scene:InstantiatePrefab(...) end
4 changes: 2 additions & 2 deletions Sources/OvCore/include/OvCore/ECS/Actor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OvCore::ECS
m_components.push_back(std::make_shared<T>(*this, p_args...));
T& instance = *dynamic_cast<T*>(m_components.back().get());
ComponentAddedEvent.Invoke(instance);
if (m_playing && IsActive())
if (m_playing && !m_sleeping && IsActive())
{
reinterpret_cast<OvCore::ECS::Components::AComponent&>(instance).OnAwake();
reinterpret_cast<OvCore::ECS::Components::AComponent&>(instance).OnEnable();
Expand Down Expand Up @@ -74,4 +74,4 @@ namespace OvCore::ECS

return nullptr;
}
}
}
11 changes: 11 additions & 0 deletions Sources/OvCore/include/OvCore/SceneSystem/PrefabOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <filesystem>
#include <functional>
#include <string>

namespace OvCore::ECS
{
Expand All @@ -34,6 +35,16 @@ namespace OvCore::SceneSystem
*/
static bool SaveToFile(OvCore::ECS::Actor& p_rootActor, const std::filesystem::path& p_outputPath);

/**
* Set the root prefab source and remove redundant inherited sources from children.
* @param p_rootActor
* @param p_newRootPrefabSource
*/
static void SetRootPrefabSourceAndNormalizeChildren(
OvCore::ECS::Actor& p_rootActor,
const std::string& p_newRootPrefabSource
);

/**
* Instantiate a prefab file using the provided actor factory.
* @param p_prefabPath
Expand Down
34 changes: 32 additions & 2 deletions Sources/OvCore/include/OvCore/SceneSystem/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#pragma once

#include <filesystem>
#include <functional>
#include <string>
#include <vector>

#include <OvCore/API/ISerializable.h>
#include <OvCore/ECS/Actor.h>
Expand All @@ -14,6 +18,7 @@
#include <OvCore/ECS/Components/CModelRenderer.h>
#include <OvCore/ECS/Components/CPostProcessStack.h>
#include <OvCore/ECS/Components/CReflectionProbe.h>
#include <OvTools/Utils/OptRef.h>

namespace OvCore::SceneSystem
{
Expand All @@ -39,7 +44,10 @@ namespace OvCore::SceneSystem
/**
* Constructor of the scene
*/
Scene();
Scene(
const std::filesystem::path& p_projectAssetsPath = "",
const std::filesystem::path& p_engineAssetsPath = ""
);

/**
* Handle the memory de-allocation of every actors
Expand Down Expand Up @@ -116,6 +124,19 @@ namespace OvCore::SceneSystem
*/
ECS::Actor& CreateActor(const std::string& p_name, const std::string& p_tag = "");

/**
* Instantiate a prefab and return its root actor.
* @param p_prefabPath
*/
ECS::Actor* InstantiatePrefab(const std::string& p_prefabPath);

/**
* Instantiate a prefab under the given parent and return its root actor.
* @param p_prefabPath
* @param p_parent
*/
ECS::Actor* InstantiatePrefab(const std::string& p_prefabPath, ECS::Actor& p_parent);

/**
* Destroy and actor and return true on success
* @param p_target (The actor to remove from the scene)
Expand Down Expand Up @@ -205,10 +226,19 @@ namespace OvCore::SceneSystem
virtual void OnDeserialize(tinyxml2::XMLDocument& p_doc, tinyxml2::XMLNode* p_root) override;

private:
std::filesystem::path GetRealAssetPath(const std::string& p_path) const;
void BeginBatchActorCreation();
void EndBatchActorCreation(bool p_startCreatedActors);
ECS::Actor* InstantiatePrefabInternal(const std::string& p_prefabPath, OvTools::Utils::OptRef<ECS::Actor> p_parent);

int64_t m_availableID = 1;
bool m_isPlaying = false;
bool m_batchActorCreation = false;
std::vector<ECS::Actor*> m_actors;
std::vector<std::reference_wrapper<ECS::Actor>> m_batchCreatedActors;

FastAccessComponents m_fastAccessComponents;
std::filesystem::path m_projectAssetsPath;
std::filesystem::path m_engineAssetsPath;
};
}
}
15 changes: 11 additions & 4 deletions Sources/OvCore/include/OvCore/SceneSystem/SceneManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#pragma once


#include <functional>
#include <memory>
#include <string>

#include "OvCore/SceneSystem/Scene.h"

Expand All @@ -19,11 +21,15 @@ namespace OvCore::SceneSystem
{
public:

/**
/**
* Default constructor
* @param p_sceneRootFolder (Optional)
* @param p_engineAssetsFolder (Optional)
*/
SceneManager(const std::string& p_sceneRootFolder = "");
SceneManager(
const std::string& p_sceneRootFolder = "",
const std::string& p_engineAssetsFolder = ""
);

/**
* Default destructor
Expand Down Expand Up @@ -108,11 +114,12 @@ namespace OvCore::SceneSystem

private:
const std::string m_sceneRootFolder;
const std::string m_engineAssetsFolder;
std::unique_ptr<Scene> m_currentScene = nullptr;

bool m_currentSceneLoadedFromPath = false;
std::string m_currentSceneSourcePath = "";

std::function<void()> m_delayedLoadCall;
};
}
}
4 changes: 2 additions & 2 deletions Sources/OvCore/src/OvCore/ECS/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ OvCore::ECS::Components::Behaviour & OvCore::ECS::Actor::AddBehaviour(const std:
m_behavioursOrder.push_back(p_name);
Components::Behaviour& newInstance = m_behaviours.at(p_name);
BehaviourAddedEvent.Invoke(newInstance);
if (m_playing && IsActive())
if (m_playing && !m_sleeping && IsActive())
{
newInstance.OnAwake();
newInstance.OnEnable();
Expand Down Expand Up @@ -475,7 +475,7 @@ bool OvCore::ECS::Actor::RenameBehaviour(const std::string& p_previousName, cons
m_behaviours.try_emplace(p_newName, *this, p_newName);
Components::Behaviour& newInstance = m_behaviours.at(p_newName);
BehaviourAddedEvent.Invoke(newInstance);
if (m_playing && IsActive())
if (m_playing && !m_sleeping && IsActive())
{
newInstance.OnAwake();
newInstance.OnEnable();
Expand Down
34 changes: 34 additions & 0 deletions Sources/OvCore/src/OvCore/SceneSystem/PrefabOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@

namespace
{
void NormalizePrefabSourcesRecursively(
OvCore::ECS::Actor& p_actor,
const std::string& p_inheritedPrefabSource)
{
if (p_actor.HasPrefabSource())
{
const std::string currentPrefabSource = p_actor.GetPrefabSource();
if (currentPrefabSource == p_inheritedPrefabSource)
{
p_actor.SetPrefabSource("?");
}
}

const std::string nextInheritedPrefabSource =
p_actor.HasPrefabSource() ? p_actor.GetPrefabSource() : p_inheritedPrefabSource;

for (auto* child : p_actor.GetChildren())
{
NormalizePrefabSourcesRecursively(*child, nextInheritedPrefabSource);
}
}

void SerializeActorHierarchy(
OvCore::ECS::Actor& p_actor,
tinyxml2::XMLDocument& p_doc,
Expand Down Expand Up @@ -54,6 +76,18 @@ bool OvCore::SceneSystem::PrefabOperations::SaveToFile(OvCore::ECS::Actor& p_roo
return doc.SaveFile(p_outputPath.string().c_str()) == tinyxml2::XML_SUCCESS;
}

void OvCore::SceneSystem::PrefabOperations::SetRootPrefabSourceAndNormalizeChildren(
OvCore::ECS::Actor& p_rootActor,
const std::string& p_newRootPrefabSource)
{
p_rootActor.SetPrefabSource(p_newRootPrefabSource);

for (auto* child : p_rootActor.GetChildren())
{
NormalizePrefabSourcesRecursively(*child, p_newRootPrefabSource);
}
}

OvCore::ECS::Actor* OvCore::SceneSystem::PrefabOperations::InstantiateFromFile(
const std::filesystem::path& p_prefabPath,
const std::function<OvCore::ECS::Actor&(void)>& p_createActor)
Expand Down
Loading
Loading