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