Sp-3-5 ecs #33

Merged
maverickdgg merged 4 commits from SP3-5-ECS into main 2022-09-19 17:31:21 +08:00
12 changed files with 101 additions and 21 deletions

View File

@ -47,32 +47,57 @@ namespace SHADE
void SHEntity::SetParent(SHEntity* newParent) noexcept
{
(void)newParent;
//TODO
SHSceneManager::GetCurrentSceneGraph().SetParent(GetEID(), newParent->GetEID());
}
void SHEntity::SetParent(EntityID newParentID) noexcept
{
(void)newParentID;
//TODO
SHSceneManager::GetCurrentSceneGraph().SetParent(GetEID(), newParentID);
}
SHEntity* SHEntity::GetParent()const noexcept
{
//TODO
return nullptr;
SHSceneNode* parent = SHSceneManager::GetCurrentSceneGraph().GetParent(GetEID());
if (parent != nullptr)
return SHEntityManager::GetEntityByID(parent->GetEntityID());
else
return nullptr;
}
EntityID SHEntity::GetParentEID()const noexcept
{
SHSceneNode* parent = SHSceneManager::GetCurrentSceneGraph().GetParent(GetEID());
if (parent != nullptr)
return parent->GetEntityID();
else
return MAX_EID;
}
std::vector<SHEntity*>const& SHEntity::GetChildren()const noexcept
{
//TODO
return std::vector<SHEntity*>{};
std::vector<SHEntity*> childrenEntity;
auto& childrenNodes = SHSceneManager::GetCurrentSceneGraph().GetChildren(GetEID());
for (auto& childNode : childrenNodes)
{
childrenEntity.push_back(SHEntityManager::GetEntityByID(childNode->GetEntityID()));
}
return childrenEntity;
}
std::vector<EntityID>const& SHEntity::GetChildrenID()const noexcept
{
return std::vector<EntityID>{};
std::vector<EntityID> childrenEntity;
auto& childrenNodes = SHSceneManager::GetCurrentSceneGraph().GetChildren(GetEID());
for (auto& childNode : childrenNodes)
{
childrenEntity.push_back(childNode->GetEntityID());
}
return childrenEntity;
}
}

View File

@ -127,6 +127,13 @@ namespace SHADE
***************************************************************************/
SHEntity* GetParent()const noexcept;
/********************************************************************
* \brief
* Get the entity ID of the parent.
* \return
* return the entity ID of the parent
********************************************************************/
EntityID GetParentEID() const noexcept;
/**************************************************************************
* \brief

View File

@ -8,5 +8,6 @@ namespace SHADE
{
EntityID eid;
std::vector<ComponentTypeID> componentTypeIDs;
EntityID parentEID;
};
}

View File

@ -103,8 +103,16 @@ namespace SHADE
SHComponentManager::AddComponent(eID, id);
}
//Link up with scene graph.
if (parentEID != MAX_EID)
SHSceneManager::GetCurrentSceneGraph().AddNode(eID, SHSceneManager::GetCurrentSceneGraph().GetNode(parentEID));
else
SHSceneManager::GetCurrentSceneGraph().AddNode(eID);
//set up event stuff
SHEntityCreationEvent event{ eID,componentTypeIDs };
SHEntityCreationEvent event{ eID,componentTypeIDs, parentEID };
SHEventManager::BroadcastEvent<SHEntityCreationEvent>(event, SH_ENTITY_CREATION_EVENT);
//(SHComponentManager::AddComponent<ComponentTypes>(eID), ...);
@ -138,6 +146,14 @@ namespace SHADE
//Call all the children to Destroy themselves first before the parent is destroyed.
if (entityVec[eIndex])
{
auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eID);
for (auto& child : children)
{
DestroyEntity(child->GetEntityID());
}
SHSceneManager::GetCurrentSceneGraph().RemoveNode(eID);
//auto& children = entityVec[eIndex]->GetChildrenID();
//while(!children.empty())
//{
@ -159,6 +175,8 @@ namespace SHADE
entityVec[eIndex].reset(nullptr);
SHEntityDestroyedEvent event{eID};
SHEventManager::BroadcastEvent<SHEntityDestroyedEvent>(event, SH_ENTITY_DESTROYED_EVENT);

View File

@ -22,6 +22,7 @@
#include "../SHECSMacros.h"
#include "ECS_Base/Events/SHEntityCreationEvent.h"
#include "ECS_Base/Events/SHEntityDestroyedEvent.h"
#include "Scene/SHSceneManager.h"
#include "Events/SHEventManager.hpp"
#include "SH_API.h"
@ -132,10 +133,19 @@ namespace SHADE
std::vector<ComponentTypeID> typeIDVec;
(typeIDVec.push_back(ComponentFamily::GetID<ComponentTypes>()), ...);
SHEntityCreationEvent event{ eID,typeIDVec };
//Link up with scene graph.
if (parentEID != MAX_EID)
SHSceneManager::GetCurrentSceneGraph().AddNode(eID, SHSceneManager::GetCurrentSceneGraph().GetNode(parentEID));
else
SHSceneManager::GetCurrentSceneGraph().AddNode(eID);
SHEntityCreationEvent event{ eID,typeIDVec,parentEID };
SHEventManager::BroadcastEvent<SHEntityCreationEvent>(event, SH_ENTITY_CREATION_EVENT);
/*if (entityHandle.IsValid(parentEID) == false)
{
entityVec[eIndex]->sceneNode.ConnectToRoot();
@ -146,8 +156,6 @@ namespace SHADE
}*/
//Link up with scene graph.
return eID;
}

View File

@ -40,7 +40,7 @@ namespace SHADE
std::string const GetName() const noexcept;
SHRoutineStats const& GetStats()const noexcept;
virtual void Execute(double dt) noexcept {};
virtual void Execute(double dt) noexcept { (void)dt; };
};

View File

@ -30,9 +30,9 @@ namespace SHADE
SHLOG_INFO("Test for add and remove component")
EntityID id1 = SHEntityManager::CreateEntity();
EntityID id2 = SHEntityManager::CreateEntity();
EntityID id3 = SHEntityManager::CreateEntity();
SHEntityManager::CreateEntity();
SHEntityManager::CreateEntity();
SHComponentManager::AddComponent<SHComponent_A>(id1);
}

View File

@ -30,7 +30,7 @@ namespace SHADE
virtual void Execute(double dt) noexcept
{
(void)dt;
std::cout << GetName() << " System Version: " << GetSystem()->GetSystemVersion() << std::endl;
}
};

View File

@ -12,6 +12,7 @@
#define SH_SCENE_H
#include <string>
#include "SHSceneGraph.h"
namespace SHADE
{
@ -19,15 +20,19 @@ namespace SHADE
class SHScene
{
private:
SHSceneGraph sceneGraph;
protected:
SHScene() = default;
public:
virtual ~SHScene() = default;
std::string sceneName;
virtual ~SHScene() = default;
SHSceneGraph& GetSceneGraph() noexcept { return sceneGraph; }
virtual void Load() = 0;
virtual void Init() = 0;

View File

@ -14,6 +14,7 @@
// Project Headers
#include "ECS_Base/Entity/SHEntity.h"
#include "SH_API.h"
namespace SHADE
{
@ -21,7 +22,7 @@ namespace SHADE
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
class SHSceneNode
class SH_API SHSceneNode
{
public:
/*---------------------------------------------------------------------------------*/
@ -75,7 +76,7 @@ namespace SHADE
std::vector<SHSceneNode*> children;
};
class SHSceneGraph
class SH_API SHSceneGraph
{
public:
/*---------------------------------------------------------------------------------*/

View File

@ -37,6 +37,12 @@ namespace SHADE
std::function<void()> SHSceneManager::newScene = []() {};
//void (*SHSceneManager::prevSceneCreate)() = []() {};
SHSceneGraph& SHSceneManager::GetCurrentSceneGraph() noexcept
{
return currentScene->GetSceneGraph();
}
void SHSceneManager::UpdateSceneManager() noexcept
{
if (sceneChanged == false)

View File

@ -57,6 +57,15 @@ namespace SHADE
//boolean to check if the programme has been terminated.
static bool quit;
/********************************************************************
* \brief
* Get the scene graph of the current scene.
* \return
* A reference to the scene graph of the current active scene.
********************************************************************/
static SHSceneGraph& GetCurrentSceneGraph() noexcept;
/*!*************************************************************************
* \brief
* Initialize scene manager and loads a default scene