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