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