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