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