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