supertux
game_object.hpp
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 
17 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
19 
20 #include "squirrel/exposable_class.hpp"
21 
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 #include <typeindex>
26 
27 #include "editor/object_settings.hpp"
28 #include "supertux/game_object_component.hpp"
29 #include "util/fade_helper.hpp"
30 #include "util/gettext.hpp"
31 #include "util/uid.hpp"
32 
33 class DrawingContext;
35 class GameObjectManager;
37 class ReaderMapping;
38 class Writer;
39 
40 namespace ssq {
41 class VM;
42 } // namespace ssq
43 
45 {
46  const std::string id;
47  const std::string name;
48 };
49 typedef std::vector<GameObjectType> GameObjectTypes;
50 
58 {
59  std::vector<std::type_index> types;
60 
61  GameObjectClasses& add(const std::type_info& info)
62  {
63  std::type_index idx(info);
64  types.push_back(idx);
65  return *this;
66  }
67 };
68 
83 class GameObject : public ExposableClass
84 {
85  friend class GameObjectManager;
86 
87 public:
88  static void register_class(ssq::VM& vm);
89 
90 public:
91  GameObject();
92  GameObject(const std::string& name);
93  GameObject(const ReaderMapping& reader);
94  virtual ~GameObject() override;
95 
99  virtual void finish_construction() {}
100 
101  UID get_uid() const { return m_uid; }
102 
108  virtual void update(float dt_sec) = 0;
109 
112  virtual void draw(DrawingContext& context) = 0;
113 
115  virtual void save(Writer& writer);
116  std::string save();
117  virtual std::string get_class_name() const { return "game-object"; }
118  virtual std::string get_exposed_class_name() const override { return "GameObject"; }
123  virtual std::string get_display_name() const { return _("Unknown object"); }
126  virtual GameObjectClasses get_class_types() const;
127 
129  virtual std::vector<std::string> get_patches() const;
130  virtual void update_version();
135  int get_version() const;
140  int get_latest_version() const;
145  bool is_up_to_date() const;
146 
149  virtual bool is_singleton() const { return false; }
150 
153  virtual bool has_variable_size() const { return false; }
154 
157  virtual bool is_saveable() const { return true; }
158 
161  virtual bool track_state() const { return true; }
162 
164  virtual bool has_object_manager_priority() const { return false; }
165 
168  virtual bool has_settings() const { return is_saveable(); }
169  virtual ObjectSettings get_settings();
170 
172  virtual GameObjectTypes get_types() const;
177  int get_type() const;
178 
179  virtual void after_editor_set();
180 
182  virtual void on_flip(float height) {}
183 
185  virtual void remove_me() { m_scheduled_for_removal = true; }
186 
188  bool is_valid() const { return !m_scheduled_for_removal; }
189 
192  void add_remove_listener(ObjectRemoveListener* listener);
193 
196  void del_remove_listener(ObjectRemoveListener* listener);
197 
198  void set_name(const std::string& name) { m_name = name; }
203  const std::string& get_name() const;
204 
205  virtual const std::string get_icon_path() const {
206  return "images/tiles/auxiliary/notile.png";
207  }
208 
210  virtual void stop_looping_sounds() {}
211 
213  virtual void play_looping_sounds() {}
214 
215  template<typename T>
216  T* get_component() {
217  for(auto& component : m_components) {
218  if (T* result = dynamic_cast<T*>(component.get())) {
219  return result;
220  }
221  }
222  return nullptr;
223  }
224 
225  void add_component(std::unique_ptr<GameObjectComponent> component) {
226  m_components.emplace_back(std::move(component));
227  }
228 
229  void remove_component(GameObjectComponent* component) {
230  auto it = std::find_if(m_components.begin(), m_components.end(),
231  [component](const std::unique_ptr<GameObjectComponent>& lhs){
232  return lhs.get() == component;
233  });
234  if (it != m_components.end()) {
235  m_components.erase(it);
236  }
237  }
238 
240  virtual void save_state();
241  virtual void check_state();
242 
244  virtual void editor_delete() { remove_me(); }
245 
247  virtual void editor_select() {}
248 
250  virtual void editor_deselect() {}
251 
254  virtual void editor_update() {}
255 
256  GameObjectManager* get_parent() const { return m_parent; }
257 
258 protected:
260  void parse_type(const ReaderMapping& reader);
261 
263  enum TypeChange { INITIAL = -1 }; // "old_type < 0" indicates initial call
264  virtual void on_type_change(int old_type) {}
265 
267  int type_id_to_value(const std::string& id) const;
268  std::string type_value_to_id(int value) const;
269 
270 private:
271  void set_uid(const UID& uid) { m_uid = uid; }
272 
273 private:
275  GameObjectManager* m_parent;
276 
277 protected:
280  std::string m_name;
281 
284  int m_type;
285 
287  std::vector<std::unique_ptr<FadeHelper>> m_fade_helpers;
288 
293 
294 private:
297  int m_previous_type;
298 
303  int m_version;
304 
307  UID m_uid;
308 
310  bool m_scheduled_for_removal;
311 
314  std::string m_last_state;
315 
316  std::vector<std::unique_ptr<GameObjectComponent> > m_components;
317 
318  std::vector<ObjectRemoveListener*> m_remove_listeners;
319 
320 private:
321  GameObject(const GameObject&) = delete;
322  GameObject& operator=(const GameObject&) = delete;
323 };
324 
325 #endif
326 
327 /* EOF */
virtual bool is_singleton() const
If true only a single object of this type is allowed in a given GameObjectManager.
Definition: game_object.hpp:149
std::vector< std::unique_ptr< FadeHelper > > m_fade_helpers
Fade Helpers are for easing/fading script functions.
Definition: game_object.hpp:287
virtual void editor_deselect()
The object got deselected.
Definition: game_object.hpp:250
virtual void on_flip(float height)
When level is flipped vertically.
Definition: game_object.hpp:182
virtual void remove_me()
schedules this object to be removed at the end of the frame
Definition: game_object.hpp:185
std::string m_name
a name for the gameobject, this is mostly a hint for scripts and for debugging, don&#39;t rely on names b...
Definition: game_object.hpp:280
virtual bool has_object_manager_priority() const
Indicates if the object should be added at the beginning of the object list.
Definition: game_object.hpp:164
Definition: object_remove_listener.hpp:22
This class provides basic controlling functions for a sector.
Definition: game_object_manager.hpp:45
virtual bool has_variable_size() const
Does this object have variable size (secret area trigger, wind, etc.)
Definition: game_object.hpp:153
Definition: object_settings.hpp:39
virtual void editor_update()
Called each frame in the editor, used to keep linked objects together (e.g.
Definition: game_object.hpp:254
virtual bool has_settings() const
Indicates if get_settings() is implemented.
Definition: game_object.hpp:168
Definition: path_object.hpp:28
virtual bool track_state() const
Indicates if the object&#39;s state should be tracked.
Definition: game_object.hpp:161
bool m_track_undo
Track the following creation/deletion of this object for undo.
Definition: game_object.hpp:292
Definition: uid.hpp:37
virtual void editor_select()
The user clicked on the object in the editor and selected it.
Definition: game_object.hpp:247
bool is_valid() const
returns true if the object is not scheduled to be removed yet
Definition: game_object.hpp:188
int m_type
Type of the GameObject.
Definition: game_object.hpp:284
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
virtual void editor_delete()
The editor requested the deletion of the object.
Definition: game_object.hpp:244
virtual void stop_looping_sounds()
stops all looping sounds
Definition: game_object.hpp:210
virtual void finish_construction()
Called after all objects have been added to the Sector and the Sector is fully constructed.
Definition: game_object.hpp:99
virtual std::string get_display_name() const
Returns the display name of the object, translated to the user&#39;s locale.
Definition: game_object.hpp:123
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
virtual void play_looping_sounds()
continues all looping sounds
Definition: game_object.hpp:213
Definition: game_object_component.hpp:20
TypeChange
When the type has been changed from the editor.
Definition: game_object.hpp:263
Definition: reader_mapping.hpp:32
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
virtual bool is_saveable() const
Indicates if the object will be saved.
Definition: game_object.hpp:157
Definition: game_object.hpp:44
Definition: writer.cpp:23