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