Set up the CameraDirector creation and setting of main camera

This commit is contained in:
maverickdgg 2022-10-21 09:53:19 +08:00
parent 31ad8b8c87
commit 267ad5f8c6
6 changed files with 46 additions and 6 deletions

View File

@ -1,7 +1,8 @@
#include "SHpch.h" #include "SHpch.h"
#include "SHCameraComponent.h" #include "SHCameraComponent.h"
#include "ECS_Base/Managers/SHComponentManager.h" #include "ECS_Base/Managers/SHComponentManager.h"
#include "SHCameraSystem.h"
#include "ECS_Base/Managers/SHSystemManager.h"
namespace SHADE namespace SHADE
{ {
@ -128,4 +129,12 @@ namespace SHADE
return projMatrix; return projMatrix;
} }
void SHCameraComponent::SetMainCamera(size_t directorCameraIndex) noexcept
{
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
system->GetDirector(directorCameraIndex)->SetMainCamera(*this);
}
} }

View File

@ -70,6 +70,8 @@ namespace SHADE
const SHMatrix& GetViewMatrix() const noexcept; const SHMatrix& GetViewMatrix() const noexcept;
const SHMatrix& GetProjMatrix() const noexcept; const SHMatrix& GetProjMatrix() const noexcept;
void SetMainCamera(size_t cameraDirectorIndex = 0) noexcept;
float movementSpeed; float movementSpeed;
SHVec3 turnSpeed; SHVec3 turnSpeed;

View File

@ -3,6 +3,7 @@
#include "SHCameraComponent.h" #include "SHCameraComponent.h"
#include "ECS_Base/Managers/SHComponentManager.h" #include "ECS_Base/Managers/SHComponentManager.h"
#include "ECS_Base/SHECSMacros.h" #include "ECS_Base/SHECSMacros.h"
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Tools/SHLog.h" #include "Tools/SHLog.h"
namespace SHADE namespace SHADE
@ -42,9 +43,18 @@ namespace SHADE
viewMatrix = camComponent->GetViewMatrix(); viewMatrix = camComponent->GetViewMatrix();
projMatrix = camComponent->GetProjMatrix(); projMatrix = camComponent->GetProjMatrix();
} }
}
void SHCameraDirector::SetMainCamera(SHCameraComponent& camera) noexcept
{
if (SHEntityManager::IsValidEID(camera.GetEID()) == false)
{
SHLOG_WARNING("Camera Director Warning: Attempting to set an invalid entity as main camera.")
return;
}
mainCameraEID = camera.GetEID();
} }
} }

View File

@ -3,10 +3,15 @@
#include "SH_API.h" #include "SH_API.h"
#include "ECS_Base/Entity/SHEntity.h" #include "ECS_Base/Entity/SHEntity.h"
#include "Math/SHMatrix.h" #include "Math/SHMatrix.h"
#include "Resource/Handle.h"
namespace SHADE namespace SHADE
{ {
class SHCameraComponent;
class SH_API SHCameraDirector class SH_API SHCameraDirector
{ {
public: public:
@ -21,6 +26,7 @@ namespace SHADE
SHMatrix GetProjMatrix() const noexcept; SHMatrix GetProjMatrix() const noexcept;
SHMatrix GetVPMatrix() const noexcept; SHMatrix GetVPMatrix() const noexcept;
void UpdateMatrix() noexcept; void UpdateMatrix() noexcept;
void SetMainCamera(SHCameraComponent& cam) noexcept;
private: private:
@ -32,5 +38,6 @@ namespace SHADE
}; };
typedef Handle<SHCameraDirector> DirectorHandle;
} }

View File

@ -187,11 +187,23 @@ namespace SHADE
} }
Handle<SHCameraDirector> SHCameraSystem::CreateDirector() noexcept DirectorHandle SHCameraSystem::CreateDirector() noexcept
{ {
auto handle = directorLibrary.Create(); auto handle = directorLibrary.Create();
directorHandleList.emplace_back(handle); directorHandleList.emplace_back(handle);
return handle; return handle;
} }
DirectorHandle SHCameraSystem::GetDirector(size_t index) noexcept
{
if (index < directorHandleList.size())
{
return directorHandleList[index];
}
else
{
return CreateDirector();
}
}
} }

View File

@ -5,7 +5,6 @@
#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/System/SHSystemRoutine.h"
#include "Resource/ResourceLibrary.h" #include "Resource/ResourceLibrary.h"
#include "SHCameraDirector.h" #include "SHCameraDirector.h"
#include "Resource/Handle.h"
#include "SH_API.h" #include "SH_API.h"
namespace SHADE namespace SHADE
@ -18,7 +17,7 @@ namespace SHADE
SHCameraComponent editorCamera; SHCameraComponent editorCamera;
ResourceLibrary<SHCameraDirector> directorLibrary; ResourceLibrary<SHCameraDirector> directorLibrary;
std::vector<Handle<SHCameraDirector>> directorHandleList; std::vector<DirectorHandle> directorHandleList;
public: public:
SHCameraSystem(void) = default; SHCameraSystem(void) = default;
@ -48,7 +47,8 @@ namespace SHADE
SHCameraComponent* GetEditorCamera (void) noexcept; SHCameraComponent* GetEditorCamera (void) noexcept;
void GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& up) const noexcept; void GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& up) const noexcept;
Handle<SHCameraDirector> CreateDirector() noexcept; DirectorHandle CreateDirector() noexcept;
DirectorHandle GetDirector(size_t index) noexcept;
protected: protected:
void UpdateCameraComponent(SHCameraComponent& camera) noexcept; void UpdateCameraComponent(SHCameraComponent& camera) noexcept;