b23302ad411a791e1444fdabf2888eba65654d86
[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,
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   /**
46    * Doesn't move and isn't explicitely checked for collisions with other
47    * objects (but other objects might check with this)
48    * The difference to COLGROUP_TOUCHABLE is that we can do multiple
49    * collision response tests in a row which is needed for static object
50    * that tux walks on. The results for collisions with STATIC objects
51    * are also sorted by time (so that the first hit gets handled first).
52    *
53    * Use this for static obstacles
54    */
55   COLGROUP_STATIC,
56   /**
57    * Isn't explicitely checked for collisions with other objects. But other
58    * objects might check with this object.
59    * Difference to COLGROUP_STATIC is that collisions with this object are
60    * only tested once and collision response is typically not handled
61    *
62    * Use this for touchable things like spikes/areas or collectibles like
63    * coins
64    */
65   COLGROUP_TOUCHABLE,
66
67   /**
68    * Should be used for tilemaps
69    */
70   COLGROUP_TILEMAP
71 };
72
73 /**
74  * Base class for all dynamic/moving game objects. This class contains things
75  * for handling the bounding boxes and collision feedback.
76  */
77 class MovingObject : public GameObject
78 {
79 public:
80   MovingObject(std::string name = "");
81   MovingObject(const lisp::Lisp& lisp);
82   virtual ~MovingObject();
83
84   /** this function is called when the object collided with something solid */
85   virtual void collision_solid(const CollisionHit& hit)
86   {
87     (void) hit;
88   }
89   /** this function is called when the object collided with any other object */
90   virtual HitResponse collision(GameObject& other, const CollisionHit& hit) = 0;
91   /** called when tiles with special attributes have been touched */
92   virtual void collision_tile(uint32_t tile_attributes)
93   {
94     (void) tile_attributes;
95   }
96
97   const Vector& get_pos() const
98   {
99     return bbox.p1;
100   }
101
102   /** returns the bounding box of the Object */
103   const Rect& get_bbox() const
104   {
105     return bbox;
106   }
107
108   const Vector& get_movement() const
109   {
110     return movement;
111   }
112
113   /** places the moving object at a specific position. Be carefull when
114    * using this function. There are no collision detection checks performed
115    * here so bad things could happen.
116    */
117   virtual void set_pos(const Vector& pos)
118   {
119     dest.move(pos-get_pos());
120     bbox.set_pos(pos);
121   }
122
123   /**
124    * sets the moving object's bbox to a specific width. 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_width(float w)
129   {
130     dest.set_width(w);
131     bbox.set_width(w);
132   }
133
134   /**
135    * sets the moving object's bbox to a specific size. 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_size(float w, float h)
140   {
141     dest.set_size(w, h);
142     bbox.set_size(w, h);
143   }*/
144
145   CollisionGroup get_group() const
146   {
147     return group;
148   }
149
150   void set_group(CollisionGroup group)
151   {
152     this->group = group;
153   }
154
155   // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- //
156   void set_solid(bool solid)
157   {
158     this->solid = solid;
159   }
160   bool is_solid() const
161   {
162     return solid;
163   }
164   void move(float x, float y)
165   {
166     bbox.move(Vector(x, y));
167   }
168   void set_pos(float x, float y)
169   {
170     set_pos(Vector(x, y));
171   }
172   float get_pos_x() const
173   {
174     return bbox.get_left();
175   }
176   float get_pos_y() const
177   {
178     return bbox.get_top();
179   }
180   void set_size(float w, float h)
181   {
182     dest.set_size(w, h);
183     bbox.set_size(w, h);
184   }
185   float get_width() const
186   {
187     return bbox.get_width();
188   }
189   float get_height() const
190   {
191     return bbox.get_height();
192   }
193   void set_velocity(float x, float y)
194   {
195     movement = Vector(x, y);
196   }
197   float get_velocity_x() const
198   {
199     return movement.x;
200   }
201   float get_velocity_y() const
202   {
203     return movement.y;
204   }
205   // --- END METHODS TO EXPOSE TO SQUIRREL --- //
206
207 protected:
208   MovingObject(Rect bbox, CollisionGroup group, bool solid);
209   friend class Sector;
210   friend class CollisionGrid;
211   friend class Platform;
212
213   /** The bounding box of the object (as used for collision detection, this
214    * isn't necessarily the bounding box for graphics)
215    */
216   Rect bbox;
217   /** The movement that will happen till next frame
218    */
219   Vector movement;
220   /** The collision group */
221   CollisionGroup group;
222
223 private:
224   /**
225    * this is only here for internal collision detection use (don't touch this
226    * from outside collision detection code)
227    *
228    * This field holds the currently anticipated destination of the object
229    * during collision detection
230    */
231   Rect dest;
232
233   bool solid; /**< true if this object should be considered when doing collision detection */
234 };
235
236 #endif