The BIG COMMIT(tm)
[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 "badguy.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 LispReader;
36 class Sprite;
37 }
38
39 class InteractiveObject;
40 class Background;
41 class Player;
42 class Camera;
43 class Trampoline;
44 class FlyingPlatform;
45 class TileMap;
46 class Upgrade;
47 class Bullet;
48 class SmokeCloud;
49 class Particles;
50 class BadGuy;
51 class Tile;
52
53 struct SpawnPoint
54 {
55   std::string name;
56   Vector pos;
57 };
58
59 /** This class holds a sector (a part of a level) and all the game objects
60  * (badguys, player, background, tilemap, ...)
61  */
62 class Sector
63 {
64 public:
65   Sector();
66   ~Sector();
67
68   /// create new sector
69   static Sector *create(const std::string& name, size_t width, size_t height);
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   const std::string& get_name() const
90   { return name; }
91
92   /// tests if a given rectangle is inside the sector
93   bool inside(const Rectangle& rectangle) const;
94
95   void play_music(int musictype);
96   int get_music_type();
97   
98   /** Checks for all possible collisions. And calls the
99       collision_handlers, which the collision_objects provide for this
100       case (or not). */
101   void collision_handler();
102
103   void add_score(const Vector& pos, int s);
104  
105   bool add_bullet(const Vector& pos, float xm, Direction dir);
106   bool add_smoke_cloud(const Vector& pos);
107   bool add_particles(const Vector& epicenter, int min_angle, int max_angle,
108       const Vector& initial_velocity, const Vector& acceleration, int number,
109       Color color, int size, int life_time, int drawing_layer);
110   void add_floating_text(const Vector& pos, const std::string& text);
111                                                                                 
112   /** Flip the all the sector vertically. The purpose of this is to let
113       player to play the same level in a different way :) */
114   void do_vertical_flip();
115
116   /** @evil@ but can#t always be avoided in current design... */
117   static Sector* current()
118   { return _current; }
119
120   /** Get total number of badguys */
121   int get_total_badguys();
122
123 private:
124   void collision_tilemap(MovingObject* object, int depth);
125   void collision_object(MovingObject* object1, MovingObject* object2);
126   
127   void load_music();
128   GameObject* parseObject(const std::string& name, LispReader& reader);
129   
130   static Sector* _current;
131   
132   std::string name;
133
134   MusicRef level_song;
135   MusicRef level_song_fast;
136
137 public:
138   std::string song_title;
139   float gravity;
140
141   // some special objects, where we need direct access
142   Player* player;
143   TileMap* solids;
144   Background* background;
145   Camera* camera;
146   
147 private:
148   std::vector<Bullet*> bullets;
149
150 public: // TODO make this private again
151   typedef std::vector<InteractiveObject*> InteractiveObjects;
152   InteractiveObjects interactive_objects;
153   typedef std::vector<GameObject*> GameObjects;
154   GameObjects gameobjects;
155
156 private:
157   /// container for newly created objects, they'll be added in Sector::action
158   GameObjects gameobjects_new;
159   
160   typedef std::vector<SpawnPoint*> SpawnPoints;
161   SpawnPoints spawnpoints;
162
163   int currentmusic;
164 };
165
166 #endif
167