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

Added Basic Event Util #35

Merged
merged 1 commit into from
Apr 14, 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
12 changes: 12 additions & 0 deletions Source/Game/Game/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ bool Application::Init()
ServiceLocator::SetTaskScheduler(_taskScheduler);

_registries.gameRegistry = new entt::registry();
_registries.eventIncomingRegistry = new entt::registry();
_registries.eventOutgoingRegistry = new entt::registry();
ServiceLocator::SetEnttRegistries(&_registries);

_inputManager = new InputManager();
Expand Down Expand Up @@ -451,6 +453,16 @@ bool Application::Tick(f32 deltaTime)
}

_ecsScheduler->Update(*_registries.gameRegistry, deltaTime);

// Handle Double Buffered Event Swap
{
_registries.eventIncomingRegistry->clear();

entt::registry* temp = _registries.eventIncomingRegistry;
_registries.eventIncomingRegistry = _registries.eventOutgoingRegistry;
_registries.eventOutgoingRegistry = temp;
}

_animationSystem->Update(deltaTime);

_editorHandler->Update(deltaTime);
Expand Down
2 changes: 2 additions & 0 deletions Source/Game/Game/Application/EnttRegistries.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
struct EnttRegistries
{
entt::registry* gameRegistry;
entt::registry* eventIncomingRegistry;
entt::registry* eventOutgoingRegistry;
};
10 changes: 10 additions & 0 deletions Source/Game/Game/ECS/Components/Events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <Base/Types.h>

namespace ECS::Components
{
struct MapLoadedEvent
{
u32 mapId;
};
}
16 changes: 8 additions & 8 deletions Source/Game/Game/ECS/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace ECS
{
class Scheduler
{
public:
Scheduler();
class Scheduler
{
public:
Scheduler();

void Init(entt::registry & registry);
void Update(entt::registry& registry, f32 deltaTime);
void Init(entt::registry & registry);
void Update(entt::registry& registry, f32 deltaTime);

private:
};
private:
};
}
59 changes: 40 additions & 19 deletions Source/Game/Game/ECS/Systems/CharacterController.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#include "CharacterController.h"

#include "Game/Animation/AnimationSystem.h"

#include "Game/ECS/Components/AABB.h"
#include "Game/ECS/Components/Camera.h"
#include "Game/ECS/Components/Events.h"
#include "Game/ECS/Components/Model.h"
#include "Game/ECS/Components/Name.h"
#include "Game/ECS/Singletons/ActiveCamera.h"
#include "Game/ECS/Singletons/CharacterSingleton.h"
#include "Game/ECS/Singletons/FreeflyingCameraSettings.h"
#include "Game/ECS/Singletons/JoltState.h"
#include "Game/ECS/Singletons/OrbitalCameraSettings.h"
#include "Game/ECS/Util/EventUtil.h"
#include "Game/ECS/Util/Transforms.h"

#include "Game/Gameplay/MapLoader.h"
#include "Game/Rendering/GameRenderer.h"
#include "Game/Rendering/Debug/JoltDebugRenderer.h"
#include "Game/Rendering/Model/ModelLoader.h"
Expand Down Expand Up @@ -57,21 +58,7 @@ namespace ECS::Systems
transformSystem.SetWorldPosition(characterSingleton.modelEntity, vec3(0.0f, 0.0f, 0.0f));
transformSystem.ParentEntityTo(characterSingleton.entity, characterSingleton.modelEntity);

ModelLoader* modelLoader = ServiceLocator::GetGameRenderer()->GetModelLoader();
u32 modelHash = modelLoader->GetModelHashFromModelPath("character/human/female/humanfemale.complexmodel");
modelLoader->LoadModelForEntity(characterSingleton.modelEntity, modelHash);

JPH::BodyInterface& bodyInterface = joltState.physicsSystem.GetBodyInterface();

JPH::CapsuleShapeSettings shapeSetting(0.8f, 0.25f);
JPH::ShapeSettings::ShapeResult shapeResult = shapeSetting.Create();

JPH::CharacterVirtualSettings characterSettings;
characterSettings.mShape = shapeResult.Get();
characterSettings.mBackFaceMode = JPH::EBackFaceMode::IgnoreBackFaces;

characterSingleton.character = new JPH::CharacterVirtual(&characterSettings, JPH::RVec3(0.0f, 0.0f, 0.0f), JPH::Quat::sIdentity(), &joltState.physicsSystem);
characterSingleton.character->SetShapeOffset(JPH::Vec3(0.0f, 0.8f, 0.0f));
InitCharacterController(registry);

InputManager* inputManager = ServiceLocator::GetInputManager();
characterSingleton.keybindGroup = inputManager->CreateKeybindGroup("CharacterController", 100);
Expand Down Expand Up @@ -188,6 +175,11 @@ namespace ECS::Systems
entt::registry::context& ctx = registry.ctx();
auto& characterSingleton = ctx.get<Singletons::CharacterSingleton>();

Util::EventUtil::OnEvent<Components::MapLoadedEvent>([&](const Components::MapLoadedEvent& event)
{
InitCharacterController(registry);
});

#ifdef JPH_DEBUG_RENDERER
// TODO: Fix Jolt Primitives being erased in JoltDebugRenderer causing crash when changing map
//Components::Transform& transform = registry.get<Components::Transform>(characterSingleton.modelEntity);
Expand Down Expand Up @@ -525,18 +517,47 @@ namespace ECS::Systems
}
}

void CharacterController::ReInitCharacterModel(entt::registry& registry)
void CharacterController::InitCharacterController(entt::registry& registry)
{
entt::registry::context& ctx = registry.ctx();

auto& joltState = ctx.get<Singletons::JoltState>();
auto& transformSystem = ctx.get<TransformSystem>();
auto& activeCamera = ctx.get<Singletons::ActiveCamera>();
auto& orbitalCameraSettings = ctx.get<Singletons::OrbitalCameraSettings>();
auto& characterSingleton = ctx.emplace<Singletons::CharacterSingleton>();

ModelLoader* modelLoader = ServiceLocator::GetGameRenderer()->GetModelLoader();
u32 modelHash = modelLoader->GetModelHashFromModelPath("character/human/female/humanfemale.complexmodel");
modelLoader->LoadModelForEntity(characterSingleton.modelEntity, modelHash);

characterSingleton.character->SetPosition(JPH::Vec3(-1500.0f, 310.0f, 1250.0f));
JPH::BodyInterface& bodyInterface = joltState.physicsSystem.GetBodyInterface();

JPH::CapsuleShapeSettings shapeSetting(0.8f, 0.25f);
JPH::ShapeSettings::ShapeResult shapeResult = shapeSetting.Create();

JPH::CharacterVirtualSettings characterSettings;
characterSettings.mShape = shapeResult.Get();
characterSettings.mBackFaceMode = JPH::EBackFaceMode::IgnoreBackFaces;

characterSingleton.character = new JPH::CharacterVirtual(&characterSettings, JPH::RVec3(0.0f, 0.0f, 0.0f), JPH::Quat::sIdentity(), &joltState.physicsSystem);
characterSingleton.character->SetShapeOffset(JPH::Vec3(0.0f, 0.8f, 0.0f));

MapLoader* mapLoader = ServiceLocator::GetGameRenderer()->GetMapLoader();

JPH::Vec3 newPosition = JPH::Vec3(-1500.0f, 310.0f, 1250.0f);

if (mapLoader->GetCurrentMapID() == std::numeric_limits<u32>().max())
{
newPosition = JPH::Vec3(0.0f, 0.0f, 0.0f);
}

characterSingleton.character->SetPosition(newPosition);
transformSystem.SetWorldPosition(characterSingleton.entity, vec3(newPosition.GetX(), newPosition.GetY(), newPosition.GetZ()));

if (activeCamera.entity == orbitalCameraSettings.entity)
{
transformSystem.ParentEntityTo(characterSingleton.entity, orbitalCameraSettings.entity);
}
}
}
2 changes: 1 addition & 1 deletion Source/Game/Game/ECS/Systems/CharacterController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace ECS::Systems
static void Init(entt::registry& registry);
static void Update(entt::registry& registry, f32 deltaTime);

static void ReInitCharacterModel(entt::registry& registry);
static void InitCharacterController(entt::registry& registry);
};
}
38 changes: 38 additions & 0 deletions Source/Game/Game/ECS/Util/EventUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include "Game/Application/EnttRegistries.h"
#include "Game/Util/ServiceLocator.h"

#include <Base/Types.h>

#include <entt/entt.hpp>

namespace ECS::Util
{
namespace EventUtil
{
template <typename... Events>
inline void PushEventTo(entt::registry& registry, Events&&... events)
{
entt::entity eventEntity = registry.create();
(registry.emplace<std::decay_t<Events>>(eventEntity, std::forward<Events>(events)), ...);
}

template <typename... Events>
inline void PushEvent(Events&&... events)
{
EnttRegistries* registries = ServiceLocator::GetEnttRegistries();
entt::registry* eventRegistry = registries->eventOutgoingRegistry;

PushEventTo(*eventRegistry, std::forward<Events>(events)...);
}

template <typename T, typename Handler>
inline void OnEvent(Handler&& handler)
{
EnttRegistries* registries = ServiceLocator::GetEnttRegistries();
entt::registry* eventRegistry = registries->eventIncomingRegistry;

eventRegistry->view<T>().each(std::forward<Handler>(handler));
}
}
}
9 changes: 9 additions & 0 deletions Source/Game/Game/Gameplay/MapLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include "Game/Application/EnttRegistries.h"
#include "Game/Editor/EditorHandler.h"
#include "Game/Editor/Inspector.h"
#include "Game/ECS/Components/Events.h"
#include "Game/ECS/Singletons/ClientDBCollection.h"
#include "Game/ECS/Singletons/MapDB.h"
#include "Game/ECS/Util/EventUtil.h"
#include "Game/Rendering/GameRenderer.h"
#include "Game/Rendering/Debug/JoltDebugRenderer.h"
#include "Game/Rendering/Terrain/TerrainLoader.h"
Expand Down Expand Up @@ -39,6 +41,7 @@ void MapLoader::Update(f32 deltaTime)
// Clear Map
if (request.internalMapNameHash == std::numeric_limits<u32>().max())
{
_currentMapID = std::numeric_limits<u32>().max();
ClearRenderersForMap();
}
else
Expand Down Expand Up @@ -88,12 +91,16 @@ void MapLoader::Update(f32 deltaTime)
{
if (!_modelLoader->ContainsDiscoveredModel(mapHeader.placement.nameHash))
return;

_currentMapID = mapID;

ClearRenderersForMap();
_modelLoader->LoadPlacement(mapHeader.placement);
}
else
{
_currentMapID = mapID;

TerrainLoader::LoadDesc loadDesc;
loadDesc.loadType = TerrainLoader::LoadType::Full;
loadDesc.mapName = internalMapName;
Expand Down Expand Up @@ -129,4 +136,6 @@ void MapLoader::ClearRenderersForMap()

Editor::EditorHandler* editorHandler = ServiceLocator::GetEditorHandler();
editorHandler->GetInspector()->ClearSelection();

ECS::Util::EventUtil::PushEvent(ECS::Components::MapLoadedEvent{ _currentMapID });
}
4 changes: 2 additions & 2 deletions Source/Game/Game/Gameplay/MapLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MapLoader
void UnloadMap();
void LoadMap(u32 mapHash);

const u32 GetCurrentMapIndex() { return _currentMapIndex; }
const u32 GetCurrentMapID() { return _currentMapID; }

private:
void ClearRenderersForMap();
Expand All @@ -36,6 +36,6 @@ class MapLoader
ModelLoader* _modelLoader = nullptr;
LiquidLoader* _liquidLoader = nullptr;

u32 _currentMapIndex = std::numeric_limits<u32>().max();
u32 _currentMapID = std::numeric_limits<u32>().max();
moodycamel::ConcurrentQueue<LoadDesc> _requests;
};
12 changes: 8 additions & 4 deletions Source/Game/Game/Rendering/Model/ModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void ModelLoader::Clear()
bodyInterface.RemoveBody(id);
bodyInterface.DestroyBody(id);
}

_instanceIDToBodyID.clear();
}

for (auto& pair : _nameHashToJoltShape)
Expand Down Expand Up @@ -756,10 +758,12 @@ void ModelLoader::AddStaticInstance(entt::entity entityID, const LoadRequestInte

// Create the actual rigid body
JPH::Body* body = bodyInterface.CreateBody(bodySettings); // Note that if we run out of bodies this can return nullptr

JPH::BodyID bodyID = body->GetID();
bodyInterface.AddBody(bodyID, JPH::EActivation::Activate);
_instanceIDToBodyID[instanceID] = bodyID.GetIndexAndSequenceNumber();
if (body)
{
JPH::BodyID bodyID = body->GetID();
bodyInterface.AddBody(bodyID, JPH::EActivation::Activate);
_instanceIDToBodyID[instanceID] = bodyID.GetIndexAndSequenceNumber();
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Source/Game/Game/Rendering/Terrain/TerrainLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
#include "TerrainRenderer.h"

#include "Game/Application/EnttRegistries.h"
#include "Game/ECS/Components/Events.h"
#include "Game/ECS/Singletons/JoltState.h"
#include "Game/ECS/Systems/CharacterController.h"
#include "Game/ECS/Util/EventUtil.h"
#include "Game/Editor/EditorHandler.h"
#include "Game/Editor/Inspector.h"
#include "Game/Rendering/Debug/DebugRenderer.h"
#include "Game/Rendering/Debug/JoltDebugRenderer.h"
#include "Game/Rendering/GameRenderer.h"
#include "Game/Rendering/Model/ModelLoader.h"
#include "Game/Rendering/Liquid/LiquidLoader.h"
#include "Game/Gameplay/MapLoader.h"
#include "Game/Util/JoltStream.h"
#include "Game/Util/MapUtil.h"
#include "Game/Util/ServiceLocator.h"
Expand Down Expand Up @@ -402,7 +405,8 @@ void TerrainLoader::LoadFullMapRequest(const LoadRequestInternal& request)
taskScheduler->AddTaskSetToPipe(&loadChunksTask);
taskScheduler->WaitforTask(&loadChunksTask);

ECS::Systems::CharacterController::ReInitCharacterModel(*registry);
u32 mapID = ServiceLocator::GetGameRenderer()->GetMapLoader()->GetCurrentMapID();
ECS::Util::EventUtil::PushEvent(ECS::Components::MapLoadedEvent{ mapID });

i32 physicsOptimizeBP = *CVarSystem::Get()->GetIntCVar(CVarCategory::Client | CVarCategory::Physics, "optimizeBP"_h);
if (physicsEnabled && physicsOptimizeBP)
Expand Down
2 changes: 1 addition & 1 deletion Source/Game/Game/Scripting/LuaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Scripting
template <typename T>
bool SetGlobal(const std::string& name, T& value, bool canOverride)
{
if (_globalTable.data.contains(name) && canOverride)
if (_globalTable.data.contains(name) && !canOverride)
return false;

_globalTable.data[name] = value;
Expand Down
Loading