forked from No-needto-recall/cmdGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.h
More file actions
110 lines (85 loc) · 2.52 KB
/
Copy pathGameObject.h
File metadata and controls
110 lines (85 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include <string>
#include <memory>
#include <unordered_map>
using std::string;
using std::unique_ptr;
using std::shared_ptr;
using std::unordered_map;
//前置声明
class GameObjectType;
class GameObject;
//类型别名
using AutoGameObjectType = unique_ptr<GameObjectType>;
using AutoGameObject = shared_ptr<GameObject>;
using TypeName = string;
//图标类型
using Icon = char;
//位置结构体
struct Location {
int x;
int y;
// 转换为字符串的函数,便于记录日志
string ToString() const;
};
//object类型枚举
namespace ObjectType{
enum Type
{
PLAYER = 0, GROUND, WALL, PORTAL, GRASS
};
}//end of ObjectType
//类型对象
class GameObjectType {
public:
GameObjectType(const string& name, const char& icon);
const string& getName()const;
const char& getIcon()const;
const ObjectType::Type getType()const;
private:
TypeName _name;
char _icon;
};
//游戏中的基础实体
class GameObject {
public:
GameObject(const Location& location, GameObjectType* type);
// 获取物体的位置
const Location& GetLocation()const;
// 设置物体的位置
void SetLocation(const Location& newLocation);
//获取位置的字符串形式
string LocationToString()const;
//获取类别
ObjectType::Type GetType()const;
// 获取物体的图标
const Icon& GetIcon() const;
// 获取物体的名称
const string& GetName() const;
private:
Location _location;
GameObjectType* _type;
};
//单例工厂
class GameObjectFactory {
public:
//获取句柄
static GameObjectFactory& getInstance();
//创建
AutoGameObject create(const TypeName& name, const char icon, const Location& location);
//根据配置文件创建
AutoGameObject createFromConf(ObjectType::Type index, const Location& location);
AutoGameObject createPlayerFromConf(const Location& location);
AutoGameObject createGroundFromConf(const Location& location);
AutoGameObject createWallFromConf(const Location& location);
AutoGameObject createPortalFromConf(const Location& location);
AutoGameObject createGrassFromConf(const Location& location);
private:
//存在类型对象,保障享元模式
unordered_map<TypeName, AutoGameObjectType> _types;
//单例模式,构造私有,禁止赋值语句
GameObjectFactory(const GameObjectFactory&) = delete;
void operator = (const GameObjectFactory&) = delete;
GameObjectFactory();
};
#define GAMEOBJECT_FACTORY GameObjectFactory::getInstance()