minimize some #includes and replace with forward decls
[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
27 #include "direction.hpp"
28 #include "video/color.hpp"
29 #include "scripting/ssector.hpp"
30
31 namespace lisp {
32 class Lisp;
33 class Writer;
34 }
35 namespace collision {
36 class Constraints;
37 }
38
39 class Vector;
40 class Rect;
41 class Sprite;
42 class GameObject;
43 class Player;
44 class Camera;
45 class TileMap;
46 class Bullet;
47 class ScriptInterpreter;
48 class SpawnPoint;
49 class MovingObject;
50 class CollisionHit;
51 class Level;
52 class Portable;
53 class DrawingContext;
54
55 enum MusicType {
56   LEVEL_MUSIC,
57   HERRING_MUSIC,
58   HERRING_WARNING_MUSIC
59 };
60
61 /**
62  * This class holds a sector (a part of a level) and all the game objects in
63  * the sector
64  */
65 class Sector : public Scripting::SSector
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 lisp::Lisp& lisp);
76   void parse_old_format(const lisp::Lisp& lisp);
77   /// write sector to lisp file
78   void write(lisp::Writer& writer);
79
80   /// activates this sector (change music, intialize player class, ...)
81   void activate(const std::string& spawnpoint);
82   void activate(const Vector& player_pos);
83   void deactivate();
84
85   void update(float elapsed_time);
86   void update_game_objects();
87
88   void draw(DrawingContext& context);
89
90   /**
91    * runs a script in the context of the sector (sector_table will be the
92    * roottable of this squirrel VM)
93    */
94   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
95
96   /// adds a gameobject
97   void add_object(GameObject* object);
98
99   void set_name(const std::string& name)
100   { this->name = name; }
101   const std::string& get_name() const
102   { return name; }
103
104   /**
105    * tests if a given rectangle is inside the sector
106    * (a rectangle that is on top of the sector is considered inside)
107    */
108   bool inside(const Rect& rectangle) const;
109
110   void play_music(MusicType musictype);
111   MusicType get_music_type();
112
113   bool add_bullet(const Vector& pos, float xm, Direction dir);
114   bool add_smoke_cloud(const Vector& pos);
115
116   /** get currently activated sector. */
117   static Sector* current()
118   { return _current; }
119
120   /** Get total number of badguys */
121   int get_total_badguys();
122
123   /** Get total number of GameObjects of given type */
124   template<class T> int get_total_count()
125   {
126     int total = 0;
127     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
128       if (dynamic_cast<T*>(*i)) total++;
129     }
130     return total;
131   }
132
133   void collision_tilemap(collision::Constraints* constraints,
134       const Vector& movement, const Rect& dest) 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 Rect& 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 Rect& 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 Rect& 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
163   Rect get_active_region();
164
165   /**
166    * returns the width (in px) of a sector)
167    */
168   float get_width() const;
169
170   /**
171    * returns the height (in px) of a sector)
172    */
173   float get_height() const;
174
175   /**
176    * globally changes solid tilemaps' tile ids
177    */
178   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
179
180   typedef std::vector<GameObject*> GameObjects;
181   typedef std::vector<MovingObject*> MovingObjects;
182   typedef std::vector<SpawnPoint*> SpawnPoints;
183   typedef std::vector<Portable*> Portables;
184
185   // --- Scripting ---
186   /**
187    *  get/set color of ambient light
188    */
189   void set_ambient_light(float red, float green, float blue);
190   float get_ambient_red();
191   float get_ambient_green();
192   float get_ambient_blue();
193
194 private:
195   Level* level; /**< Parent level containing this sector */
196   uint32_t collision_tile_attributes(const Rect& dest) const;
197
198   void before_object_remove(GameObject* object);
199   bool before_object_add(GameObject* object);
200
201   void try_expose(GameObject* object);
202   void try_unexpose(GameObject* object);
203   void try_expose_me();
204   void try_unexpose_me();
205
206   /** Checks for all possible collisions. And calls the
207       collision_handlers, which the collision_objects provide for this
208       case (or not). */
209   void handle_collisions();
210
211   /**
212    * Does collision detection between 2 objects and does instant
213    * collision response handling in case of a collision
214    */
215   void collision_object(MovingObject* object1, MovingObject* object2) const;
216
217   /**
218    * Does collision detection of an object against all other static
219    * objects (and the tilemap) in the level. Collision response is done
220    * for the first hit in time. (other hits get ignored, the function
221    * should be called repeatedly to resolve those)
222    *
223    * returns true if the collision detection should be aborted for this object
224    * (because of ABORT_MOVE in the collision response or no collisions)
225    */
226   void collision_static(collision::Constraints* constraints,
227       const Vector& movement, const Rect& dest, GameObject& object);
228
229   void collision_static_constrains(MovingObject& object);
230
231   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
232
233   void fix_old_tiles();
234
235   static Sector* _current;
236
237   std::string name;
238
239   std::vector<Bullet*> bullets;
240
241   std::string init_script;
242
243   /// container for newly created objects, they'll be added in Sector::update
244   GameObjects gameobjects_new;
245
246   MusicType currentmusic;
247
248   HSQOBJECT sector_table;
249   /// sector scripts
250   typedef std::vector<HSQOBJECT> ScriptList;
251   ScriptList scripts;
252
253   Color ambient_light;
254
255 public: // TODO make this private again
256   /// show collision rectangles of moving objects (for debugging)
257   static bool show_collrects;
258   static bool draw_solids_only;
259
260   GameObjects gameobjects;
261   MovingObjects moving_objects;
262   SpawnPoints spawnpoints;
263   Portables portables;
264
265   std::string music;
266   float gravity;
267
268   // some special objects, where we need direct access
269   // (try to avoid accessing them directly)
270   Player* player;
271   std::list<TileMap*> solid_tilemaps;
272   Camera* camera;
273 };
274
275 #endif