game_object_manager.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2018 Ingo Ruhnke <grumbel@gmail.com>
  4. // 2023 Vankata453
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP
  19. #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP
  20. #include <functional>
  21. #include <iostream>
  22. #include <typeindex>
  23. #include <unordered_map>
  24. #include <vector>
  25. #include "supertux/game_object.hpp"
  26. #include "util/uid_generator.hpp"
  27. class DrawingContext;
  28. class MovingObject;
  29. class TileMap;
  30. template<class T> class GameObjectRange;
  31. class GameObjectManager
  32. {
  33. public:
  34. static bool s_draw_solids_only;
  35. private:
  36. struct NameResolveRequest
  37. {
  38. std::string name;
  39. std::function<void (UID)> callback;
  40. };
  41. public:
  42. GameObjectManager(bool undo_tracking = false);
  43. virtual ~GameObjectManager();
  44. /** Queue an object up to be added to the object list */
  45. GameObject& add_object(std::unique_ptr<GameObject> object);
  46. void clear_objects();
  47. template<typename T, typename... Args>
  48. T& add(Args&&... args)
  49. {
  50. auto obj = std::make_unique<T>(std::forward<Args>(args)...);
  51. obj->update_version();
  52. T& obj_ref = *obj;
  53. add_object(std::move(obj));
  54. return obj_ref;
  55. }
  56. /** Add a MovingObject from scripting. */
  57. virtual MovingObject& add_object_scripting(const std::string& class_name, const std::string& name,
  58. const Vector& pos, const std::string& direction,
  59. const std::string& data);
  60. void update(float dt_sec);
  61. void draw(DrawingContext& context);
  62. const std::vector<std::unique_ptr<GameObject> >& get_objects() const;
  63. /** Commit the queued up additions and deletions to the object list */
  64. void flush_game_objects();
  65. float get_width() const;
  66. float get_height() const;
  67. float get_editor_width() const;
  68. float get_editor_height() const;
  69. /** returns the width (in tiles) of a worldmap */
  70. float get_tiles_width() const;
  71. /** returns the height (in tiles) of a worldmap */
  72. float get_tiles_height() const;
  73. /** Hook that is called before an object is added to the vector */
  74. virtual bool before_object_add(GameObject& object) = 0;
  75. /** Hook that is called before an object is removed from the vector */
  76. virtual void before_object_remove(GameObject& object) = 0;
  77. template<class T>
  78. GameObjectRange<T> get_objects_by_type() const
  79. {
  80. return GameObjectRange<T>(*this);
  81. }
  82. const std::vector<GameObject*>&
  83. get_objects_by_type_index(std::type_index type_idx) const
  84. {
  85. auto it = m_objects_by_type_index.find(type_idx);
  86. if (it == m_objects_by_type_index.end()) {
  87. // use a dummy return value to avoid making this method non-const
  88. static std::vector<GameObject*> dummy;
  89. return dummy;
  90. } else {
  91. return it->second;
  92. }
  93. }
  94. template<class T>
  95. T& get_singleton_by_type() const
  96. {
  97. const auto& range = get_objects_by_type<T>();
  98. assert(range.begin() != range.end());
  99. assert(range.begin()->is_singleton());
  100. return *range.begin();
  101. }
  102. template<class T>
  103. T* get_object_by_uid(const UID& uid) const
  104. {
  105. auto it = m_objects_by_uid.find(uid);
  106. if (it == m_objects_by_uid.end())
  107. {
  108. // FIXME: Is this a good idea? Should gameobjects be made
  109. // accessible even when not fully inserted into the manager?
  110. for (auto&& itnew : m_gameobjects_new)
  111. {
  112. if (itnew->get_uid() == uid)
  113. return static_cast<T*>(itnew.get());
  114. }
  115. return nullptr;
  116. }
  117. else
  118. {
  119. #ifdef NDEBUG
  120. return static_cast<T*>(it->second);
  121. #else
  122. // Since uids should be unique, there should be no need to guess
  123. // the type, thus we assert() when the object type is not what
  124. // we expected.
  125. auto ptr = dynamic_cast<T*>(it->second);
  126. assert(ptr != nullptr);
  127. return ptr;
  128. #endif
  129. }
  130. }
  131. /** Move an object to another GameObjectManager. */
  132. void move_object(const UID& uid, GameObjectManager& other);
  133. /** Register a callback to be called once the given name can be
  134. resolved to a UID. Note that this function is only valid in the
  135. construction phase, not during draw() or update() calls, use
  136. get_object_by_uid() instead. */
  137. void request_name_resolve(const std::string& name, std::function<void (UID)> callback);
  138. template<class T>
  139. T* get_object_by_name(const std::string& name) const
  140. {
  141. auto it = m_objects_by_name.find(name);
  142. if (it == m_objects_by_name.end())
  143. {
  144. return nullptr;
  145. }
  146. else
  147. {
  148. return dynamic_cast<T*>(it->second);
  149. }
  150. }
  151. /** Get total number of GameObjects of given type */
  152. template<class T>
  153. int get_object_count(std::function<bool(const T&)> predicate = nullptr) const
  154. {
  155. int total = 0;
  156. for (const auto& obj : m_gameobjects) {
  157. auto object = dynamic_cast<T*>(obj.get());
  158. if (object && (predicate == nullptr || predicate(*object)))
  159. {
  160. total += 1;
  161. }
  162. }
  163. return total;
  164. }
  165. const std::vector<TileMap*>& get_solid_tilemaps() const { return m_solid_tilemaps; }
  166. const std::vector<TileMap*>& get_all_tilemaps() const { return m_all_tilemaps; }
  167. void update_solid(TileMap* solid);
  168. /** Toggle object change tracking for undo/redo. */
  169. void toggle_undo_tracking(bool enabled);
  170. bool undo_tracking_enabled() const { return m_undo_tracking; }
  171. /** Set undo stack size. */
  172. void set_undo_stack_size(int size);
  173. /** Remove old object changes that exceed the undo stack size limit. */
  174. void undo_stack_cleanup();
  175. /** Undo/redo changes to GameObjects in the manager.
  176. Utilized by the Editor. */
  177. void undo();
  178. void redo();
  179. /** Save object change in the undo stack with given data.
  180. Used to save an object's previous state before a change had occurred. */
  181. void save_object_change(GameObject& object, const std::string& data);
  182. /** Clear undo/redo stacks. */
  183. void clear_undo_stack();
  184. /** Indicate if there are any object changes in the undo stack. */
  185. bool has_object_changes() const;
  186. /** Called on editor level save. */
  187. void on_editor_save();
  188. protected:
  189. void update_tilemaps();
  190. void process_resolve_requests();
  191. /** Same as process_resolve_requests(), but those it can't find will be kept in the buffer */
  192. void try_process_resolve_requests();
  193. template<class T>
  194. T* get_object_by_type() const
  195. {
  196. const auto& range = get_objects_by_type<T>();
  197. if (range.begin() == range.end()) {
  198. return nullptr;
  199. } else {
  200. return &*range.begin();
  201. }
  202. }
  203. private:
  204. struct ObjectChange
  205. {
  206. std::string name;
  207. UID uid;
  208. std::string data;
  209. bool creation; // If the change represents an object creation.
  210. };
  211. struct ObjectChanges
  212. {
  213. UID uid;
  214. std::vector<ObjectChange> objects;
  215. };
  216. /** Create object from object change. */
  217. void create_object_from_change(const ObjectChange& change);
  218. /** Process object change on undo/redo. */
  219. void process_object_change(ObjectChange& change);
  220. /** Save object change in the undo stack. */
  221. void save_object_change(GameObject& object, bool creation = false);
  222. void this_before_object_add(GameObject& object);
  223. void this_before_object_remove(GameObject& object);
  224. protected:
  225. /** An initial flush_game_objects() call has been initiated. */
  226. bool m_initialized;
  227. private:
  228. UIDGenerator m_uid_generator;
  229. /** Undo/redo variables */
  230. UIDGenerator m_change_uid_generator;
  231. bool m_undo_tracking;
  232. int m_undo_stack_size;
  233. std::vector<ObjectChanges> m_undo_stack;
  234. std::vector<ObjectChanges> m_redo_stack;
  235. std::vector<ObjectChange> m_pending_change_stack; // Before a flush, any changes go here
  236. UID m_last_saved_change;
  237. std::vector<std::unique_ptr<GameObject>> m_gameobjects;
  238. /** container for newly created objects, they'll be added in flush_game_objects() */
  239. std::vector<std::unique_ptr<GameObject>> m_gameobjects_new;
  240. /** Fast access to solid tilemaps */
  241. std::vector<TileMap*> m_solid_tilemaps;
  242. /** Fast access to all tilemaps */
  243. std::vector<TileMap*> m_all_tilemaps;
  244. std::unordered_map<std::string, GameObject*> m_objects_by_name;
  245. std::unordered_map<UID, GameObject*> m_objects_by_uid;
  246. std::unordered_map<std::type_index, std::vector<GameObject*> > m_objects_by_type_index;
  247. std::vector<NameResolveRequest> m_name_resolve_requests;
  248. private:
  249. GameObjectManager(const GameObjectManager&) = delete;
  250. GameObjectManager& operator=(const GameObjectManager&) = delete;
  251. };
  252. #include "supertux/game_object_iterator.hpp"
  253. #endif
  254. /* EOF */