supertux
moving_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_MOVING_OBJECT_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_MOVING_OBJECT_HPP
19 
20 #include "collision/collision_hit.hpp"
21 #include "collision/collision_object.hpp"
22 #include "collision/collision_listener.hpp"
23 #include "math/rectf.hpp"
24 #include "supertux/game_object.hpp"
25 
26 class Dispenser;
27 class Sector;
28 
35 class MovingObject : public GameObject,
36  public CollisionListener
37 {
38  friend class ResizeMarker;
39  friend class Sector;
40  friend class CollisionSystem;
41 
42 public:
43  static void register_class(ssq::VM& vm);
44 
45 public:
46  MovingObject();
47  MovingObject(const ReaderMapping& reader);
48  ~MovingObject() override;
49  virtual GameObjectClasses get_class_types() const override { return GameObject::get_class_types().add(typeid(CollisionListener)).add(typeid(MovingObject)); }
50 
51  virtual void collision_solid(const CollisionHit& /*hit*/) override
52  {
53  }
54 
55  virtual bool collides(GameObject& /*other*/, const CollisionHit& /*hit*/) const override
56  {
57  return true;
58  }
59 
60  virtual void collision_tile(uint32_t /*tile_attributes*/) override
61  {
62  }
63 
64  virtual void set_pos(const Vector& pos)
65  {
66  m_col.set_pos(pos);
67  }
68 
69  virtual void move_to(const Vector& pos)
70  {
71  m_col.move_to(pos);
72  }
73  virtual void move(const Vector& dist)
74  {
75  m_col.m_bbox.move(dist);
76  }
77 
78  virtual bool listener_is_valid() const override { return is_valid(); }
79 
80  Vector get_pos() const
81  {
82  return m_col.m_bbox.p1();
83  }
84 
85  const Rectf& get_bbox() const
86  {
87  return m_col.m_bbox;
88  }
89 
90  const Vector& get_movement() const
91  {
92  return m_col.get_movement();
93  }
94 
95  CollisionGroup get_group() const
96  {
97  return m_col.m_group;
98  }
99 
100  CollisionObject* get_collision_object() {
101  return &m_col;
102  }
103 
104  const CollisionObject* get_collision_object() const {
105  return &m_col;
106  }
107 
108  void set_parent_dispenser(Dispenser* dispenser);
109  Dispenser* get_parent_dispenser() const { return m_parent_dispenser; }
110 
111  static std::string class_name() { return "moving-object"; }
112  virtual std::string get_class_name() const override { return class_name(); }
113  virtual std::string get_exposed_class_name() const override { return "MovingObject"; }
114  virtual ObjectSettings get_settings() override;
115 
116  virtual void editor_select() override;
117 
118  virtual void on_flip(float height) override;
119 
120  virtual int get_layer() const = 0;
121 
126  float get_x() const;
131  float get_y() const;
138  void set_pos(float x, float y);
145  void move(float x, float y);
146 
151  float get_width() const;
156  float get_height() const;
157 
158 protected:
159  void set_group(CollisionGroup group)
160  {
161  m_col.m_group = group;
162  }
163 
164 protected:
165  CollisionObject m_col;
166 
167  Dispenser* m_parent_dispenser;
168 
169 private:
170  MovingObject(const MovingObject&) = delete;
171  MovingObject& operator=(const MovingObject&) = delete;
172 };
173 
174 #endif
175 
176 /* EOF */
CollisionGroup m_group
The collision group.
Definition: collision_object.hpp:153
virtual void collision_solid(const CollisionHit &) override
this function is called when the object collided with something solid
Definition: moving_object.hpp:51
Definition: collision_listener.hpp:23
Definition: resize_marker.hpp:24
float get_y() const
Returns the object&#39;s Y coordinate.
Definition: moving_object.cpp:91
float get_width() const
Returns the object&#39;s hitbox width.
Definition: moving_object.cpp:109
void move_to(const Vector &pos)
moves entire object to a specific position, including all points those the object has...
Definition: collision_object.hpp:109
Definition: object_settings.hpp:39
Definition: collision_system.hpp:36
Rectf m_bbox
The bounding box of the object (as used for collision detection, this isn&#39;t necessarily the bounding ...
Definition: collision_object.hpp:150
virtual void on_flip(float height) override
When level is flipped vertically.
Definition: moving_object.cpp:134
Represents one of (potentially) multiple, separate parts of a Level.
Definition: sector.hpp:61
A ""Dispenser"" that was given a name can be controlled by scripts.
Definition: dispenser.hpp:30
void set_pos(const Vector &pos)
places the moving object at a specific position.
Definition: collision_object.hpp:90
Definition: rectf.hpp:31
float get_height() const
Returns the object&#39;s hitbox height.
Definition: moving_object.cpp:115
bool is_valid() const
returns true if the object is not scheduled to be removed yet
Definition: game_object.hpp:188
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
virtual void collision_tile(uint32_t) override
called when tiles with special attributes have been touched
Definition: moving_object.hpp:60
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
virtual GameObjectClasses get_class_types() const
List notable classes in inheritance hierarchy of class.
Definition: game_object.cpp:113
float get_x() const
Returns the object&#39;s X coordinate.
Definition: moving_object.cpp:85
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_object.hpp:49
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
Definition: collision_object.hpp:33
virtual bool collides(GameObject &, const CollisionHit &) const override
when 2 objects collided, we will first call the collision functions of both objects that can decide o...
Definition: moving_object.hpp:55
virtual void editor_select() override
The user clicked on the object in the editor and selected it.
Definition: moving_object.cpp:121
Definition: reader_mapping.hpp:32
This class collects data about a collision.
Definition: collision_hit.hpp:44