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