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

Removed world stack #7

Merged
merged 3 commits into from
Aug 13, 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
10 changes: 7 additions & 3 deletions Include/Assets/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ namespace CE
Level& operator=(const Level&) = delete;

void CreateFromWorld(const World& world);
World CreateWorld(bool callBeginPlayImmediately) const;
void LoadIntoWorld(World& world) const;

static World CreateDefaultWorld();
// Will never return nullptr
std::unique_ptr<World> CreateWorld(bool callBeginPlayImmediately) const;

// Will never return nullptr
static std::unique_ptr<World> CreateDefaultWorld();

protected:
void OnSave(AssetSaveInfo& saveInfo) const override;
Expand Down Expand Up @@ -71,7 +75,7 @@ namespace CE
// when loading the level. Instead of
// discarding the world, we return this
// one on the first call to CreateWorld
mutable std::optional<World> mWorld{};
mutable std::unique_ptr<World> mWorld{};
mutable std::optional<BinaryGSONObject> mSerializedWorld{};

friend ReflectAccess;
Expand Down
5 changes: 3 additions & 2 deletions Include/Assets/Script.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ namespace CE
// so if this ever changes, you'll know exactly where you need to start fixing things.
static constexpr bool sIsTypeIdTheSameAsNameHash = true;

// Each component script secretly gets assigned a field of type entt::entity with this name.
// Upon constructing the component, the correct entity id is assigned.
// Each component script secretly gets assigned a field of type entt::entity and World* with these names.
// Upon constructing the component, the correct entity id and world is assigned.
static constexpr Name sNameOfOwnerField = "Owner"_Name;
static constexpr Name sNameOfWorldField = "World"_Name;

void CollectErrors(ScriptErrorInserter inserter) const;

Expand Down
6 changes: 3 additions & 3 deletions Include/Components/GridSpawnerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace CE
{
public:
void OnConstruct(World&, entt::entity owner);
void OnBeginPlay(World&, entt::entity);
void OnBeginPlay(World& world, entt::entity);

void ClearGrid();
void SpawnGrid();
void ClearGrid(World& world);
void SpawnGrid(World& world);

std::vector<AssetHandle<Prefab>> mTiles{};

Expand Down
6 changes: 2 additions & 4 deletions Include/EditorSystems/ThumbnailEditorSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CE
class Texture;
class World;

using GetThumbnailRet = std::variant<AssetHandle<Texture>, World>;
using GetThumbnailRet = std::variant<AssetHandle<Texture>, std::unique_ptr<World>>;

class ThumbnailEditorSystem final :
public EditorSystem
Expand Down Expand Up @@ -72,9 +72,7 @@ namespace CE

struct CurrentlyGenerating
{
CurrentlyGenerating(World&& world, WeakAssetHandle<> forAsset);

World mWorld;
std::unique_ptr<World> mWorld{};
WeakAssetHandle<> mForAsset{};
FrameBuffer mFrameBuffer{ sGeneratedThumbnailResolution };
Timer mNumSecondsSinceLastRequested{};
Expand Down
9 changes: 1 addition & 8 deletions Include/Scripting/ScriptPin.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#pragma once
#include "ScriptIds.h"
#include "imnodes/imgui_node_editor.h"

#include "Meta/MetaAny.h"
#include "Meta/MetaTypeTraits.h"
#include "Meta/Fwd/MetaFuncFwd.h"
#include "Meta/MetaTypeTraits.h"
#include "Scripting/ScriptErrors.h"

namespace CE
{
struct MetaFuncNamedParam;
}

namespace CE
{
enum class ScriptPinKind : bool
Expand Down
2 changes: 1 addition & 1 deletion Include/Utilities/Imgui/WorldInspect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CE
The provided doUndoStack must remain alive for the duration of this
objects lifetime.
*/
WorldInspectHelper(World&& worldThatHasNotYetBegunPlay);
WorldInspectHelper(std::unique_ptr<World> worldThatHasNotYetBegunPlay);
~WorldInspectHelper();

/*
Expand Down
10 changes: 4 additions & 6 deletions Include/Utilities/Reflect/ReflectComponentType.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ namespace CE
MetaFunc& addComponentFunc = entityType.AddFunc(
[](MetaFunc::DynamicArgs args, MetaFunc::RVOBuffer) -> FuncResult
{
World* world = World::TryGetWorldAtTopOfStack();
ASSERT(world != nullptr && "Reached a scripting context without pushing a world");
World& world = *static_cast<World*>(args[0].GetData());
const entt::entity entity = *static_cast<entt::entity*>(args[1].GetData());

entt::entity entity = *static_cast<entt::entity*>(args[0].GetData());

Registry& reg = world->GetRegistry();
Registry& reg = world.GetRegistry();

if (reg.HasComponent<T>(entity))
{
Expand All @@ -61,7 +59,7 @@ namespace CE
},
Internal::GetAddComponentFuncName(type.GetName()),
MetaFunc::Return{ isEmpty ? MakeTypeTraits<void>() : MakeTypeTraits<T&>() },
MetaFunc::Params{ { MakeTypeTraits<const entt::entity&>(), "Entity" } });
MetaFunc::Params{ { MakeTypeTraits<World&>() }, { MakeTypeTraits<entt::entity>() } });

if (type.GetProperties().Has(Props::sIsScriptableTag))
{
Expand Down
2 changes: 0 additions & 2 deletions Include/World/EventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace CE
MetaFunc::DynamicArgs argsProvided,
std::span<TypeForm> argFormsProvided);

// mWorld needs to be updated in World::World(World&&), so we give access to World to do so.
friend class World;
std::reference_wrapper<World> mWorld;

std::unordered_map<std::string_view, std::vector<BoundEvent>> mBoundEvents{};
Expand Down
2 changes: 0 additions & 2 deletions Include/World/Physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ namespace CE
static MetaType Reflect();
REFLECT_AT_START_UP(Physics);

// mWorld needs to be updated in World::World(World&&), so we give access to World to do so.
friend class World;
std::reference_wrapper<World> mWorld;

BVHS mBVHs;
Expand Down
15 changes: 0 additions & 15 deletions Include/World/Registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ namespace CE

void CallEndPlayEventsForEntity(entt::sparse_set& storage, entt::entity entity, const BoundEvent& endPlayEvent);

// mWorld needs to be updated in World::World(World&&), so we give access to World to do so.
friend World;

std::reference_wrapper<World> mWorld;

entt::registry mRegistry{};
Expand Down Expand Up @@ -217,8 +214,6 @@ namespace CE
}
}();

World::PushWorld(mWorld);

if constexpr (isEmpty)
{
mRegistry.emplace<ComponentType>(toEntity, std::forward<AdditonalArgs>(additionalArgs)...);
Expand All @@ -236,8 +231,6 @@ namespace CE
events.mOnBeginPlay->mFunc.get().InvokeUncheckedUnpacked(GetWorld(), toEntity);
}
}

World::PopWorld();
}
else
{
Expand Down Expand Up @@ -271,8 +264,6 @@ namespace CE
}
}

World::PopWorld();

return component;
}
}
Expand Down Expand Up @@ -307,7 +298,6 @@ namespace CE
template<typename ComponentType>
void Registry::RemoveComponent(entt::entity fromEntity)
{
World::PushWorld(mWorld);
static constexpr TypeId componentClassTypeId = MakeStrippedTypeId<ComponentType>();
entt::sparse_set* storage = Storage(componentClassTypeId);
ASSERT(storage != nullptr);
Expand Down Expand Up @@ -335,7 +325,6 @@ namespace CE
}

storage->erase(fromEntity);
World::PopWorld();
}

template <typename ComponentType, typename It>
Expand All @@ -358,8 +347,6 @@ namespace CE
return;
}

World::PushWorld(mWorld);

if constexpr (sIsReflectable<ComponentType>)
{
static std::optional<BoundEvent> endPlayEvent =
Expand All @@ -379,7 +366,6 @@ namespace CE
{
if (!storage->contains(fromEntity))
{
World::PopWorld();
return;
}

Expand All @@ -388,7 +374,6 @@ namespace CE
}

storage->remove(fromEntity);
World::PopWorld();
}

template<typename It>
Expand Down
14 changes: 5 additions & 9 deletions Include/World/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ namespace CE
{
public:
World(bool beginPlayImmediately);
World(World&& other) noexcept;
World(const World&) = delete;

~World();
World(World&&) = delete;
World(const World&) = delete;

World& operator=(World&& other) noexcept;
World& operator=(World&&) noexcept = delete;
World& operator=(const World&) = delete;

~World();

void Tick(float deltaTime);
void Render(FrameBuffer* renderTarget = nullptr);

Expand Down Expand Up @@ -75,11 +76,6 @@ namespace CE

void RequestEndplay();

static void PushWorld(World& world);
static void PopWorld();

static World* TryGetWorldAtTopOfStack();

/**
* \brief Will request a transition to a different level.
*
Expand Down
2 changes: 0 additions & 2 deletions Include/World/WorldViewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace CE
glm::vec3 ScreenToWorldPlane(glm::vec2 screenPosition, float planeHeight) const;

private:
// mWorld needs to be updated in World::World(World&&), so we give access to World to do so.
friend class World;
std::reference_wrapper<const World> mWorld;

// In pixels
Expand Down
46 changes: 23 additions & 23 deletions Source/Assets/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CE::Level::Level(std::string_view name) :

CE::Level::Level(AssetLoadInfo& loadInfo) :
Asset(loadInfo),
mWorld(false)
mWorld(std::make_unique<World>(false))
{
BinaryGSONObject savedData{};
savedData.LoadFromBinary(loadInfo.GetStream());
Expand Down Expand Up @@ -178,7 +178,7 @@ void CE::Level::OnSave(AssetSaveInfo& saveInfo) const
{
if (!mSerializedWorld.has_value())
{
if (!mWorld.has_value())
if (mWorld == nullptr)
{
LOG(LogAssets, Error, "Cannot save level {}, mSerializedComponents and mWorld are null",
GetName());
Expand Down Expand Up @@ -213,49 +213,49 @@ void CE::Level::CreateFromWorld(const World& world)
mSerializedWorld.emplace(Archiver::Serialize(world));
}

CE::World CE::Level::CreateWorld(const bool callBeginPlayImmediately) const
void CE::Level::LoadIntoWorld(World& world) const
{
if (mWorld.has_value())
if (!mSerializedWorld.has_value())
{
World world = std::move(*mWorld);
mWorld.reset();
LOG(LogAssets, Warning, "Failed to load {} into world, mSerializedWorld was null", GetName());
return;
}

Archiver::Deserialize(world, *mSerializedWorld);
}

std::unique_ptr<CE::World> CE::Level::CreateWorld(const bool callBeginPlayImmediately) const
{
if (mWorld != nullptr)
{
if (!mSerializedWorld.has_value())
{
mSerializedWorld.emplace(Archiver::Serialize(world));
mSerializedWorld.emplace(Archiver::Serialize(*mWorld));
}

if (callBeginPlayImmediately)
{
world.BeginPlay();
mWorld->BeginPlay();
}

return world;
return std::move(mWorld);
}

World world{ false };

if (mSerializedWorld.has_value())
{
Archiver::Deserialize(world, *mSerializedWorld);
}
else
{
LOG(LogAssets, Warning, "mWorld and mSerializedWorld were both null for {}", GetName());
}
std::unique_ptr<World> world = std::make_unique<World>(false);
LoadIntoWorld(*world);

if (callBeginPlayImmediately)
{
world.BeginPlay();
world->BeginPlay();
}

return world;
}

CE::World CE::Level::CreateDefaultWorld()
std::unique_ptr<CE::World> CE::Level::CreateDefaultWorld()
{
World world{ false };
Registry& reg = world.GetRegistry();
std::unique_ptr<World> world = std::make_unique<World>(false);
Registry& reg = world->GetRegistry();

{
const entt::entity camera = reg.Create();
Expand Down
Loading