New grow and skid sounds from remaxim
[supertux.git] / src / moving_object.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_MOVING_OBJECT_H
20 #define SUPERTUX_MOVING_OBJECT_H
21
22 #include <stdint.h>
23
24 #include "game_object.hpp"
25 #include "collision_hit.hpp"
26 #include "math/vector.hpp"
27 #include "math/rect.hpp"
28
29 class Sector;
30 class CollisionGrid;
31
32 enum CollisionGroup {
33   /** Objects in DISABLED group are not tested for collisions */
34   COLGROUP_DISABLED = 0,
35   /**
36    * "default" is moving object. MovingObjects get tested against all other
37    * objects and against other movingobjects
38    */
39   COLGROUP_MOVING,
40   /**
41    * a Moving object, that is not tested against other MovingObjects (or other
42    * MovingOnlyStatic objects), but is tested against all other objects.
43    */
44   COLGROUP_MOVING_ONLY_STATIC,
45   /** TODO write docu :-/ */
46   COLGROUP_MOVING_STATIC,
47   /**
48    * Doesn't move and isn't explicitly checked for collisions with other
49    * objects (but other objects might check with this)
50    * The difference to COLGROUP_TOUCHABLE is that we can do multiple
51    * collision response tests in a row which is needed for static object
52    * that tux walks on. The results for collisions with STATIC objects
53    * are also sorted by time (so that the first hit gets handled first).
54    *
55    * Use this for static obstacles
56    */
57   COLGROUP_STATIC,
58   /**
59    * Isn't explicitly checked for collisions with other objects. But other
60    * objects might check with this object.
61    * Difference to COLGROUP_STATIC is that collisions with this object are
62    * only tested once and collision response is typically not handled
63    *
64    * Use this for touchable things like spikes/areas or collectibles like
65    * coins
66    */
67   COLGROUP_TOUCHABLE,
68
69   /**
70    * Should be used for tilemaps
71    */
72   COLGROUP_TILEMAP
73 };
74
75 /**
76  * Base class for all dynamic/moving game objects. This class contains things
77  * for handling the bounding boxes and collision feedback.
78  */
79 class MovingObject : public GameObject
80 {
81 public:
82   MovingObject();
83   virtual ~MovingObject();
84
85   /** this function is called when the object collided with something solid */
86   virtual void collision_solid(const CollisionHit& hit)
87   {
88     (void) hit;
89   }
90   /**
91    * when 2 objects collided, we will first call the pre_collision_check
92    * functions of both objects that can decide on how to react to the collision.
93    */
94   virtual bool collides(GameObject& other, const CollisionHit& hit)
95   {
96     (void) other;
97     (void) hit;
98     return true;
99   }
100   /** this function is called when the object collided with any other object */
101   virtual HitResponse collision(GameObject& other, const CollisionHit& hit) = 0;
102   /** called when tiles with special attributes have been touched */
103   virtual void collision_tile(uint32_t tile_attributes)
104   {
105     (void) tile_attributes;
106   }
107
108   const Vector& get_pos() const
109   {
110     return bbox.p1;
111   }
112
113   /** returns the bounding box of the Object */
114   const Rect& get_bbox() const
115   {
116     return bbox;
117   }
118
119   const Vector& get_movement() const
120   {
121     return movement;
122   }
123
124   /** places the moving object at a specific position. Be careful when
125    * using this function. There are no collision detection checks performed
126    * here so bad things could happen.
127    */
128   virtual void set_pos(const Vector& pos)
129   {
130     dest.move(pos-get_pos());
131     bbox.set_pos(pos);
132   }
133
134   /**
135    * sets the moving object's bbox to a specific width. Be careful when
136    * using this function. There are no collision detection checks performed
137    * here so bad things could happen.
138    */
139   virtual void set_width(float w)
140   {
141     dest.set_width(w);
142     bbox.set_width(w);
143   }
144
145   /**
146    * sets the moving object's bbox to a specific size. Be careful when
147    * using this function. There are no collision detection checks performed
148    * here so bad things could happen.
149    */
150   virtual void set_size(float w, float h)
151   {
152     dest.set_size(w, h);
153     bbox.set_size(w, h);
154   }
155
156   CollisionGroup get_group() const
157   {
158     return group;
159   }
160
161 protected:
162   friend class Sector;
163   friend class CollisionGrid;
164   friend class Platform;
165
166   void set_group(CollisionGroup group)
167   {
168     this->group = group;
169   }
170
171   /** The bounding box of the object (as used for collision detection, this
172    * isn't necessarily the bounding box for graphics)
173    */
174   Rect bbox;
175   /** The movement that will happen till next frame
176    */
177   Vector movement;
178   /** The collision group */
179   CollisionGroup group;
180
181 private:
182   /**
183    * this is only here for internal collision detection use (don't touch this
184    * from outside collision detection code)
185    *
186    * This field holds the currently anticipated destination of the object
187    * during collision detection
188    */
189   Rect dest;
190 };
191
192 #endif