i6engine  1.0
ObjectFacade.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_OBJECTFACADE_H__
26 #define __I6ENGINE_API_OBJECTFACADE_H__
27 
28 #include <condition_variable>
29 #include <map>
30 #include <mutex>
31 #include <unordered_map>
32 
33 #include "i6engine/utils/weakPtr.h"
34 
37 
38 namespace i6e {
39 namespace api {
40 
41  class Component;
42  class GameObject;
43 
44  typedef utils::sharedPtr<GameObject, GameObject> GOPtr;
45  typedef utils::weakPtr<Component> WeakComPtr;
46 
48  public:
52  ObjectFacade();
53 
54  ~ObjectFacade();
55 
62  GOPtr getObject(const int64_t guid) const;
63 
68  std::unordered_map<int64_t, GOPtr> getGOMap() const;
69 
75  std::vector<GOPtr> getAllObjectsOfType(const std::string & types) const;
76 
82  std::vector<GOPtr> getAllObjectsOfType(const std::vector<std::string> & types) const;
83 
87  void deleteAllObjectsOfType(const std::string & type) const;
88 
95  void createObject(const std::string & gTemplate, const objects::GOTemplate & tmpl, uint64_t uuid, const bool sender) const;
96 
100  void cleanUpAll() const;
101 
105  void loadLevel(const std::string & file, const std::string & flags) const {
106  loadLevel(file, flags, "", [](uint16_t) {});
107  }
108 
112  void loadLevel(const std::string & file, const std::string & flags, const std::string & resourcesFile) const {
113  loadLevel(file, flags, resourcesFile, [](uint16_t) {});
114  }
115 
121  void loadLevel(const std::string & file, const std::string & flags, const std::function<void(uint16_t)> & callback) const {
122  loadLevel(file, flags, "", callback);
123  }
124 
130  void loadLevel(const std::string & file, const std::string & flags, const std::string & resourcesFile, const std::function<void(uint16_t)> & callback) const;
131 
135  void addTicker(const WeakComPtr & c);
136 
140  void removeTicker(int64_t c);
141 
145  uint32_t getFrameTime() const;
146 
153  void registerCTemplate(const std::string & GOCType, const createGOCCallback & fn) const;
154 
164  void createGO(const std::string & gTemplate, const objects::GOTemplate & tmpl, uint64_t uuid, const bool sender, const boost::function<void(GOPtr)> & func) const;
165 
173  void createComponent(int64_t goid, int64_t coid, const std::string & component, const attributeMap & params) const;
174 
183  void createComponentCallback(int64_t goid, int64_t coid, const std::string & component, const attributeMap & params, const boost::function<void(ComPtr)> & callback) const;
184 
189  void updateGOMap(const std::unordered_map<int64_t, GOPtr> & GOMap);
190 
194  void setFrametime(uint32_t frametime) { _frameTime = frametime; }
195 
203  void sendConditionalMessage(const GameMessage::Ptr & m, const boost::function<bool(const GOPtr &)> & f, bool sync, uint32_t compFamID) const;
204 
208  void registerNotifyCallback(const boost::function<void(int64_t)> & f);
209 
213  void notifyNewID(int64_t id);
214 
218  void registerAddTickerCallback(const boost::function<void(const WeakComPtr &)> & func);
219 
223  void registerRemoveTickerCallback(const boost::function<void(int64_t)> & func);
224 
228  void resetSubSystem() const;
229 
233  void pause() const;
234 
238  void unpause() const;
239 
244  void setGOTemplates(const std::map<std::string, objects::GOTemplate> & templates) {
245  _templateList = templates;
246  }
247 
251  std::map<std::string, objects::GOTemplate> getGOTemplates() const {
252  return _templateList;
253  }
254 
259  void allowComponentsTicking(bool allowTicking) const;
260 
261  private:
265  std::unordered_map<int64_t, GOPtr> _GOMap;
266 
270  mutable std::mutex _lock;
271 
275  uint32_t _frameTime;
276 
277  boost::function<void(int64_t)> _notify;
278 
279  boost::function<void(const WeakComPtr &)> _addTicker;
280  boost::function<void(int64_t)> _removeTicker;
281 
282  mutable std::mutex _loadLevelLock;
283  mutable std::condition_variable _loadLevelCondVar;
284 
285  std::map<std::string, objects::GOTemplate> _templateList;
286 
290  ObjectFacade(const ObjectFacade &) = delete;
291 
295  ObjectFacade & operator=(const ObjectFacade &) = delete;
296  };
297 
298 } /* namespace api */
299 } /* namespace i6e */
300 
301 #endif /* __I6ENGINE_API_OBJECTFACADE_H__ */
302 
utils::sharedPtr< GameObject, GameObject > GOPtr
Definition: Component.h:55
void setGOTemplates(const std::map< std::string, objects::GOTemplate > &templates)
sets all available GOTemplates don't use, only for Object Subsystem
Definition: ObjectFacade.h:244
#define ISIXE_MODULES_API
void setFrametime(uint32_t frametime)
sets the frametime for this subsystem in the facade, so you can see it from outside the subsystem ...
Definition: ObjectFacade.h:194
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
std::map< std::string, std::string > attributeMap
Definition: attributeMap.h:42
A shared pointer counting references and adds objects being not referenced any more to an internal li...
Definition: sharedPtr.h:50
internal representation of all GameObject templates
Definition: ObjectConfig.h:104
utils::weakPtr< Component > WeakComPtr
Definition: Component.h:56
boost::function< ComPtr(const int64_t, const attributeMap &)> createGOCCallback
Definition: Component.h:58
std::map< std::string, objects::GOTemplate > getGOTemplates() const
returns all available GOTemplates
Definition: ObjectFacade.h:251
void loadLevel(const std::string &file, const std::string &flags, const std::function< void(uint16_t)> &callback) const
loads a level from an xml file only using the objects with given flags calls callback when progress c...
Definition: ObjectFacade.h:121
void loadLevel(const std::string &file, const std::string &flags, const std::string &resourcesFile) const
loads a level from an xml file only using the objects with given flags and preloads resources specifi...
Definition: ObjectFacade.h:112
void loadLevel(const std::string &file, const std::string &flags) const
loads a level from an xml file only using the objects with given flags
Definition: ObjectFacade.h:105