another try
[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   bool collision_static(MovingObject* object, const Vector& movement);
160   
161   /** Checks for all possible collisions. And calls the
162       collision_handlers, which the collision_objects provide for this
163       case (or not). */
164   void handle_collisions();
165   
166   void collision_object(MovingObject* object1, MovingObject* object2) const;
167   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
168
169   void fix_old_tiles();
170
171   static Sector* _current;
172   
173   std::string name;
174
175   std::vector<Bullet*> bullets;
176
177   std::string init_script;
178
179   /// container for newly created objects, they'll be added in Sector::update
180   GameObjects gameobjects_new;
181  
182   MusicType currentmusic;
183
184   std::auto_ptr<CollisionGrid> grid;
185
186   HSQOBJECT sector_table;
187   /// sector scripts
188   typedef std::vector<HSQOBJECT> ScriptList;
189   ScriptList scripts;
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