19 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP 20 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_MANAGER_HPP 22 #include "squirrel/exposable_class.hpp" 27 #include <unordered_map> 30 #include "supertux/game_object.hpp" 31 #include "util/uid_generator.hpp" 48 static bool s_draw_solids_only;
51 static void register_class(ssq::VM& vm);
54 struct NameResolveRequest
57 std::function<void (UID)> callback;
64 virtual std::string get_exposed_class_name()
const override {
return "GameObjectManager"; }
70 template<
typename T,
typename... Args>
71 T& add(Args&&... args)
73 auto obj = std::make_unique<T>(std::forward<Args>(args)...);
74 obj->update_version();
80 void update(
float dt_sec);
83 const std::vector<std::unique_ptr<GameObject> >& get_objects()
const;
126 void set_music(
const std::string& music);
139 void add_object(
const std::string& class_name,
const std::string& name,
140 float pos_x,
float pos_y,
const std::string& direction,
141 const std::string& data);
143 float get_width()
const;
144 float get_height()
const;
145 float get_editor_width()
const;
146 float get_editor_height()
const;
166 const std::vector<GameObject*>&
167 get_objects_by_type_index(std::type_index type_idx)
const 169 auto it = m_objects_by_type_index.find(type_idx);
170 if (it == m_objects_by_type_index.end()) {
172 static std::vector<GameObject*> dummy;
180 T& get_singleton_by_type()
const 182 const auto& range = get_objects_by_type<T>();
183 assert(range.begin() != range.end());
184 assert(range.begin()->is_singleton());
185 return *range.begin();
189 T* get_object_by_uid(
const UID& uid)
const 191 auto it = m_objects_by_uid.find(uid);
192 if (it == m_objects_by_uid.end())
196 for (
auto&& itnew : m_gameobjects_new)
198 if (itnew->get_uid() == uid)
199 return static_cast<T*>(itnew.get());
206 return static_cast<T*
>(it->second);
211 auto ptr =
dynamic_cast<T*
>(it->second);
212 assert(ptr !=
nullptr);
228 T* get_object_by_name(
const std::string& name)
const 230 auto it = m_objects_by_name.find(name);
231 if (it == m_objects_by_name.end())
237 return dynamic_cast<T*
>(it->second);
246 for (
const auto& obj : get_objects_by_type_index(
typeid(T))) {
247 auto object =
static_cast<T*
>(obj);
248 if (
object && (predicate ==
nullptr || predicate(*
object)))
256 const std::vector<TileMap*>& get_solid_tilemaps()
const {
return m_solid_tilemaps; }
257 const std::vector<TileMap*>& get_all_tilemaps()
const {
return m_all_tilemaps; }
259 void update_solid(
TileMap* solid);
263 bool undo_tracking_enabled()
const {
return m_undo_tracking; }
292 const Vector& pos,
const std::string& direction,
293 const std::string& data);
295 void update_tilemaps();
297 void process_resolve_requests();
303 T* get_object_by_type()
const 305 const auto& range = get_objects_by_type<T>();
306 if (range.begin() == range.end()) {
309 return &*range.begin();
324 std::vector<ObjectChange> objects;
328 void create_object_from_change(
const ObjectChange& change);
331 void process_object_change(ObjectChange& change);
336 void this_before_object_add(
GameObject&
object);
337 void this_before_object_remove(
GameObject&
object);
348 bool m_undo_tracking;
349 int m_undo_stack_size;
350 std::vector<ObjectChanges> m_undo_stack;
351 std::vector<ObjectChanges> m_redo_stack;
352 std::vector<ObjectChange> m_pending_change_stack;
353 UID m_last_saved_change;
355 std::vector<std::unique_ptr<GameObject>> m_gameobjects;
358 std::vector<std::unique_ptr<GameObject>> m_gameobjects_new;
361 std::vector<TileMap*> m_solid_tilemaps;
364 std::vector<TileMap*> m_all_tilemaps;
366 std::unordered_map<std::string, GameObject*> m_objects_by_name;
367 std::unordered_map<UID, GameObject*> m_objects_by_uid;
368 std::unordered_map<std::type_index, std::vector<GameObject*> > m_objects_by_type_index;
370 std::vector<NameResolveRequest> m_name_resolve_requests;
377 #include "supertux/game_object_iterator.hpp" void on_editor_save()
Called on editor level save.
Definition: game_object_manager.cpp:374
void set_music(const std::string &music)
Sets the sector's music.
Definition: game_object_manager.cpp:554
float get_ambient_green() const
Returns the green channel of the ambient light color.
Definition: game_object_manager.cpp:542
void try_process_resolve_requests()
Same as process_resolve_requests(), but those it can't find will be kept in the buffer.
Definition: game_object_manager.cpp:94
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:96
Definition: uid_generator.hpp:22
bool has_object_changes() const
Indicate if there are any object changes in the undo stack.
Definition: game_object_manager.cpp:463
float get_tiles_height() const
returns the height (in tiles) of a worldmap
Definition: game_object_manager.cpp:612
void set_undo_stack_size(int size)
Set undo stack size.
Definition: game_object_manager.cpp:355
void undo()
Undo/redo changes to GameObjects in the manager.
Definition: game_object_manager.cpp:380
float get_ambient_blue() const
Returns the blue channel of the ambient light color.
Definition: game_object_manager.cpp:548
This class provides basic controlling functions for a sector.
Definition: game_object_manager.hpp:45
GameObject & add_object(std::unique_ptr< GameObject > object)
Queue an object up to be added to the object list.
Definition: game_object_manager.cpp:128
virtual MovingObject & add_object_scripting(const std::string &class_name, const std::string &name, const Vector &pos, const std::string &direction, const std::string &data)
Add a MovingObject from scripting.
Definition: game_object_manager.cpp:172
virtual void before_object_remove(GameObject &object)=0
Hook that is called before an object is removed from the vector.
void set_ambient_light(float red, float green, float blue)
Sets the sector's ambient light to the specified color.
Definition: game_object_manager.cpp:530
void clear_undo_stack()
Clear undo/redo stacks.
Definition: game_object_manager.cpp:455
bool m_initialized
An initial flush_game_objects() call has been initiated.
Definition: game_object_manager.hpp:341
void flush_game_objects()
Commit the queued up additions and deletions to the object list.
Definition: game_object_manager.cpp:240
void move_object(const UID &uid, GameObjectManager &other)
Move an object to another GameObjectManager.
Definition: game_object_manager.cpp:319
void save_object_change(GameObject &object, const std::string &data)
Save object change in the undo stack with given data.
Definition: game_object_manager.cpp:448
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:243
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
Represents a class, which can be exposed to scripting.
Definition: exposable_class.hpp:25
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
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 resolved to a UID.
Definition: game_object_manager.cpp:64
void undo_stack_cleanup()
Remove old object changes that exceed the undo stack size limit.
Definition: game_object_manager.cpp:365
This class is responsible for managing an array of tiles.
Definition: tilemap.hpp:49
void toggle_undo_tracking(bool enabled)
Toggle object change tracking for undo/redo.
Definition: game_object_manager.cpp:345
float get_ambient_red() const
Returns the red channel of the ambient light color.
Definition: game_object_manager.cpp:536
void fade_to_ambient_light(float red, float green, float blue, float fadetime)
Fades to a specified ambient light color in ""fadetime"" seconds.
Definition: game_object_manager.cpp:524
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:600