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 <algorithm>
21 #include <string>
22 
23 #include "editor/object_settings.hpp"
24 #include "supertux/game_object_component.hpp"
25 #include "util/gettext.hpp"
26 #include "util/uid.hpp"
27 
28 class DrawingContext;
31 class ReaderMapping;
32 class Writer;
33 
47 {
48  friend class GameObjectManager;
49 
50 public:
51  GameObject();
52  GameObject(const std::string& name);
53  GameObject(const ReaderMapping& reader);
54  virtual ~GameObject();
55 
59  virtual void finish_construction() {}
60 
61  UID get_uid() const { return m_uid; }
62 
68  virtual void update(float dt_sec) = 0;
69 
72  virtual void draw(DrawingContext& context) = 0;
73 
75  void save(Writer& writer);
76  virtual std::string get_class() const { return "game-object"; }
77  virtual std::string get_display_name() const { return _("Unknown object"); }
78 
81  virtual bool is_singleton() const { return false; }
82 
85  virtual bool has_variable_size() const { return false; }
86 
89  virtual bool is_saveable() const { return true; }
90 
93  virtual bool has_settings() const { return is_saveable(); }
94  virtual ObjectSettings get_settings();
95 
96  virtual void after_editor_set() {}
97 
99  bool is_valid() const { return !m_scheduled_for_removal; }
100 
102  void remove_me() { m_scheduled_for_removal = true; }
103 
107 
111 
112  void set_name(const std::string& name) { m_name = name; }
113  const std::string& get_name() const { return m_name; }
114 
115  virtual const std::string get_icon_path() const {
116  return "images/tiles/auxiliary/notile.png";
117  }
118 
120  virtual void stop_looping_sounds() {}
121 
123  virtual void play_looping_sounds() {}
124 
125  template<typename T>
126  T* get_component() {
127  for(auto& component : m_components) {
128  if (T* result = dynamic_cast<T*>(component.get())) {
129  return result;
130  }
131  }
132  return nullptr;
133  }
134 
135  void add_component(std::unique_ptr<GameObjectComponent> component) {
136  m_components.emplace_back(std::move(component));
137  }
138 
139  void remove_component(GameObjectComponent* component) {
140  auto it = std::find_if(m_components.begin(), m_components.end(),
141  [component](const std::unique_ptr<GameObjectComponent>& lhs){
142  return lhs.get() == component;
143  });
144  if (it != m_components.end()) {
145  m_components.erase(it);
146  }
147  }
148 
150  virtual void editor_delete() { remove_me(); }
151 
153  virtual void editor_select() {}
154 
156  virtual void editor_deselect() {}
157 
160  virtual void editor_update() {}
161 
162 private:
163  void set_uid(const UID& uid) { m_uid = uid; }
164 
165 protected:
168  std::string m_name;
169 
170 private:
173  UID m_uid;
174 
176  bool m_scheduled_for_removal;
177 
178  std::vector<std::unique_ptr<GameObjectComponent> > m_components;
179 
180  std::vector<ObjectRemoveListener*> m_remove_listeners;
181 
182 private:
183  GameObject(const GameObject&) = delete;
184  GameObject& operator=(const GameObject&) = delete;
185 };
186 
187 #endif
188 
189 /* 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:81
Definition: writer.hpp:27
void del_remove_listener(ObjectRemoveListener *listener)
unregisters a remove listener, so it will no longer be called if the object gets removed/destroyed ...
Definition: game_object.cpp:65
virtual void editor_deselect()
The object got deselected.
Definition: game_object.hpp:156
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:168
Definition: object_remove_listener.hpp:22
Definition: game_object_manager.hpp:34
virtual void draw(DrawingContext &context)=0
The GameObject should draw itself onto the provided DrawingContext if this function is called...
virtual bool has_variable_size() const
Does this object have variable size (secret area trigger, wind, etc.)
Definition: game_object.hpp:85
void add_remove_listener(ObjectRemoveListener *listener)
registers a remove listener which will be called if the object gets removed/destroyed ...
Definition: game_object.cpp:59
Definition: object_settings.hpp:35
virtual void editor_update()
Called each frame in the editor, used to keep linked objects together (e.g.
Definition: game_object.hpp:160
void remove_me()
schedules this object to be removed at the end of the frame
Definition: game_object.hpp:102
virtual bool has_settings() const
Indicates if get_settings() is implemented.
Definition: game_object.hpp:93
Definition: uid.hpp:37
virtual void editor_select()
The user clicked on the object in the editor and selected it.
Definition: game_object.hpp:153
bool is_valid() const
returns true if the object is not scheduled to be removed yet
Definition: game_object.hpp:99
Base class for all the things that make up Levels&#39; Sectors.
Definition: game_object.hpp:46
virtual void editor_delete()
The editor requested the deletion of the object.
Definition: game_object.hpp:150
virtual void stop_looping_sounds()
stops all looping sounds
Definition: game_object.hpp:120
virtual void update(float dt_sec)=0
This function is called once per frame and allows the object to update it&#39;s state.
virtual void finish_construction()
Called after all objects have been added to the Sector and the Sector is fully constructed.
Definition: game_object.hpp:59
virtual void play_looping_sounds()
continues all looping sounds
Definition: game_object.hpp:123
Definition: game_object_component.hpp:20
void save(Writer &writer)
This function saves the object.
Definition: game_object.cpp:74
Definition: reader_mapping.hpp:31
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:89