KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
GameObj.hpp
[詳解]
1#pragma once
2
3#include <vector>
4#include <typeinfo>
5#include <cassert>
6#include <string>
7#include <map>
8
10#include "ComponentList.hpp"
11#include "Transform.hpp"
12#include "Accessor.hpp"
13
14using std::string;
15using std::vector;
16using std::map;
18
19
20namespace Kisaragi_Lib
21{
22 class ComponentBase;
23 class Collision2DBase;
24
25 namespace GameObj
26 {
28 {
29 private:
30 //ユーザーが派生クラスで扱った時に勝手にdeleteしないようにする.
32
33 protected:
35 string id;
36 string name;
37
38 public:
39
40 string GetName()
41 {
42 return name;
43 }
44
45 virtual void OnCollisionEnter(GameObjBase* _other)
46 {
47 //TODO ここでメッセージ受信
48 }
49
50 virtual void OnCollisionStay(GameObjBase* _other)
51 {
52 //TODO ここでメッセージ受信
53 }
54
55 virtual void OnCollisionExit(GameObjBase* _other)
56 {
57 //TODO ここでメッセージ受信
58 }
59
61
62 GameObjBase() : isActive(true) {}
63 virtual ~GameObjBase(){}
64
65 /// <summary>
66 /// コンポーネントを追加
67 /// </summary>
68 /// <typeparam name="T">追加したいコンポーネントの型</typeparam>
69 template<class T>
71 {
72 componentList.AddComponent<T>(this);
73 }
74
75 /// <summary>
76 /// コンポーネントを取得する
77 /// </summary>
78 /// <typeparam name="T">捜索するコンポーネントの型</typeparam>
79 /// <returns>
80 /// <param>成功時 : 発見したコンポーネントのポインタ</param>
81 /// <param>失敗時 : プログラム終了</param>
82 /// </returns>
83 template<class T>
85 {
86 return componentList.GetComponent<T>();
87 }
88
89 /// <summary>
90 /// コンポーネントを取得する
91 /// </summary>
92 /// <typeparam name="T">捜索するコンポーネントの型</typeparam>
93 /// <returns>
94 /// <param>成功時 : 発見したコンポーネントのポインタ</param>
95 /// <param>失敗時 : 何も返さない</param>
96 /// </returns>
97 template<class T>
99 {
100 return componentList.TryGetComponent<T>();
101 }
102
103
105
106 void AddRemoveComponent(string _id);
107
108 void Destroy();
109
110 string ID() const;
111
112 void ID(string _id);
113
114 virtual void Update() {};
115
116 virtual void Start() {};
117
118 //TODO あたまおかしい
119 void MessageComponentList(string _message);
120 };
121
123 {
124 public:
125
127
128
130
131 template <class T>
133 {
134 //TODO GameObjBaseの派生クラスではないなら
135 /*if (!(typeid(GameObjBase).before(typeid(T))))
136 {
137 assert(false);
138 }*/
139
140 string tmpID = idAssignment.GetID();
141
142 //既に存在するIDなら
143 assert(objects.count(tmpID) == 0);
144
145 //生成
146 objects.emplace(tmpID, new T);
147
148 //ID登録
149 objects[tmpID]->ID(tmpID);
150
151 //Startを呼び出す.
152 objects[tmpID]->Start();
153
154 return objects[tmpID];
155 }
156
157
158 /// <summary>
159 /// GameObjct派生クラスを全て破棄する
160 /// </summary>
161 void FinalizeGameObject();
162
163 /// <summary>
164 /// 破棄オブジェクトの登録
165 /// </summary>
166 /// <param name="_id">破棄オブジェクトのID</param>
167 void AddDestroy(string _id);
168
169 /// <summary>
170 /// 破棄オブジェクトの登録.
171 /// </summary>
172 /// <param name="_obj">破棄オブジェクト</param>
173 void AddDestroy(GameObjBase* _obj);
174
175 /// <summary>
176 /// オブジェクトを破棄する.
177 /// </summary>
178 void Destroy();
179
181
182 void MessageUpdate();
183
184 GameObjBase* FindName(string _name)
185 {
186 for (auto& obj : objects)
187 {
188 if (obj.second->GetName() == _name)
189 {
190 return obj.second;
191 }
192 }
193 }
194
195 template<class T>
197 {
198 for (auto& obj : objects)
199 {
200 if (typeid(*obj.second).name() == typeid(T).name())
201 {
202 return *((T*)obj.second);
203 }
204 }
205 }
206
207 template<class T>
208 T& GetGameObject(string _name)
209 {
210 std::vector<GameObjBase*> objs;
211
212 for (auto& obj : objects)
213 {
214 if (typeid(*obj.second).name() == typeid(T).name())
215 {
216 objs.push_back(((T*)obj.second));
217 }
218 }
219
220 for (auto& obj : objs)
221 {
222 if (obj->GetName() == _name)
223 {
224 return *((T*)obj);
225
226 }
227 }
228 }
229
230
231 private:
232 map<string,GameObjBase*> objects;
234 map<string,string>destroyID;
235
236 void InitIDAssignment();
237
238 };
239
241
242
243 }
244
245}
246
GameObjBase()
Definition GameObj.hpp:62
Definition Collision.hpp:25
Definition ComponentBase.hpp:24
Definition ComponentList.hpp:22
Definition GameObj.hpp:28
void Destroy()
Definition GameObj.cpp:19
void AddRemoveComponent(ComponentBase *_com)
Definition GameObj.cpp:8
virtual void Start()
Definition GameObj.hpp:116
virtual void OnCollisionExit(GameObjBase *_other)
Definition GameObj.hpp:55
virtual void OnCollisionStay(GameObjBase *_other)
Definition GameObj.hpp:50
string GetName()
Definition GameObj.hpp:40
string ID() const
Definition GameObj.cpp:27
void MessageComponentList(string _message)
Definition GameObj.cpp:38
bool isActive
Definition GameObj.hpp:34
T & GetComponent()
コンポーネントを取得する
Definition GameObj.hpp:84
GameObjBase()
Definition GameObj.hpp:62
T & TryGetComponent()
コンポーネントを取得する
Definition GameObj.hpp:98
ComponentList componentList
Definition GameObj.hpp:31
virtual void OnCollisionEnter(GameObjBase *_other)
Definition GameObj.hpp:45
virtual ~GameObjBase()
Definition GameObj.hpp:63
string name
Definition GameObj.hpp:36
Transform2D transform
Definition GameObj.hpp:60
void AddComponent()
コンポーネントを追加
Definition GameObj.hpp:70
virtual void Update()
Definition GameObj.hpp:114
string id
Definition GameObj.hpp:35
Definition GameObj.hpp:123
GameObjectFacade()
Definition GameObj.cpp:50
void MessageUpdate()
Definition GameObj.cpp:120
void FinalizeGameObject()
GameObjct派生クラスを全て破棄する
Definition GameObj.cpp:63
void AddDestroy(string _id)
破棄オブジェクトの登録
Definition GameObj.cpp:77
map< string, GameObjBase * > objects
Definition GameObj.hpp:232
GameObjBase * Create()
Definition GameObj.hpp:132
~GameObjectFacade()
Definition GameObj.cpp:55
void MessageRemoveComponent()
Definition GameObj.cpp:112
IDAssignment idAssignment
Definition GameObj.hpp:233
T & GetGameObject(string _name)
Definition GameObj.hpp:208
map< string, string > destroyID
Definition GameObj.hpp:234
T & GetGameObject()
Definition GameObj.hpp:196
GameObjBase * FindName(string _name)
Definition GameObj.hpp:184
void Destroy()
オブジェクトを破棄する.
Definition GameObj.cpp:95
void InitIDAssignment()
Definition GameObj.cpp:128
ID割り当てクラス
Definition IDAssignment.h:15
matrix3x3座標系のラッパークラス
Definition Transform.hpp:16
Definition ComponentBase.hpp:13
GameObjectFacade & GetGameObjectFacade()
Definition GameObj.cpp:45
Definition Accessor.hpp:110