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