- More work on scripting interface
[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 Rectangle;
36 class Sprite;
37 class GameObject;
38 class Player;
39 class Camera;
40 class TileMap;
41 class Bullet;
42 class CollisionGrid;
43
44 class SpawnPoint
45 {
46 public:
47   std::string name;
48   Vector pos;
49 };
50
51 enum MusicType {
52   LEVEL_MUSIC,
53   HERRING_MUSIC
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);
73   void activate(const Vector& player_pos);
74
75   void action(float elapsed_time);
76   void update_game_objects();
77
78   void draw(DrawingContext& context);
79
80   /// adds a gameobject
81   void add_object(GameObject* object);
82
83   void set_name(const std::string& name)
84   { this->name = name; }
85   const std::string& get_name() const
86   { return name; }
87
88   /// tests if a given rectangle is inside the sector
89   bool inside(const Rectangle& rectangle) const;
90
91   void play_music(MusicType musictype);
92   MusicType get_music_type();
93   
94   /** Checks for all possible collisions. And calls the
95       collision_handlers, which the collision_objects provide for this
96       case (or not). */
97   void collision_handler();
98
99   bool add_bullet(const Vector& pos, float xm, Direction dir);
100   bool add_smoke_cloud(const Vector& pos);
101   void add_floating_text(const Vector& pos, const std::string& text);
102                                                                                 
103   /** @evil@ but can't always be avoided in current design... */
104   static Sector* current()
105   { return _current; }
106
107   /** Get total number of badguys */
108   int get_total_badguys();
109
110   // make this private again soon
111   void collision_tilemap(MovingObject* object, int depth);
112
113 private:
114   void collision_object(MovingObject* object1, MovingObject* object2);
115   
116   void load_music();
117   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
118   
119   static Sector* _current;
120   
121   std::string name;
122
123   MusicRef level_song;
124
125 public:
126   std::string song_title;
127   float gravity;
128
129   // some special objects, where we need direct access
130   Player* player;
131   TileMap* solids;
132   Camera* camera;
133   
134 private:
135   std::vector<Bullet*> bullets;
136
137 public: // TODO make this private again
138   typedef std::vector<GameObject*> GameObjects;
139   GameObjects gameobjects;
140   typedef std::vector<SpawnPoint*> SpawnPoints;
141   SpawnPoints spawnpoints;                       
142
143   Rectangle get_active_region();
144
145 private:
146   void fix_old_tiles();
147   
148   /// container for newly created objects, they'll be added in Sector::action
149   GameObjects gameobjects_new;
150  
151   MusicType currentmusic;
152
153   CollisionGrid* grid;
154 };
155
156 #endif
157