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