- sounds are on both channels
[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) const;
133
134   /**
135    * Checks if the specified rectangle is free of (solid) tiles.
136    * Note that this does not include static objects, e.g. bonus blocks.
137    */
138   bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false) const;
139   /**
140    * Checks if the specified rectangle is free of both
141    * 1.) solid tiles and
142    * 2.) MovingObjects in COLGROUP_STATIC.
143    * Note that this does not include badguys or players.
144    */
145   bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
146   /**
147    * Checks if the specified rectangle is free of both
148    * 1.) solid tiles and
149    * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
150    * This includes badguys and players.
151    */
152   bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = 0) const;
153
154   /**
155    * returns a list of players currently in the sector
156    */
157   std::vector<Player*> get_players() {
158     return std::vector<Player*>(1, this->player);
159   }
160
161   Rectf get_active_region();
162
163   /**
164    * returns the width (in px) of a sector)
165    */
166   float get_width() const;
167
168   /**
169    * returns the height (in px) of a sector)
170    */
171   float get_height() const;
172
173   /**
174    * globally changes solid tilemaps' tile ids
175    */
176   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
177
178   typedef std::vector<GameObject*> GameObjects;
179   typedef std::vector<MovingObject*> MovingObjects;
180   typedef std::vector<SpawnPoint*> SpawnPoints;
181   typedef std::vector<Portable*> Portables;
182
183   // --- scripting ---
184   /**
185    *  get/set color of ambient light
186    */
187   void set_ambient_light(float red, float green, float blue);
188   float get_ambient_red();
189   float get_ambient_green();
190   float get_ambient_blue();
191
192   /**
193    *  set gravity throughout sector
194    */
195   void set_gravity(float gravity);
196   float get_gravity() const;
197
198 private:
199   uint32_t collision_tile_attributes(const Rectf& dest) const;
200
201   void before_object_remove(GameObject* object);
202   bool before_object_add(GameObject* object);
203
204   void try_expose(GameObject* object);
205   void try_unexpose(GameObject* object);
206   void try_expose_me();
207   void try_unexpose_me();
208
209   /** Checks for all possible collisions. And calls the
210       collision_handlers, which the collision_objects provide for this
211       case (or not). */
212   void handle_collisions();
213
214   /**
215    * Does collision detection between 2 objects and does instant
216    * collision response handling in case of a collision
217    */
218   void collision_object(MovingObject* object1, MovingObject* object2) const;
219
220   /**
221    * Does collision detection of an object against all other static
222    * objects (and the tilemap) in the level. Collision response is done
223    * for the first hit in time. (other hits get ignored, the function
224    * should be called repeatedly to resolve those)
225    *
226    * returns true if the collision detection should be aborted for this object
227    * (because of ABORT_MOVE in the collision response or no collisions)
228    */
229   void collision_static(collision::Constraints* constraints,
230                         const Vector& movement, const Rectf& dest, GameObject& object);
231
232   void collision_static_constrains(MovingObject& object);
233
234   GameObject* parse_object(const std::string& name, const Reader& lisp);
235
236   void fix_old_tiles();
237
238 private:
239   static Sector* _current;
240
241   Level* level; /**< Parent level containing this sector */
242
243   std::string name;
244
245   std::vector<Bullet*> bullets;
246
247   std::string init_script;
248
249   /// container for newly created objects, they'll be added in Sector::update
250   GameObjects gameobjects_new;
251
252   MusicType currentmusic;
253
254   HSQOBJECT sector_table;
255   /// sector scripts
256   typedef std::vector<HSQOBJECT> ScriptList;
257   ScriptList scripts;
258
259   Color ambient_light;
260
261 public: // TODO make this private again
262   /// show collision rectangles of moving objects (for debugging)
263   static bool show_collrects;
264   static bool draw_solids_only;
265
266   GameObjects gameobjects;
267   MovingObjects moving_objects;
268   SpawnPoints spawnpoints;
269   Portables portables;
270
271   std::string music;
272   float gravity;
273
274   // some special objects, where we need direct access
275   // (try to avoid accessing them directly)
276   Player* player;
277   std::list<TileMap*> solid_tilemaps;
278   Camera* camera;
279   DisplayEffect* effect;
280
281 private:
282   Sector(const Sector&);
283   Sector& operator=(const Sector&);
284 };
285
286 #endif
287
288 /* EOF */