Design Pattern/게임 디자인패턴
5장. 프로토타입 패턴
GGym_
2020. 4. 27. 21:49
책에선 클래스, 스폰 함수, 템플릿을 사용하여 프로토타입 패턴을 작성했다.
막상 작성하고 나니 프로토타입 패턴이라기 보단 Spawner를 만드는 것 같았지만 뒤에 코드없이 설명만 해둬서 그냥 내 입맛에 맞게 수정하였다.
수정할때 이전에 C++디자인패턴에서 배웠던 프로토타입 팩토리를 추가해서 프로토타입을 만들도록 하였다.
생성하는 과정이 복잡한데, 나중에 빌더 패턴하고 합쳐서 예쁘게 사용하면 좋을 듯 싶다.
- 클래스 기반 프로토타입 생성
더보기
class Spawner {
public:
virtual ~Spawner(){}
Spawner(Monster* prototype) : prototype_(prototype){}
Monster* spawnMonster(){
return prototype_->clone();
};
private:
Monster* prototype_;
};
Monster* GhostPrototype = new Ghost(50, 1);
Spawner* GhostSpawner = new Spawner(GhostPrototype);
- 스폰 함수로 생성
더보기
Monster* spawnGhost(){
return new Ghost(50, 1);
}
typedef Monster* (*SpawnCallback)();
class Spawner {
public:
Spawner (SpawnCallback spawn) : spawn_(spawn){}
Monster* spawnMonster(){
return spawn_();
}
private:
SpawnCallback spawn_;
};
Spawner* ghostSpawner = new Spawner(spawnGhost);
- 템플릿으로 생성
더보기
class Spawner{
public:
virtual ~Spawner(){}
virtual Monster* spawnMonster() = 0;
};
template <typename T>
class SpawnFor : public Spawner {
public:
virtual Monster* spawnMonster() {
return new T();
}
};
Spawner* ghostSpawner = new SpawnFor<Ghost>();
- 만든 전체 코드
#include<iostream>
#include<vector>
using namespace std;
class GameObject{
public:
GameObject(){}
};
class Monster {
public:
virtual ~Monster() {
delete gameObject_;
}
Monster(int health, int speed, GameObject* gameObject)
: health_(health), speed_(speed), gameObject_(gameObject){
cout << " 몬스터 생성" << endl;
}
protected:
int health_;
int speed_;
GameObject* gameObject_;
};
class Ghost : public Monster {
public:
Ghost(Ghost& ghost)
: Monster(ghost.health_, ghost.speed_, ghost.gameObject_){
}
Ghost(int health, int speed, GameObject* gameObject)
: Monster(health, speed, gameObject){
}
};
class Demon : public Monster {};
class Sorcerer : public Monster {};
/////////////////////////////////////////////////////////////////
class Spawner{
public:
virtual ~Spawner(){}
virtual Monster* spawnMonster() = 0;
};
template<typename T>
class SpawnFor : public Spawner {
public:
T* prototype_;
~SpawnFor(){
delete prototype_;
}
SpawnFor(T* prototype) : prototype_(prototype){}
virtual T* spawnMonster() {
return newMonster(*prototype_);
}
private:
T* newMonster(T& proto){
T* monster = new T(proto);
//
return monster;
}
};
int main(){
Ghost* originalGhost = new Ghost(50, 1, new GameObject());
SpawnFor<Ghost>* ghostSpawner = new SpawnFor<Ghost>(originalGhost);
vector<Monster*> monsters;
for(int i=0; i<3; i++)
monsters.push_back(ghostSpawner->spawnMonster());
for(auto i : monsters){
delete i;
}
return 0;
}