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