i6engine  1.0
GameObject.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_API_GAMEOBJECT_H__
26 #define __I6ENGINE_API_GAMEOBJECT_H__
27 
28 #include <map>
29 
32 #include "i6engine/utils/weakPtr.h"
33 
35 
39 
40 #include "boost/function.hpp"
41 
42 namespace i6e {
43 namespace modules {
44  class ObjectMailbox;
45 } /* namespace modules */
46 namespace api {
47 
48  class Component;
49  class GameObject;
50 
51  typedef utils::weakPtr<GameObject> WeakGOPtr;
52  typedef utils::sharedPtr<Component, Component> ComPtr;
54  typedef std::pair<int64_t, ComPtr> component_table_pair_t;
55 
71  public:
78  }
79  GameObject(const int64_t goid, const core::IPKey & ownerNode, uint64_t uuid, const std::string & tpl, const boost::function<ComPtr(const int64_t, const std::string &, const attributeMap &, const WeakGOPtr &)> & f);
80 
84  ~GameObject();
85 
90  inline int64_t getID() const {
91  return _objGoid;
92  }
93 
100  ComPtr getGOC(uint32_t famID) const;
101 
109  ComPtr getGOC(uint32_t famID, const std::string & identifier) const;
110 
111  template<typename T>
112  utils::sharedPtr<T, api::Component> getGOC(uint32_t famID) const;
113 
114  template<typename T>
115  utils::sharedPtr<T, api::Component> getGOC(uint32_t famID, const std::string & identifier) const;
116 
117  template<typename T>
119 
120  template<typename T>
121  utils::sharedPtr<T, api::Component> getGOC(const std::string & identifier) const;
122 
129  ComPtr getGOCID(const int64_t id) const;
130 
134  std::vector<ComPtr> getGOCList() const;
135 
142  void setGOC(const ComPtr & objNewGOC);
143 
147  void clearGOCs();
148 
152  void setDie() const;
153 
157  inline std::string getType() const { return _type; }
158 
162  void setType(const std::string & type);
163 
168  void deleteGOC(uint32_t famID);
169 
175  void deleteGOC(uint32_t famID, const std::string & identifier);
176 
180  void sendCreateMessage() const;
181 
186  void synchronize(std::vector<GameMessage::Ptr> & messages, bool all) const;
187 
191  void sendDeleteMessage() const;
192 
196  inline core::IPKey getOwner() const { return _owner; }
197 
203  void News(const GameMessage::Ptr & msg);
204 
208  void initializeComponents();
209 
213  uint64_t getUUID() const {
214  return _uuid;
215  }
216 
217  void setSelf(const WeakGOPtr & self) {
218  _self = self;
219  }
220 
224  void setFlags(const std::vector<std::string> & flags) {
225  _flags = flags;
226  }
227 
228  std::vector<std::string> getFlags() const {
229  return _flags;
230  }
231 
235  void setSend(bool send) {
236  _send = send;
237  }
238 
239  bool getSend() const {
240  return _send;
241  }
242 
243  private:
244  std::string _type;
245 
249  int64_t _objGoid;
250 
254  component_table_t _objComponents;
255 
256  std::string _template;
257 
261  std::map<std::string, GameMessage::Ptr> _messages;
262 
263  core::IPKey _owner;
264 
265  uint64_t _uuid;
266 
270  boost::function<ComPtr(const int64_t, const std::string &, const attributeMap &, const WeakGOPtr &)> _componentFunc;
271 
272  WeakGOPtr _self;
273 
274  std::vector<std::string> _flags;
275  bool _send;
276 
278  };
279 
280  template<typename T>
282  component_table_t::const_iterator it = _objComponents.find(famID);
283  // GOComponent not found
284  if (it == _objComponents.end()) {
286  }
287 
288  // Return a pointer to the found GOComponent
289  return utils::dynamic_pointer_cast<T>(it->second);
290  }
291 
292  template<typename T>
293  utils::sharedPtr<T, Component> GameObject::getGOC(uint32_t famID, const std::string & identifier) const {
294  component_table_t::const_iterator it = _objComponents.find(famID);
295  // GOComponent not found
296  if (it == _objComponents.end()) {
298  }
299 
300  if (it->second->getIdentifier() != identifier) {
301  for (ComPtr & c : it->second->_subComps) {
302  if (c->getIdentifier() == identifier) {
303  return utils::dynamic_pointer_cast<T>(c);
304  }
305  }
306 
308  }
309 
310  // Return a pointer to the found GOComponent
311  return utils::dynamic_pointer_cast<T>(it->second);
312  }
313 
314  template<typename T>
316  for (const std::pair<int64_t, ComPtr> & com : _objComponents) {
317  auto c = utils::dynamic_pointer_cast<T>(com.second);
318  if (c != nullptr) {
319  return c;
320  }
321  }
322 
323  // Return a pointer to the found GOComponent
325  }
326 
327  template<typename T>
328  utils::sharedPtr<T, Component> GameObject::getGOC(const std::string & identifier) const {
329  for (const std::pair<int64_t, ComPtr> & com : _objComponents) {
330  auto co = utils::dynamic_pointer_cast<T>(com.second);
331  if (co != nullptr) {
332  for (ComPtr & c : co->_subComps) {
333  if (c->getIdentifier() == identifier) {
334  return utils::dynamic_pointer_cast<T>(c);
335  }
336  }
337  }
338  }
339 
340  // Return a pointer to the found GOComponent
342  }
343 
344 } /* namespace api */
345 } /* namespace i6e */
346 
347 #endif /* __I6ENGINE_API_GAMEOBJECT_H__ */
348 
std::string getType() const
Returns the type of the GameObject.
Definition: GameObject.h:157
GameObject()
Constructor for GameObject. Sets the _objGoid with an ID given by GuidServer. The GameObject register...
Definition: GameObject.h:77
void setFlags(const std::vector< std::string > &flags)
stores the flags for which this GO is visible
Definition: GameObject.h:224
std::list< std::pair< KeyType, ValueType > >::iterator find(const KeyType &key)
returns iterator to found entry for key, otherwise end()
Definition: sequence_map.h:112
#define ISIXE_MODULES_API
void setSelf(const WeakGOPtr &self)
Definition: GameObject.h:217
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
void setSend(bool send)
stores whether this object has to be synchronized or not
Definition: GameObject.h:235
std::map< std::string, std::string > attributeMap
Definition: attributeMap.h:42
utils::weakPtr< GameObject > WeakGOPtr
Definition: Component.h:57
std::vector< std::string > getFlags() const
Definition: GameObject.h:228
utils::sequence_map< int64_t, ComPtr > component_table_t
Definition: GameObject.h:53
std::list< std::pair< int64_t, ComPtr > >::const_iterator const_iterator
Definition: sequence_map.h:49
#define ASSERT_THREAD_SAFETY_HEADER
A shared pointer counting references and adds objects being not referenced any more to an internal li...
Definition: sharedPtr.h:50
core::IPKey getOwner() const
returns the owner of this gameObject
Definition: GameObject.h:196
int64_t getID() const
Returns the GUID of the GameObject.
Definition: GameObject.h:90
uint64_t getUUID() const
returns the uuid of this GO
Definition: GameObject.h:213
std::list< std::pair< KeyType, ValueType > >::iterator end()
returns iterator to the end of the list
Definition: sequence_map.h:155
sharedPtr< T1, U > dynamic_pointer_cast(const sharedPtr< T2, U > &)
casts a sharedPtr of dynamic type T2 to dynamic type T1
Definition: sharedPtr.h:245
bool getSend() const
Definition: GameObject.h:239
utils::sharedPtr< T, api::Component > getGOC() const
utils::sharedPtr< Component, Component > ComPtr
Definition: Component.h:52
Class representing a GameObject in the i6engine.
Definition: GameObject.h:70
std::pair< int64_t, ComPtr > component_table_pair_t
Definition: GameObject.h:54