Statisticts now also count secret areas found
[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 <memory>
25 #include <squirrel.h>
26
27 #include "direction.hpp"
28 #include "script_manager.hpp"
29 #include "math/vector.hpp"
30 #include "video/drawing_context.hpp"
31
32 namespace lisp {
33 class Lisp;
34 class Writer;
35 }
36
37 class Rect;
38 class Sprite;
39 class GameObject;
40 class Player;
41 class Camera;
42 class TileMap;
43 class Bullet;
44 class CollisionGrid;
45 class ScriptInterpreter;
46 class SpawnPoint;
47 class MovingObject;
48 class CollisionHit;
49 class Level;
50
51 enum MusicType {
52   LEVEL_MUSIC,
53   HERRING_MUSIC,
54   HERRING_WARNING_MUSIC
55 };
56
57 /**
58  * This class holds a sector (a part of a level) and all the game objects in
59  * the sector
60  */
61 class Sector
62 {
63 public:
64   Sector(Level* parent);
65   ~Sector();
66
67   /// get parent level
68   Level* get_level();
69
70   /// read sector from lisp file
71   void parse(const lisp::Lisp& lisp);
72   void parse_old_format(const lisp::Lisp& lisp);
73   /// write sector to lisp file
74   void write(lisp::Writer& writer);
75
76   /// activates this sector (change music, intialize player class, ...)
77   void activate(const std::string& spawnpoint);
78   void activate(const Vector& player_pos);
79   void deactivate();
80
81   void update(float elapsed_time);
82   void update_game_objects();
83
84   void draw(DrawingContext& context);
85
86   /**
87    * runs a script in the context of the sector (sector_table will be the
88    * roottable of this squirrel VM)
89    */
90   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
91
92   /// adds a gameobject
93   void add_object(GameObject* object);
94
95   void set_name(const std::string& name)
96   { this->name = name; }
97   const std::string& get_name() const
98   { return name; }
99
100   /**
101    * tests if a given rectangle is inside the sector
102    * (a rectangle that is on top of the sector is considered inside)
103    */
104   bool inside(const Rect& rectangle) const;
105
106   void play_music(MusicType musictype);
107   MusicType get_music_type();
108   
109   bool add_bullet(const Vector& pos, float xm, Direction dir);
110   bool add_smoke_cloud(const Vector& pos);
111   void add_floating_text(const Vector& pos, const std::string& text);
112                                                                                 
113   /** get currently activated sector. */
114   static Sector* current()
115   { return _current; }
116
117   /** Get total number of badguys */
118   int get_total_badguys();
119
120   /** Get total number of GameObjects of given type */
121   template<class T> int get_total_count()
122   {
123     int total = 0;
124     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
125       if (dynamic_cast<T*>(*i)) total++;
126     }
127     return total;
128   }
129
130   void collision_tilemap(const Rect& dest, const Vector& movement, CollisionHit& hit) const;
131
132   /** Checks if at the specified rectangle are gameobjects with STATIC flag set
133    * (or solid tiles from the tilemap)
134    */
135   bool is_free_space(const Rect& rect) const;
136
137   /**
138    * returns a list of players currently in the sector
139    */
140   std::vector<Player*> get_players() {
141     return std::vector<Player*>(1, this->player);
142   }
143
144   Rect get_active_region();
145
146   typedef std::vector<GameObject*> GameObjects;
147   typedef std::vector<MovingObject*> MovingObjects;
148   typedef std::vector<SpawnPoint*> SpawnPoints;
149
150 private:
151   Level* level; /**< Parent level containing this sector */
152   uint32_t collision_tile_attributes(const Rect& dest) const;
153
154   void before_object_remove(GameObject* object);
155   bool before_object_add(GameObject* object);
156
157   void try_expose(GameObject* object);
158   void try_unexpose(GameObject* object);
159   
160   bool collision_static(MovingObject* object, const Vector& movement);
161   
162   /** Checks for all possible collisions. And calls the
163       collision_handlers, which the collision_objects provide for this
164       case (or not). */
165   void handle_collisions();
166   
167   void collision_object(MovingObject* object1, MovingObject* object2) const;
168   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
169
170   void fix_old_tiles();
171
172   static Sector* _current;
173   
174   std::string name;
175
176   std::vector<Bullet*> bullets;
177
178   std::string init_script;
179
180   /// container for newly created objects, they'll be added in Sector::update
181   GameObjects gameobjects_new;
182  
183   MusicType currentmusic;
184
185   std::auto_ptr<CollisionGrid> grid;
186
187   HSQOBJECT sector_table;
188   /// sector scripts
189   std::auto_ptr<ScriptManager> script_manager;
190
191 public: // TODO make this private again
192   /// show collision rectangles of moving objects (for debugging)
193   static bool show_collrects;
194   static bool draw_solids_only;
195   
196   GameObjects gameobjects;
197   MovingObjects moving_objects;
198   SpawnPoints spawnpoints;                       
199
200   std::string music;
201   float gravity;
202
203   // some special objects, where we need direct access
204   // (try to avoid accessing them directly)
205   Player* player;
206   TileMap* solids;
207   Camera* camera;
208 };
209
210 #endif
211