move over rewritten lispreader from tuxkart (with additional fixes), generalized...
[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 Sprite;
36 class Rectangle;
37 }
38 namespace lisp {
39 class Lisp;
40 class Writer;
41 }
42
43 class InteractiveObject;
44 class Background;
45 class Player;
46 class Camera;
47 class Trampoline;
48 class FlyingPlatform;
49 class TileMap;
50 class Upgrade;
51 class Bullet;
52 class SmokeCloud;
53 class Particles;
54 class BadGuy;
55 class Tile;
56
57 struct SpawnPoint
58 {
59   std::string name;
60   Vector pos;
61 };
62
63 /** This class holds a sector (a part of a level) and all the game objects
64  * (badguys, player, background, tilemap, ...)
65  */
66 class Sector
67 {
68 public:
69   Sector();
70   ~Sector();
71
72   /// read sector from lisp file
73   void parse(const lisp::Lisp& lisp);
74   void parse_old_format(const lisp::Lisp& lisp);
75   /// write sector to lisp file
76   void write(lisp::Writer& 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   void set_name(const std::string& name)
92   { this->name = name; }
93   const std::string& get_name() const
94   { return name; }
95
96   /// tests if a given rectangle is inside the sector
97   bool inside(const Rectangle& rectangle) const;
98
99   void play_music(int musictype);
100   int get_music_type();
101   
102   /** Checks for all possible collisions. And calls the
103       collision_handlers, which the collision_objects provide for this
104       case (or not). */
105   void collision_handler();
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   /** @evil@ but can#t always be avoided in current design... */
112   static Sector* current()
113   { return _current; }
114
115   /** Get total number of badguys */
116   int get_total_badguys();
117
118 private:
119   void collision_tilemap(MovingObject* object, int depth);
120   void collision_object(MovingObject* object1, MovingObject* object2);
121   
122   void load_music();
123   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
124   
125   static Sector* _current;
126   
127   std::string name;
128
129   MusicRef level_song;
130   MusicRef level_song_fast;
131
132 public:
133   std::string song_title;
134   float gravity;
135
136   // some special objects, where we need direct access
137   Player* player;
138   TileMap* solids;
139   Camera* camera;
140   
141 private:
142   std::vector<Bullet*> bullets;
143
144 public: // TODO make this private again
145   typedef std::vector<InteractiveObject*> InteractiveObjects;
146   InteractiveObjects interactive_objects;
147   typedef std::vector<GameObject*> GameObjects;
148   GameObjects gameobjects;
149
150 private:
151   void fix_old_tiles();
152   
153   /// container for newly created objects, they'll be added in Sector::action
154   GameObjects gameobjects_new;
155   
156   typedef std::vector<SpawnPoint*> SpawnPoints;
157   SpawnPoints spawnpoints;
158
159   int currentmusic;
160 };
161
162 #endif
163