Merge pull request #68 from SHADE-DP/SP3-5-ECS

Sp3 5 ecs
Fixed SHFamilyID for SHSystem and SHComponent. It is still bugged for SHScene. For now we cant change SHScene.

Added ComponentAdded and ComponentRemoved events. (untested).
This commit is contained in:
XiaoQiDigipen 2022-09-30 20:27:44 +08:00 committed by GitHub
commit 4647a94aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 119 additions and 18 deletions

View File

@ -32,6 +32,8 @@
#include "Assets/SHAssetManager.h" #include "Assets/SHAssetManager.h"
#include "Tools/SHLogger.h"
using namespace SHADE; using namespace SHADE;
namespace Sandbox namespace Sandbox
@ -85,6 +87,11 @@ namespace Sandbox
SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds"); SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds");
SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds"); SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds");
SHADE::SHAssetManager::LoadDataTemp("../../Assets/TD_Checker_Base_Color.dds"); SHADE::SHAssetManager::LoadDataTemp("../../Assets/TD_Checker_Base_Color.dds");
auto id = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
auto id2 = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
auto id3 = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
//TODO: REMOVE AFTER PRESENTATION //TODO: REMOVE AFTER PRESENTATION
//SHADE::SHSystemManager::RegisterRoutine<SHADE::SHAudioSystem, SHADE::SHAudioSystem::AudioRoutine>(); //SHADE::SHSystemManager::RegisterRoutine<SHADE::SHAudioSystem, SHADE::SHAudioSystem::AudioRoutine>();
@ -100,7 +107,9 @@ namespace Sandbox
#else #else
#endif #endif
SHSceneManager::InitSceneManager<SBTestScene>("TestScene"); SHSceneManager::InitSceneManager<SBTestScene>("TestScene");
SHFrameRateController::UpdateFRC(); SHFrameRateController::UpdateFRC();
} }

View File

@ -14,6 +14,7 @@
#include "SHpch.h" #include "SHpch.h"
#include "../SHECSMacros.h" #include "../SHECSMacros.h"
#include "SH_API.h" #include "SH_API.h"
#include "ECS_Base/General/SHFamily.h"
namespace SHADE namespace SHADE
{ {
@ -117,4 +118,7 @@ namespace SHADE
}; };
template class SH_API SHFamilyID<SHComponent>;
} }

View File

@ -0,0 +1,12 @@
#pragma once
#include "ECS_Base/Components/SHComponent.h"
namespace SHADE
{
struct SHComponentAddedEvent
{
EntityID eid;
ComponentTypeID addedComponentType;
};
}

View File

@ -0,0 +1,12 @@
#pragma once
#include "ECS_Base/Components/SHComponent.h"
namespace SHADE
{
struct SHComponentRemovedEvent
{
EntityID eid;
ComponentTypeID removedComponentType;
};
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "SHFamily.h"
#include "SHpch.h"
namespace SHADE
{
//initialize currentID as 0
}

View File

@ -14,16 +14,17 @@
#pragma once #pragma once
#include "../SHECSMacros.h" #include "../SHECSMacros.h"
#include "SH_API.h"
namespace SHADE namespace SHADE
{ {
template<typename BaseClass> template<typename BaseClass>
class SHFamilyID class SH_API SHFamilyID
{ {
private: private:
//this is used to keep track of the new current ID to be assign to a new Derived class type.
static ComponentTypeID currentID;
/*!************************************************************************* /*!*************************************************************************
* \brief Construct a new SHFamilyID object * \brief Construct a new SHFamilyID object
@ -46,6 +47,9 @@ namespace SHADE
} }
public: public:
//this is used to keep track of the new current ID to be assign to a new Derived class type.
static inline ComponentTypeID currentID = 0;
/*!************************************************************************* /*!*************************************************************************
* \brief * \brief
* Checks if this identifier is cuurrently in use / valid. * Checks if this identifier is cuurrently in use / valid.
@ -59,7 +63,6 @@ namespace SHADE
{ {
return(id < currentID); return(id < currentID);
} }
/*!************************************************************************* /*!*************************************************************************
* \brief * \brief
* Get the ID of a derived class type. * Get the ID of a derived class type.
@ -68,16 +71,27 @@ namespace SHADE
* @tparam DerivedClass * @tparam DerivedClass
* The derived class type that we are trying to get the ID of. * The derived class type that we are trying to get the ID of.
***************************************************************************/ ***************************************************************************/
#ifdef SH_API_EXPORT
template<typename DerivedClass> template<typename DerivedClass>
static ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept static SH_API ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept
{ {
//The first time a new derived class type call this get id, it will initialize id using the currentID from familyID class. //The first time a new derived class type call this get id, it will initialize id using the currentID from familyID class.
static ComponentTypeID id = currentID++; static ComponentTypeID id = SHFamilyID<BaseClass>::currentID++;
return id; return id;
//return 0;
} }
#else
template<typename DerivedClass>
static SH_API ENABLE_IF_DERIVED(ComponentTypeID, BaseClass, DerivedClass) GetID() noexcept;
#endif // SH_API_EXPORT
}; };
//initialize currentID as 0
template<typename BaseClass>
ComponentTypeID SHFamilyID<BaseClass>::currentID = 0;
} }

View File

@ -17,6 +17,8 @@
#include "../General/SHSparseSetContainer.h" #include "../General/SHSparseSetContainer.h"
#include "../Components/SHComponent.h" #include "../Components/SHComponent.h"
#include "../Components/SHComponentGroup.h" #include "../Components/SHComponentGroup.h"
#include "../Events/SHComponentAddedEvent.h"
#include "../Events/SHComponentRemovedEvent.h"
//#include "Scene/SHSceneNode.h" //#include "Scene/SHSceneNode.h"
#include "SH_API.h" #include "SH_API.h"
@ -216,6 +218,11 @@ namespace SHADE
comp->OnCreate(); comp->OnCreate();
} }
SHComponentAddedEvent eventData;
eventData.eid = entityID;
eventData.addedComponentType = ComponentFamily::GetID<T>();
SHEventManager::BroadcastEvent<SHComponentAddedEvent>(eventData, SH_COMPONENT_ADDED_EVENT);
} }
/************************************************************************** /**************************************************************************
@ -247,6 +254,13 @@ namespace SHADE
{ {
comp->OnCreate(); comp->OnCreate();
} }
SHComponentAddedEvent eventData;
eventData.eid = entityID;
eventData.addedComponentType = componentTypeID;
SHEventManager::BroadcastEvent<SHComponentAddedEvent>(eventData, SH_COMPONENT_ADDED_EVENT);
} }
@ -313,6 +327,12 @@ namespace SHADE
componentSet.GetSparseSet<T>()->Remove(EntityHandleGenerator::GetIndex(entityID)); componentSet.GetSparseSet<T>()->Remove(EntityHandleGenerator::GetIndex(entityID));
SHComponentRemovedEvent eventData;
eventData.eid = entityID;
eventData.addedComponentType = ComponentFamily::GetID<T>();
SHEventManager::BroadcastEvent<SHComponentRemovedEvent>(eventData, SH_COMPONENT_REMOVED_EVENT);
} }
/*!************************************************************************* /*!*************************************************************************
@ -464,11 +484,6 @@ namespace SHADE
return componentGroups[index]; return componentGroups[index];
} }
static void AddScriptComponent(EntityID eid, std::string const& scriptClassName) noexcept;
static void RemoveScriptComponent(EntityID eid, std::string const& scriptClassName) noexcept;
};// end SHComponentManager };// end SHComponentManager

View File

@ -68,6 +68,9 @@ namespace SHADE
id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID; id = ((SystemID)version << sizeof(SystemVersionID) * CHAR_BIT) + typeID;
} }
systemContainer.emplace(id, std::make_unique<T>()); systemContainer.emplace(id, std::make_unique<T>());
auto size = systemContainer.size();
systemContainer[id].get()->systemID = id; systemContainer[id].get()->systemID = id;
return id; return id;

View File

@ -26,4 +26,6 @@ const EntityIndex MAX_EID = 51000;
#define ENABLE_IF_UINT(_TYPE, _RETURN)\ #define ENABLE_IF_UINT(_TYPE, _RETURN)\
typename std::enable_if<(std::is_integral<_TYPE>::value && !std::is_signed<_TYPE>::value),_RETURN>::type typename std::enable_if<(std::is_integral<_TYPE>::value && !std::is_signed<_TYPE>::value),_RETURN>::type
#endif #endif

View File

@ -12,6 +12,7 @@
#include "../SHECSMacros.h" #include "../SHECSMacros.h"
#include "SH_API.h" #include "SH_API.h"
#include "ECS_Base/General/SHFamily.h"
namespace SHADE namespace SHADE
{ {
@ -69,5 +70,9 @@ namespace SHADE
}; };
template class SH_API SHFamilyID<SHSystem>;
} }

View File

@ -23,6 +23,8 @@
#include "SHEditor.hpp" #include "SHEditor.hpp"
#include "SHEditorWidgets.hpp" #include "SHEditorWidgets.hpp"
#include "Math/Transform/SHTransformSystem.h"
//#==============================================================# //#==============================================================#
//|| Editor Window Includes || //|| Editor Window Includes ||
//#==============================================================# //#==============================================================#
@ -87,6 +89,11 @@ namespace SHADE
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; //Enable docking io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; //Enable docking
InitFonts(); InitFonts();
auto id = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
auto id2 = SHFamilyID<SHSystem>::GetID<SHTransformSystem>();
auto id3 = SHFamilyID<SHSystem>::GetID<SHGraphicsSystem>();
InitBackend(sdlWindow); InitBackend(sdlWindow);
SetStyle(Style::SHADE); SetStyle(Style::SHADE);
@ -104,7 +111,6 @@ namespace SHADE
{ {
(void)dt; (void)dt;
NewFrame(); NewFrame();
for (const auto& window : editorWindows | std::views::values) for (const auto& window : editorWindows | std::views::values)
{ {
if(window->isOpen) if(window->isOpen)

View File

@ -8,3 +8,5 @@ typedef uint32_t SHEventHandle;
constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0}; constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0};
constexpr SHEventIdentifier SH_ENTITY_DESTROYED_EVENT{ 1 }; constexpr SHEventIdentifier SH_ENTITY_DESTROYED_EVENT{ 1 };
constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT{ 2 }; constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT{ 2 };
constexpr SHEventIdentifier SH_COMPONENT_ADDED_EVENT{ 3 };
constexpr SHEventIdentifier SH_COMPONENT_REMOVED_EVENT{ 4 };

View File

@ -13,6 +13,7 @@
#include <string> #include <string>
#include "SHSceneGraph.h" #include "SHSceneGraph.h"
#include "ECS_Base/General/SHFamily.h"
namespace SHADE namespace SHADE
{ {
@ -42,6 +43,7 @@ namespace SHADE
virtual void Unload() = 0; virtual void Unload() = 0;
}; };
template class SH_API SHFamilyID<SHScene>;
} }

View File

@ -84,7 +84,8 @@ namespace SHADE
{ {
//prevSceneCreate = newScene; //prevSceneCreate = newScene;
newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName = sceneName; }; newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName = sceneName; };
nextSceneID = SHFamilyID<SHScene>::template GetID<T>(); //nextSceneID = SHFamilyID<SHScene>::GetID<T>();
nextSceneID = 0;
sceneChanged = true; sceneChanged = true;
} }
@ -101,13 +102,13 @@ namespace SHADE
static std::enable_if_t<std::is_base_of_v<SHScene, T>, void> ChangeScene(std::string const& sceneName) noexcept static std::enable_if_t<std::is_base_of_v<SHScene, T>, void> ChangeScene(std::string const& sceneName) noexcept
{ {
//check if this new Scene is current Scene (Use RestartScene instead) //check if this new Scene is current Scene (Use RestartScene instead)
if (currentSceneID == SHFamilyID<SHScene>::template GetID<T>()) if (currentSceneID == SHFamilyID<SHScene>::GetID<T>())
{ {
return; return;
} }
//prevSceneCreate = newScene; //prevSceneCreate = newScene;
newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName; }; newScene = [sceneName]() { currentScene = new T(); currentScene->sceneName; };
nextSceneID = SHFamilyID<SHScene>::template GetID<T>(); nextSceneID = SHFamilyID<SHScene>::GetID<T>();
sceneChanged = true; sceneChanged = true;
} }