Add a couple grey castle tiles to ST castle inventory.
[supertux.git] / src / 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 <stdint.h>
21
22 #include "math/rectf.hpp"
23 #include "supertux/collision_hit.hpp"
24 #include "supertux/game_object.hpp"
25
26 class Sector;
27 class CollisionGrid;
28
29 enum CollisionGroup {
30   /** Objects in DISABLED group are not tested for collisions */
31   COLGROUP_DISABLED = 0,
32
33   /** Tested against:
34       - tiles + attributes
35       - static obstacles
36       - touchables
37       - other moving objects
38       and it counts as an obstacle during static collision phase.
39
40       Use for kinematic moving objects like platforms and rocks. */
41   COLGROUP_MOVING_STATIC,
42
43   /** Tested against:
44       - tiles + attributes
45       - static obstacles
46       - touchables
47       - other moving objects
48
49       Use for ordinary objects. */
50   COLGROUP_MOVING,
51
52   /** Tested against:
53       - tiles + attributes
54       - static obstacles
55
56       Use for interactive particles and decoration. */
57   COLGROUP_MOVING_ONLY_STATIC,
58
59   /** Tested against:
60       - moving objects
61       and it counts as an obstacle during static collision phase.
62
63       Use for static obstacles that Tux walks on. */
64   COLGROUP_STATIC,
65
66   /** Tested against:
67       - moving objects
68
69       Use for triggers like spikes/areas or collectibles like coins. */
70   COLGROUP_TOUCHABLE
71 };
72
73 /** Base class for all dynamic/moving game objects. This class
74     contains things for handling the bounding boxes and collision
75     feedback. */
76 class MovingObject : public GameObject
77 {
78 public:
79   MovingObject();
80   virtual ~MovingObject();
81
82   /** this function is called when the object collided with something solid */
83   virtual void collision_solid(const CollisionHit& hit)
84   {
85     (void) hit;
86   }
87
88   /** when 2 objects collided, we will first call the
89       pre_collision_check functions of both objects that can decide on
90       how to react to the collision. */
91   virtual bool collides(GameObject& other, const CollisionHit& hit)
92   {
93     (void) other;
94     (void) hit;
95     return true;
96   }
97
98   /** this function is called when the object collided with any other object */
99   virtual HitResponse collision(GameObject& other, const CollisionHit& hit) = 0;
100
101   /** called when tiles with special attributes have been touched */
102   virtual void collision_tile(uint32_t tile_attributes)
103   {
104     (void) tile_attributes;
105   }
106
107   const Vector& get_pos() const
108   {
109     return bbox.p1;
110   }
111
112   /** returns the bounding box of the Object */
113   const Rectf& get_bbox() const
114   {
115     return bbox;
116   }
117
118   const Vector& get_movement() const
119   {
120     return movement;
121   }
122
123   /** places the moving object at a specific position. Be careful when
124       using this function. There are no collision detection checks
125       performed here so bad things could happen. */
126   virtual void set_pos(const Vector& pos)
127   {
128     dest.move(pos-get_pos());
129     bbox.set_pos(pos);
130   }
131
132   /** sets the moving object's bbox to a specific width. Be careful
133       when using this function. There are no collision detection
134       checks performed here so bad things could happen. */
135   virtual void set_width(float w)
136   {
137     dest.set_width(w);
138     bbox.set_width(w);
139   }
140
141   /** sets the moving object's bbox to a specific size. Be careful
142       when using this function. There are no collision detection
143       checks performed here so bad things could happen. */
144   virtual void set_size(float w, float h)
145   {
146     dest.set_size(w, h);
147     bbox.set_size(w, h);
148   }
149
150   CollisionGroup get_group() const
151   {
152     return group;
153   }
154
155 protected:
156   friend class Sector;
157   friend class CollisionGrid;
158   friend class Platform;
159
160   void set_group(CollisionGroup group_)
161   {
162     this->group = group_;
163   }
164
165   /** The bounding box of the object (as used for collision detection,
166       this isn't necessarily the bounding box for graphics) */
167   Rectf bbox;
168
169   /** The movement that will happen till next frame */
170   Vector movement;
171
172   /** The collision group */
173   CollisionGroup group;
174
175 private:
176   /** this is only here for internal collision detection use (don't touch this
177       from outside collision detection code)
178
179       This field holds the currently anticipated destination of the object
180       during collision detection */
181   Rectf dest;
182 };
183
184 #endif
185
186 /* EOF */