-Use "jump-with-up" in config for all controlers. No need
[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, intialize 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 private:
197   Level* level; /**< Parent level containing this sector */
198   uint32_t collision_tile_attributes(const Rect& dest) const;
199
200   void before_object_remove(GameObject* object);
201   bool before_object_add(GameObject* object);
202
203   void try_expose(GameObject* object);
204   void try_unexpose(GameObject* object);
205   void try_expose_me();
206   void try_unexpose_me();
207
208   /** Checks for all possible collisions. And calls the
209       collision_handlers, which the collision_objects provide for this
210       case (or not). */
211   void handle_collisions();
212
213   /**
214    * Does collision detection between 2 objects and does instant
215    * collision response handling in case of a collision
216    */
217   void collision_object(MovingObject* object1, MovingObject* object2) const;
218
219   /**
220    * Does collision detection of an object against all other static
221    * objects (and the tilemap) in the level. Collision response is done
222    * for the first hit in time. (other hits get ignored, the function
223    * should be called repeatedly to resolve those)
224    *
225    * returns true if the collision detection should be aborted for this object
226    * (because of ABORT_MOVE in the collision response or no collisions)
227    */
228   void collision_static(collision::Constraints* constraints,
229       const Vector& movement, const Rect& dest, GameObject& object);
230
231   void collision_static_constrains(MovingObject& object);
232
233   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
234
235   void fix_old_tiles();
236
237   static Sector* _current;
238
239   std::string name;
240
241   std::vector<Bullet*> bullets;
242
243   std::string init_script;
244
245   /// container for newly created objects, they'll be added in Sector::update
246   GameObjects gameobjects_new;
247
248   MusicType currentmusic;
249
250   HSQOBJECT sector_table;
251   /// sector scripts
252   typedef std::vector<HSQOBJECT> ScriptList;
253   ScriptList scripts;
254
255   Color ambient_light;
256
257 public: // TODO make this private again
258   /// show collision rectangles of moving objects (for debugging)
259   static bool show_collrects;
260   static bool draw_solids_only;
261
262   GameObjects gameobjects;
263   MovingObjects moving_objects;
264   SpawnPoints spawnpoints;
265   Portables portables;
266
267   std::string music;
268   float gravity;
269
270   // some special objects, where we need direct access
271   // (try to avoid accessing them directly)
272   Player* player;
273   std::list<TileMap*> solid_tilemaps;
274   Camera* camera;
275 };
276
277 #endif