- Yet another try in the endless quest for perfect collision detection.
[supertux.git] / src / sector.hpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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
25 #include "direction.hpp"
26 #include "math/vector.hpp"
27 #include "video/drawing_context.hpp"
28
29 namespace lisp {
30 class Lisp;
31 class Writer;
32 }
33
34 class Rect;
35 class Sprite;
36 class GameObject;
37 class Player;
38 class Camera;
39 class TileMap;
40 class Bullet;
41 class CollisionGrid;
42 class ScriptInterpreter;
43 class SpawnPoint;
44 class MovingObject;
45 class CollisionHit;
46
47 enum MusicType {
48   LEVEL_MUSIC,
49   HERRING_MUSIC
50 };
51
52 /** This class holds a sector (a part of a level) and all the game objects
53  * (badguys, player, background, tilemap, ...)
54  */
55 class Sector
56 {
57 public:
58   Sector();
59   ~Sector();
60
61   /// read sector from lisp file
62   void parse(const lisp::Lisp& lisp);
63   void parse_old_format(const lisp::Lisp& lisp);
64   /// write sector to lisp file
65   void write(lisp::Writer& writer);
66
67   /// activates this sector (change music, intialize player class, ...)
68   void activate(const std::string& spawnpoint);
69   void activate(const Vector& player_pos);
70
71   void update(float elapsed_time);
72   void update_game_objects();
73
74   void draw(DrawingContext& context);
75
76   /// adds a gameobject
77   void add_object(GameObject* object);
78
79   void set_name(const std::string& name)
80   { this->name = name; }
81   const std::string& get_name() const
82   { return name; }
83
84   /// tests if a given rectangle is inside the sector
85   bool inside(const Rect& rectangle) const;
86
87   void play_music(MusicType musictype);
88   MusicType get_music_type();
89   
90   bool add_bullet(const Vector& pos, float xm, Direction dir);
91   bool add_smoke_cloud(const Vector& pos);
92   void add_floating_text(const Vector& pos, const std::string& text);
93                                                                                 
94   /** get currently activated sector. */
95   static Sector* current()
96   { return _current; }
97
98   /** Get total number of badguys */
99   int get_total_badguys();
100
101   void collision_tilemap(const Rect& dest, const Vector& movement, CollisionHit& hit) const;
102
103   /** Checks if at the specified rectangle are gameobjects with STATIC flag set
104    * (or solid tiles from the tilemap)
105    */
106   bool is_free_space(const Rect& rect) const;
107
108   /**
109    * returns a list of players currently in the sector
110    */
111   std::vector<Player*> get_players() {
112     return std::vector<Player*>(1, this->player);
113   }
114
115 private:
116   uint32_t collision_tile_attributes(const Rect& dest) const;
117   
118   bool collision_static(MovingObject* object, const Vector& movement);
119   
120   /** Checks for all possible collisions. And calls the
121       collision_handlers, which the collision_objects provide for this
122       case (or not). */
123   void handle_collisions();
124   
125   void collision_object(MovingObject* object1, MovingObject* object2) const;
126   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
127   
128   static Sector* _current;
129   
130   std::string name;
131
132 public:
133   std::string music;
134   float gravity;
135
136   // some special objects, where we need direct access
137   Player* player;
138   TileMap* solids;
139   Camera* camera;
140   
141 private:
142   std::vector<Bullet*> bullets;
143
144   std::string init_script;
145
146 public: // TODO make this private again
147   typedef std::vector<GameObject*> GameObjects;
148   GameObjects gameobjects;
149   typedef std::vector<MovingObject*> MovingObjects;
150   MovingObjects moving_objects;
151   typedef std::vector<SpawnPoint*> SpawnPoints;
152   SpawnPoints spawnpoints;                       
153
154   Rect get_active_region();
155
156 private:
157   void fix_old_tiles();
158   
159   /// container for newly created objects, they'll be added in Sector::update
160   GameObjects gameobjects_new;
161  
162   MusicType currentmusic;
163
164   CollisionGrid* grid;
165 };
166
167 #endif
168