game_object.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
  18. #include <algorithm>
  19. #include <string>
  20. #include <vector>
  21. #include "editor/object_settings.hpp"
  22. #include "supertux/game_object_component.hpp"
  23. #include "util/fade_helper.hpp"
  24. #include "util/gettext.hpp"
  25. #include "util/uid.hpp"
  26. class DrawingContext;
  27. class GameObjectComponent;
  28. class GameObjectManager;
  29. class ObjectRemoveListener;
  30. class ReaderMapping;
  31. class Writer;
  32. struct GameObjectType
  33. {
  34. const std::string id;
  35. const std::string name;
  36. };
  37. typedef std::vector<GameObjectType> GameObjectTypes;
  38. /**
  39. Base class for all the things that make up Levels' Sectors.
  40. Each sector of a level will hold a list of active GameObject while the
  41. game is played.
  42. This class is responsible for:
  43. - Updating and Drawing the object. This should happen in the update() and
  44. draw() functions. Both are called once per frame.
  45. - Providing a safe way to remove the object by calling the remove_me
  46. functions.
  47. */
  48. class GameObject
  49. {
  50. friend class GameObjectManager;
  51. public:
  52. GameObject();
  53. GameObject(const std::string& name);
  54. GameObject(const ReaderMapping& reader);
  55. virtual ~GameObject();
  56. /** Called after all objects have been added to the Sector and the
  57. Sector is fully constructed. If objects refer to other objects
  58. by name, those connection can be resolved here. */
  59. virtual void finish_construction() {}
  60. UID get_uid() const { return m_uid; }
  61. /** This function is called once per frame and allows the object to
  62. update it's state. The dt_sec is the time that has passed since
  63. the last frame in seconds and should be the base for all timed
  64. calculations (don't use SDL_GetTicks directly as this will fail
  65. in pause mode). This function is not called in the Editor. */
  66. virtual void update(float dt_sec) = 0;
  67. /** The GameObject should draw itself onto the provided
  68. DrawingContext if this function is called. */
  69. virtual void draw(DrawingContext& context) = 0;
  70. /** This function saves the object. Editor will use that. */
  71. virtual void save(Writer& writer);
  72. std::string save();
  73. virtual std::string get_class_name() const { return "game-object"; }
  74. virtual std::string get_display_name() const { return _("Unknown object"); }
  75. /** Version checking/updating, patch information */
  76. virtual std::vector<std::string> get_patches() const;
  77. int get_version() const { return m_version; }
  78. int get_latest_version() const;
  79. bool is_up_to_date() const;
  80. virtual void update_version();
  81. /** If true only a single object of this type is allowed in a
  82. given GameObjectManager */
  83. virtual bool is_singleton() const { return false; }
  84. /** Does this object have variable size
  85. (secret area trigger, wind, etc.) */
  86. virtual bool has_variable_size() const { return false; }
  87. /** Indicates if the object will be saved. If false, the object will
  88. be skipped on saving and can't be cloned in the editor. */
  89. virtual bool is_saveable() const { return true; }
  90. /** Indicates if the object's state should be tracked.
  91. If false, load_state() and save_state() calls would not do anything. */
  92. virtual bool track_state() const { return true; }
  93. /** Indicates if the object should be added at the beginning of the object list. */
  94. virtual bool has_object_manager_priority() const { return false; }
  95. /** Indicates if get_settings() is implemented. If true the editor
  96. will display Tip and ObjectMenu. */
  97. virtual bool has_settings() const { return is_saveable(); }
  98. virtual ObjectSettings get_settings();
  99. /** Get all types of the object, if available. **/
  100. virtual GameObjectTypes get_types() const;
  101. int get_type() const { return m_type; }
  102. virtual void after_editor_set();
  103. /** When level is flipped vertically */
  104. virtual void on_flip(float height) {}
  105. /** schedules this object to be removed at the end of the frame */
  106. virtual void remove_me() { m_scheduled_for_removal = true; }
  107. /** returns true if the object is not scheduled to be removed yet */
  108. bool is_valid() const { return !m_scheduled_for_removal; }
  109. /** registers a remove listener which will be called if the object
  110. gets removed/destroyed */
  111. void add_remove_listener(ObjectRemoveListener* listener);
  112. /** unregisters a remove listener, so it will no longer be called if
  113. the object gets removed/destroyed */
  114. void del_remove_listener(ObjectRemoveListener* listener);
  115. void set_name(const std::string& name) { m_name = name; }
  116. const std::string& get_name() const { return m_name; }
  117. virtual const std::string get_icon_path() const {
  118. return "images/tiles/auxiliary/notile.png";
  119. }
  120. /** stops all looping sounds */
  121. virtual void stop_looping_sounds() {}
  122. /** continues all looping sounds */
  123. virtual void play_looping_sounds() {}
  124. template<typename T>
  125. T* get_component() {
  126. for(auto& component : m_components) {
  127. if (T* result = dynamic_cast<T*>(component.get())) {
  128. return result;
  129. }
  130. }
  131. return nullptr;
  132. }
  133. void add_component(std::unique_ptr<GameObjectComponent> component) {
  134. m_components.emplace_back(std::move(component));
  135. }
  136. void remove_component(GameObjectComponent* component) {
  137. auto it = std::find_if(m_components.begin(), m_components.end(),
  138. [component](const std::unique_ptr<GameObjectComponent>& lhs){
  139. return lhs.get() == component;
  140. });
  141. if (it != m_components.end()) {
  142. m_components.erase(it);
  143. }
  144. }
  145. /** Save/check the current state of the object. */
  146. virtual void save_state();
  147. virtual void check_state();
  148. /** The editor requested the deletion of the object */
  149. virtual void editor_delete() { remove_me(); }
  150. /** The user clicked on the object in the editor and selected it*/
  151. virtual void editor_select() {}
  152. /** The object got deselected */
  153. virtual void editor_deselect() {}
  154. /** Called each frame in the editor, used to keep linked objects
  155. together (e.g. platform on a path) */
  156. virtual void editor_update() {}
  157. GameObjectManager* get_parent() const { return m_parent; }
  158. protected:
  159. /** Parse object type. **/
  160. void parse_type(const ReaderMapping& reader);
  161. /** When the type has been changed from the editor. **/
  162. enum TypeChange { INITIAL = -1 }; // "old_type < 0" indicates initial call
  163. virtual void on_type_change(int old_type) {}
  164. /** Conversion between type ID and value. **/
  165. int type_id_to_value(const std::string& id) const;
  166. std::string type_value_to_id(int value) const;
  167. private:
  168. void set_uid(const UID& uid) { m_uid = uid; }
  169. private:
  170. /** The parent GameObjectManager. Set by the manager itself. */
  171. GameObjectManager* m_parent;
  172. protected:
  173. /** a name for the gameobject, this is mostly a hint for scripts and
  174. for debugging, don't rely on names being set or being unique */
  175. std::string m_name;
  176. /** Type of the GameObject. Used to provide special functionality,
  177. based on the child object. */
  178. int m_type;
  179. /** Fade Helpers are for easing/fading script functions */
  180. std::vector<std::unique_ptr<FadeHelper>> m_fade_helpers;
  181. /** Track the following creation/deletion of this object for undo.
  182. If track_state() returns false, this object would not be tracked,
  183. regardless of the value of this variable. */
  184. bool m_track_undo;
  185. private:
  186. /** The object's type at the time of the last get_settings() call.
  187. Used to check if the type has changed. **/
  188. int m_previous_type;
  189. /** Indicates the object's version. By default, this is equal to 1.
  190. Useful for retaining retro-compatibility for objects, whilst allowing for
  191. updated behaviour in newer levels.
  192. The version of an object can be updated from the editor. */
  193. int m_version;
  194. /** A unique id for the object to safely refer to it. This will be
  195. set by the GameObjectManager. */
  196. UID m_uid;
  197. /** this flag indicates if the object should be removed at the end of the frame */
  198. bool m_scheduled_for_removal;
  199. /** The object's data at the time of the last state save.
  200. Used to check for changes that may have occured. */
  201. std::string m_last_state;
  202. std::vector<std::unique_ptr<GameObjectComponent> > m_components;
  203. std::vector<ObjectRemoveListener*> m_remove_listeners;
  204. private:
  205. GameObject(const GameObject&) = delete;
  206. GameObject& operator=(const GameObject&) = delete;
  207. };
  208. #endif
  209. /* EOF */