18 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP 19 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP 23 #include <unordered_map> 26 #include "supertux/game_object.hpp" 27 #include "util/uid_generator.hpp" 37 static bool s_draw_solids_only;
40 struct NameResolveRequest
43 std::function<void (UID)> callback;
54 template<
typename T,
typename... Args>
55 T& add(Args&&... args)
57 auto obj = std::make_unique<T>(std::forward<Args>(args)...);
63 void update(
float dt_sec);
66 const std::vector<std::unique_ptr<GameObject> >& get_objects()
const;
71 float get_width()
const;
72 float get_height()
const;
92 const std::vector<GameObject*>&
93 get_objects_by_type_index(std::type_index type_idx)
const 95 auto it = m_objects_by_type_index.find(type_idx);
96 if (it == m_objects_by_type_index.end()) {
98 static std::vector<GameObject*> dummy;
106 T& get_singleton_by_type()
const 108 const auto& range = get_objects_by_type<T>();
109 assert(range.begin() != range.end());
110 assert(range.begin()->is_singleton());
111 return *range.begin();
115 T* get_object_by_uid(
const UID& uid)
const 117 auto it = m_objects_by_uid.find(uid);
118 if (it == m_objects_by_uid.end())
122 for (
auto&& itnew : m_gameobjects_new)
124 if (itnew->get_uid() == uid)
125 return static_cast<T*>(itnew.get());
132 return static_cast<T*
>(it->second);
137 auto ptr =
dynamic_cast<T*
>(it->second);
138 assert(ptr !=
nullptr);
151 T* get_object_by_name(
const std::string& name)
const 153 auto it = m_objects_by_name.find(name);
154 if (it == m_objects_by_name.end())
160 return dynamic_cast<T*
>(it->second);
169 for (
const auto& obj : m_gameobjects) {
170 auto object =
dynamic_cast<T*
>(obj.get());
171 if (
object && (predicate ==
nullptr || predicate(*
object)))
179 const std::vector<TileMap*>& get_solid_tilemaps()
const {
return m_solid_tilemaps; }
182 void process_resolve_requests();
185 T* get_object_by_type()
const 187 const auto& range = get_objects_by_type<T>();
188 if (range.begin() == range.end()) {
191 return &*range.begin();
196 void this_before_object_add(
GameObject&
object);
197 void this_before_object_remove(
GameObject&
object);
202 std::vector<std::unique_ptr<GameObject>> m_gameobjects;
205 std::vector<std::unique_ptr<GameObject>> m_gameobjects_new;
208 std::vector<TileMap*> m_solid_tilemaps;
210 std::unordered_map<std::string, GameObject*> m_objects_by_name;
211 std::unordered_map<UID, GameObject*> m_objects_by_uid;
212 std::unordered_map<std::type_index, std::vector<GameObject*> > m_objects_by_type_index;
214 std::vector<NameResolveRequest> m_name_resolve_requests;
221 #include "supertux/game_object_iterator.hpp" virtual bool before_object_add(GameObject &object)=0
Hook that is called before an object is added to the vector.
Definition: game_object_iterator.hpp:110
Definition: uid_generator.hpp:22
float get_tiles_height() const
returns the height (in tiles) of a worldmap
Definition: game_object_manager.cpp:267
Definition: game_object_manager.hpp:34
GameObject & add_object(std::unique_ptr< GameObject > object)
Queue an object up to be added to the object list.
Definition: game_object_manager.cpp:79
virtual void before_object_remove(GameObject &object)=0
Hook that is called before an object is removed from the vector.
void flush_game_objects()
Commit the queued up additions and deletions to the object list.
Definition: game_object_manager.cpp:144
int get_object_count(std::function< bool(const T &)> predicate=nullptr) const
Get total number of GameObjects of given type.
Definition: game_object_manager.hpp:166
Base class for all the things that make up Levels' Sectors.
Definition: game_object.hpp:46
void request_name_resolve(const std::string &name, std::function< void(UID)> callback)
Register a callback to be called once the given name can be resolsed to a UID.
Definition: game_object_manager.cpp:46
This class is responsible for drawing the level tiles.
Definition: tilemap.hpp:39
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
float get_tiles_width() const
returns the width (in tiles) of a worldmap
Definition: game_object_manager.cpp:256