64e2b3d02aa044e7ae88e5e0b51950cfe521cefd
[supertux.git] / src / sector.h
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.h"
26 #include "math/vector.h"
27 #include "audio/musicref.h"
28 #include "video/drawing_context.h"
29
30 using namespace SuperTux;
31
32 namespace SuperTux {
33 class Rectangle;
34 }
35 namespace lisp {
36 class Lisp;
37 class Writer;
38 }
39
40 class Sprite;
41 class GameObject;
42 class Player;
43 class Camera;
44 class TileMap;
45 class Bullet;
46 class CollisionGrid;
47
48 class SpawnPoint
49 {
50 public:
51   std::string name;
52   Vector pos;
53 };
54
55 enum MusicType {
56   LEVEL_MUSIC,
57   HERRING_MUSIC
58 };
59
60 /** This class holds a sector (a part of a level) and all the game objects
61  * (badguys, player, background, tilemap, ...)
62  */
63 class Sector
64 {
65 public:
66   Sector();
67   ~Sector();
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
79   void action(float elapsed_time);
80   void update_game_objects();
81
82   void draw(DrawingContext& context);
83
84   /// adds a gameobject
85   void add_object(GameObject* object);
86
87   void set_name(const std::string& name)
88   { this->name = name; }
89   const std::string& get_name() const
90   { return name; }
91
92   /// tests if a given rectangle is inside the sector
93   bool inside(const Rectangle& rectangle) const;
94
95   void play_music(MusicType musictype);
96   MusicType get_music_type();
97   
98   /** Checks for all possible collisions. And calls the
99       collision_handlers, which the collision_objects provide for this
100       case (or not). */
101   void collision_handler();
102
103   bool add_bullet(const Vector& pos, float xm, Direction dir);
104   bool add_smoke_cloud(const Vector& pos);
105   void add_floating_text(const Vector& pos, const std::string& text);
106                                                                                 
107   /** @evil@ but can't always be avoided in current design... */
108   static Sector* current()
109   { return _current; }
110
111   /** Get total number of badguys */
112   int get_total_badguys();
113
114   // make this private again soon
115   void collision_tilemap(MovingObject* object, int depth);
116
117 private:
118   void collision_object(MovingObject* object1, MovingObject* object2);
119   
120   void load_music();
121   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
122   
123   static Sector* _current;
124   
125   std::string name;
126
127   MusicRef level_song;
128
129 public:
130   std::string song_title;
131   float gravity;
132
133   // some special objects, where we need direct access
134   Player* player;
135   TileMap* solids;
136   Camera* camera;
137   
138 private:
139   std::vector<Bullet*> bullets;
140
141 public: // TODO make this private again
142   typedef std::vector<GameObject*> GameObjects;
143   GameObjects gameobjects;
144   typedef std::vector<SpawnPoint*> SpawnPoints;
145   SpawnPoints spawnpoints;                       
146
147   Rectangle get_active_region();
148
149 private:
150   void fix_old_tiles();
151   
152   /// container for newly created objects, they'll be added in Sector::action
153   GameObjects gameobjects_new;
154  
155   MusicType currentmusic;
156
157   CollisionGrid* grid;
158 };
159
160 #endif
161