Had a bit of time today and worked on supertux:
[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 "direction.h"
27 #include "math/vector.h"
28 #include "audio/musicref.h"
29 #include "video/drawing_context.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 class SpawnPoint
50 {
51 public:
52   std::string name;
53   Vector pos;
54 };
55
56 /** This class holds a sector (a part of a level) and all the game objects
57  * (badguys, player, background, tilemap, ...)
58  */
59 class Sector
60 {
61 public:
62   Sector();
63   ~Sector();
64
65   /// read sector from lisp file
66   void parse(const lisp::Lisp& lisp);
67   void parse_old_format(const lisp::Lisp& lisp);
68   /// write sector to lisp file
69   void write(lisp::Writer& writer);
70
71   /// activates this sector (change music, intialize player class, ...)
72   void activate(const std::string& spawnpoint = "main");
73   /// get best spawn point
74   Vector get_best_spawn_point(Vector pos);
75
76   void action(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 Rectangle& rectangle) const;
91
92   void play_music(int musictype);
93   int 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   MusicRef level_song_fast;
126
127 public:
128   std::string song_title;
129   float gravity;
130
131   // some special objects, where we need direct access
132   Player* player;
133   TileMap* solids;
134   Camera* camera;
135   
136 private:
137   std::vector<Bullet*> bullets;
138
139 public: // TODO make this private again
140   typedef std::vector<GameObject*> GameObjects;
141   GameObjects gameobjects;
142   typedef std::vector<SpawnPoint*> SpawnPoints;
143   SpawnPoints spawnpoints;                       
144
145   Rectangle get_active_region();
146
147 private:
148   void fix_old_tiles();
149   
150   /// container for newly created objects, they'll be added in Sector::action
151   GameObjects gameobjects_new;
152   
153   int currentmusic;
154
155   CollisionGrid* grid;
156 };
157
158 #endif
159