[DirectX] 엔터티와 오브젝트 기본 설계
in Study on WinAPI&DirectX
CEntity 설계
// 헤더파일
#pragma once
class CEntity
{
private:
static UINT g_iNextID;
private:
wstring m_strName;
const UINT m_iID;
public:
void SetName(const wstring& _strName) { m_strName = _strName; }
const wstring& GetName() { return m_strName; }
UINT GetID() { return m_iID; }
public:
virtual CEntity* Clone() = 0;
public:
CEntity();
virtual ~CEntity();
}
// cpp 파일
#include "pch.h"
#include "CEntity.h"
UINT CEntity::g_iNextID = 0;
CEntity::CEntity()
{
}
CEntity::CEntity(const CEntity& _other)
: m_iID(g_iNextID++) // ID의 중복 방지
, m_strName(_other.m_strName)
{
}
CEntity::~CEntity()
{
}
#define CLONE(type) virtual type* Clone() { return new type(*this); }
CGameObject 설계
#pragma once
#include "CEntity.h"
class CComponent;
class CGameObject :
public CEntity
{
private:
CComponent* m_arrCom[(UINT)COMPONENT_TYPE::END];
public:
CLONE(CGameObject);
public:
void AddComponent(COMPONENT_TYPE _type, CComponent* _Component);
public:
CGameObject();
~CGameObject
}
#include "CGameObject.h"
#include "pch.h"
#include "CComponent.h"
CGameObject::CGameObject()
: m_arrCom{}
{
}
CGameObject::~CGameObject()
{
Safe_Del_Array(m_arrCom);
/*
for (UINT i = 0; i < (UINT)COMPONENT_TYPE::END; ++i)
{
if (m_arrCom[i] != nullptr)
delete m_arrCom[i];
}
*/
}
void CGameObject::AddComponent(CComponent* _Component)
{
// 이미 보유하고 있는 컴포넌트인 경우
assert(!m_arrCom[(UINT)_Component->GetType()]);
// 무사히 통과했을 경우
_Component->m_pOwner = this;
m_arrCom[(UINT)_Component->GetType()] = _Component;
}
delete하는 과정 함수 템플릿으로 하기
template<typename T, UINT Size>
void Safe_Del_Array(T* (&arr)[Size])
{
for (UINT i = 0; i < Size; ++i)
{
if (arr[i] != nullptr)
delete arr[i];
}
}
Component 설계
레벨, 카메라, 광원, 타일 맵 등을 관리하는 Component 객체 설계
enum class COMPONENT_TYPE
{
// update
TRANSFORM, // 위치 관련
COLLIDER2D, // 충돌체
COLLIDER3D,
CAMERA,
ANIMATOR2D,
ANIMATOR3D,
LIGHT2D,
LIGHT3D,
// render
MESHRENDER,
PARTICLESYSTEM,
TILEMAP,
LANDSCAPE,
DECAL,
// custom
SCRIPT,
END
}
// 헤더파일
#pragma once
#include "CEntity.h"
class CGameObject;
class CComponent :
public CEntity
{
private:
CGameObject* m_pOwner;
const COMPONENT_TYPE m_Type;
public:
COMPONENT_TYPE GetType() { return m_Type;}
public:
virtual void finaltick() = 0;
virtual CComponent* Clone() = 0;
public:
CComponent(COMPONENT_TYPE _Type)
virtual ~CComponent();
friend class CGameObject;
}
//cpp 파일
#include "pch.h"
#include "CComponent.h"
CComponent::CComponent(COMPONENT_TYPE _Type)
{
}
CComponent::~CComponent()
{
}
WinAPI와는 달리, 카메라, 충돌체 등 기능을 컴포넌트를 상속받는 것을 통해서 구현하고자 한다.
이는 유니티의 방식과 유사하다고 한다.