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