• Home
  • About
    • 게임 개발자 유정룡 photo

      게임 개발자 유정룡

      포트폴리오

    • Learn More
    • Email
    • Github
    • Bitbucket
  • Projects
    • All Projects
    • All Tags

DirectX11 공부 9일차(Component 패턴)

09 Jun 2021

Reading time ~1 minute

Component 패턴

Component 유니티나 언리얼에서 본 그 컴포넌트이다.

  • 장점
    • 각자의 기능을 갖고있는 스스로 동작하는 클래스다.
    • 컴포넌트를 뗀다고 해서 오브젝트의 다른 컴포넌트에 영향이 없다.
      • 컴포넌트들끼리 독립적이며, 커플링이 없다.
    • 코드의 의존성을 줄이고 재활용성을 높인다.
  • 단점
    • 지나치게 자유로워서 클레스의 구성요소에 제약을 가하기 힘듦

이러한 이유로 인해 다양한 곳에 사용되고 있다고 한다.

당장 유니티나 언리얼을 실행시키면 컴포넌트 패턴을 사용하고 있다.

이번엔 유니티에 적용한 컴포넌트대로 제작해 보려 한다.

Component.h

생각보다 컴포넌트에는 많은 데이터가 없다.

다만 컴포넌트를 갖고있는 오브젝트는 할게 많지….

#pragma once

enum class ComponentType : uint
{
	Unknown,
	Camera,
	Transform
};

class IComponent
{
public:
	template <typename T>
	static constexpr ComponentType DeduceComponenetType();

public:
	IComponent(class Actor* const actor, class Transform* const transform);
	virtual ~IComponent() = default;

	virtual void Initialize() = 0;
	virtual void Update() = 0;
	virtual void Destroy() = 0;

	Actor* GetActor() const { return actor; }
	void SetActor(class Actor* const actor) { this->actor = actor; }

	Transform* GetTransform() const { return transform; }
	void SetTransform(class Transform* const transform) { this->transform = transform; }

	ComponentType GetComponentType() const { return component_type; }
	void SetComponentType(const ComponentType& component_type) { this->component_type = component_type; }

	bool IsEnabled() const { return is_enabled; }
	void SetEnabled(const bool& is_enabled) { this->is_enabled = is_enabled; }

protected:
	class Actor* actor = nullptr;
	class Transform* transform = nullptr;

	ComponentType component_type = ComponentType::Unknown;
	bool is_enabled = true;
};

이 컴포넌트 클래스는 이 클래스를 상속받아 사용하는 클래스이기 때문에 추상 클래스로 만든다.

Component.h

#include "stdafx.h"
#include "IComponent.h"

IComponent::IComponent(Actor* const actor, Transform* const transform)
	:actor(actor)
	,transform(transform)
{
}

template<typename T>
constexpr ComponentType IComponent::DeduceComponenetType()
{
	return ComponentType::Unknown;
}

// explicite template instantiation
#define REGISTER_COMPONENT_TYPE(T, enum_type) template <>ComponentType IComponent::DeduceComponenetType<T>(){	return enum_type; }
#define REGISTER_COMPONENT_TYPE(T, enum_type) template <>ComponentType IComponent::DeduceComponenetType<T>(){	return enum_type; }

이부분은

template <typename T>
ComponentType IComponent::DeduceComponenetType<T>()
{	
	return enum_type; 
}

이 코드를 매크로로 만든것이다.



DirectX Share Tweet +1