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