Bug 562: Collision detection for unisolid tiles doesn't handle tilemap offset
[supertux.git] / src / supertux / sector.hpp
1 //  SuperTux -  A Jump'n Run
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_SECTOR_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
19
20 #include <list>
21 #include <squirrel.h>
22 #include <stdint.h>
23
24 #include "scripting/ssector.hpp"
25 #include "supertux/direction.hpp"
26 #include "util/reader_fwd.hpp"
27 #include "util/writer_fwd.hpp"
28 #include "util/currenton.hpp"
29 #include "video/color.hpp"
30
31 namespace collision {
32 class Constraints;
33 }
34
35 class Vector;
36 class Rectf;
37 class Sprite;
38 class GameObject;
39 class Player;
40 class PlayerStatus;
41 class Camera;
42 class TileMap;
43 class Bullet;
44 class ScriptInterpreter;
45 class SpawnPoint;
46 class MovingObject;
47 class CollisionHit;
48 class Level;
49 class Portable;
50 class DrawingContext;
51 class DisplayEffect;
52
53 enum MusicType {
54   LEVEL_MUSIC,
55   HERRING_MUSIC,
56   HERRING_WARNING_MUSIC
57 };
58
59 /**
60  * Represents one of (potentially) multiple, separate parts of a Level.
61  *
62  * Sectors contain GameObjects, e.g. Badguys and Players.
63  */
64 class Sector : public scripting::SSector,
65                public Currenton<Sector>
66 {
67 public:
68   Sector(Level* parent);
69   ~Sector();
70
71   /// get parent level
72   Level* get_level();
73
74   /// read sector from lisp file
75   void parse(const Reader& lisp);
76   void parse_old_format(const Reader& lisp);
77   
78   /// activates this sector (change music, initialize player class, ...)
79   void activate(const std::string& spawnpoint);
80   void activate(const Vector& player_pos);
81   void deactivate();
82
83   void update(float elapsed_time);
84   void update_game_objects();
85
86   void draw(DrawingContext& context);
87
88   /**
89    * runs a script in the context of the sector (sector_table will be the
90    * roottable of this squirrel VM)
91    */
92   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
93
94   /// adds a gameobject
95   void add_object(GameObject* object);
96
97   void set_name(const std::string& name)
98   { this->name = name; }
99   const std::string& get_name() const
100   { return name; }
101
102   /**
103    * tests if a given rectangle is inside the sector
104    * (a rectangle that is on top of the sector is considered inside)
105    */
106   bool inside(const Rectf& rectangle) const;
107
108   void play_music(MusicType musictype);
109   MusicType get_music_type();
110
111   bool add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir);
112   bool add_smoke_cloud(const Vector& pos);
113
114   /** get currently activated sector. */
115   static Sector* current()
116   { return _current; }
117
118   /** Get total number of badguys */
119   int get_total_badguys();
120
121   /** Get total number of GameObjects of given type */
122   template<class T> int get_total_count()
123   {
124     int total = 0;
125     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
126       if (dynamic_cast<T*>(*i)) total++;
127     }
128     return total;
129   }
130
131   void collision_tilemap(collision::Constraints* constraints,
132                          const Vector& movement, const Rectf& dest,
133                          MovingObject &object) const;
134
135   /**
136    * Checks if the specified rectangle is free of (solid) tiles.
137    * Note that this does not include static objects, e.g. bonus blocks.
138    */
139   bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false) const;
140   /**
141    * Checks if the specified rectangle is free of both
142    * 1.) solid tiles and
143    * 2.) MovingObjects in COLGROUP_STATIC.
144    * Note that this does not include badguys or players.
145    */
146   bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
147   /**
148    * Checks if the specified rectangle is free of both
149    * 1.) solid tiles and
150    * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
151    * This includes badguys and players.
152    */
153   bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = 0) const;
154
155   /**
156    * returns a list of players currently in the sector
157    */
158   std::vector<Player*> get_players() {
159     return std::vector<Player*>(1, this->player);
160   }
161
162   Rectf get_active_region();
163
164   /**
165    * returns the width (in px) of a sector)
166    */
167   float get_width() const;
168
169   /**
170    * returns the height (in px) of a sector)
171    */
172   float get_height() const;
173
174   /**
175    * globally changes solid tilemaps' tile ids
176    */
177   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
178
179   typedef std::vector<GameObject*> GameObjects;
180   typedef std::vector<MovingObject*> MovingObjects;
181   typedef std::vector<SpawnPoint*> SpawnPoints;
182   typedef std::vector<Portable*> Portables;
183
184   // --- scripting ---
185   /**
186    *  get/set color of ambient light
187    */
188   void set_ambient_light(float red, float green, float blue);
189   float get_ambient_red();
190   float get_ambient_green();
191   float get_ambient_blue();
192
193   /**
194    *  set gravity throughout sector
195    */
196   void set_gravity(float gravity);
197   float get_gravity() const;
198
199 private:
200   uint32_t collision_tile_attributes(const Rectf& dest) const;
201
202   void before_object_remove(GameObject* object);
203   bool before_object_add(GameObject* object);
204
205   void try_expose(GameObject* object);
206   void try_unexpose(GameObject* object);
207   void try_expose_me();
208   void try_unexpose_me();
209
210   /** Checks for all possible collisions. And calls the
211       collision_handlers, which the collision_objects provide for this
212       case (or not). */
213   void handle_collisions();
214
215   /**
216    * Does collision detection between 2 objects and does instant
217    * collision response handling in case of a collision
218    */
219   void collision_object(MovingObject* object1, MovingObject* object2) const;
220
221   /**
222    * Does collision detection of an object against all other static
223    * objects (and the tilemap) in the level. Collision response is done
224    * for the first hit in time. (other hits get ignored, the function
225    * should be called repeatedly to resolve those)
226    *
227    * returns true if the collision detection should be aborted for this object
228    * (because of ABORT_MOVE in the collision response or no collisions)
229    */
230   void collision_static(collision::Constraints* constraints,
231                         const Vector& movement, const Rectf& dest, MovingObject& object);
232
233   void collision_static_constrains(MovingObject& object);
234
235   GameObject* parse_object(const std::string& name, const Reader& lisp);
236
237   void fix_old_tiles();
238
239 private:
240   static Sector* _current;
241
242   Level* level; /**< Parent level containing this sector */
243
244   std::string name;
245
246   std::vector<Bullet*> bullets;
247
248   std::string init_script;
249
250   /// container for newly created objects, they'll be added in Sector::update
251   GameObjects gameobjects_new;
252
253   MusicType currentmusic;
254
255   HSQOBJECT sector_table;
256   /// sector scripts
257   typedef std::vector<HSQOBJECT> ScriptList;
258   ScriptList scripts;
259
260   Color ambient_light;
261
262 public: // TODO make this private again
263   /// show collision rectangles of moving objects (for debugging)
264   static bool show_collrects;
265   static bool draw_solids_only;
266
267   GameObjects gameobjects;
268   MovingObjects moving_objects;
269   SpawnPoints spawnpoints;
270   Portables portables;
271
272   std::string music;
273   float gravity;
274
275   // some special objects, where we need direct access
276   // (try to avoid accessing them directly)
277   Player* player;
278   std::list<TileMap*> solid_tilemaps;
279   Camera* camera;
280   DisplayEffect* effect;
281
282 private:
283   Sector(const Sector&);
284   Sector& operator=(const Sector&);
285 };
286
287 #endif
288
289 /* EOF */