80ed7340a7218a3ee6346343fd59cf23eca52f29
[supertux.git] / src / sector.hpp
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 #include <squirrel.h>
25
26 #include "direction.hpp"
27 #include "math/vector.hpp"
28 #include "video/drawing_context.hpp"
29
30 namespace lisp {
31 class Lisp;
32 class Writer;
33 }
34
35 class Rect;
36 class Sprite;
37 class GameObject;
38 class Player;
39 class Camera;
40 class TileMap;
41 class Bullet;
42 class CollisionGrid;
43 class ScriptInterpreter;
44 class SpawnPoint;
45 class MovingObject;
46 class CollisionHit;
47
48 enum MusicType {
49   LEVEL_MUSIC,
50   HERRING_MUSIC
51 };
52
53 /** This class holds a sector (a part of a level) and all the game objects
54  * (badguys, player, background, tilemap, ...)
55  */
56 class Sector
57 {
58 public:
59   Sector();
60   ~Sector();
61
62   /// read sector from lisp file
63   void parse(const lisp::Lisp& lisp);
64   void parse_old_format(const lisp::Lisp& lisp);
65   /// write sector to lisp file
66   void write(lisp::Writer& writer);
67
68   /// activates this sector (change music, intialize player class, ...)
69   void activate(const std::string& spawnpoint);
70   void activate(const Vector& player_pos);
71   void deactivate();
72
73   void update(float elapsed_time);
74   void update_game_objects();
75
76   void draw(DrawingContext& context);
77
78   /**
79    * runs a script in the context of the sector (sector_table will be the
80    * roottable of this squirrel VM)
81    */
82   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
83
84   /// adds a gameobject
85   void add_object(GameObject* object);
86
87   void set_name(const std::string& name)
88   { this->name = name; }
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 Rect& rectangle) const;
94
95   void play_music(MusicType musictype);
96   MusicType get_music_type();
97   
98   bool add_bullet(const Vector& pos, float xm, Direction dir);
99   bool add_smoke_cloud(const Vector& pos);
100   void add_floating_text(const Vector& pos, const std::string& text);
101                                                                                 
102   /** get currently activated sector. */
103   static Sector* current()
104   { return _current; }
105
106   /** Get total number of badguys */
107   int get_total_badguys();
108
109   void collision_tilemap(const Rect& dest, const Vector& movement, CollisionHit& hit) const;
110
111   /** Checks if at the specified rectangle are gameobjects with STATIC flag set
112    * (or solid tiles from the tilemap)
113    */
114   bool is_free_space(const Rect& rect) const;
115
116   /**
117    * returns a list of players currently in the sector
118    */
119   std::vector<Player*> get_players() {
120     return std::vector<Player*>(1, this->player);
121   }
122
123   Rect get_active_region();
124
125 private:
126   uint32_t collision_tile_attributes(const Rect& dest) const;
127
128   void before_object_remove(GameObject* object);
129   bool before_object_add(GameObject* object);
130
131   void try_expose(GameObject* object);
132   void try_unexpose(GameObject* object);
133   
134   bool collision_static(MovingObject* object, const Vector& movement);
135   
136   /** Checks for all possible collisions. And calls the
137       collision_handlers, which the collision_objects provide for this
138       case (or not). */
139   void handle_collisions();
140   
141   void collision_object(MovingObject* object1, MovingObject* object2) const;
142   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
143
144   void fix_old_tiles();
145
146   typedef std::vector<GameObject*> GameObjects;
147   typedef std::vector<MovingObject*> MovingObjects;
148   typedef std::vector<SpawnPoint*> SpawnPoints;
149
150   static Sector* _current;
151   
152   std::string name;
153
154   std::vector<Bullet*> bullets;
155
156   std::string init_script;
157
158   /// container for newly created objects, they'll be added in Sector::update
159   GameObjects gameobjects_new;
160  
161   MusicType currentmusic;
162
163   CollisionGrid* grid;
164
165   HSQOBJECT sector_table;
166
167 public: // TODO make this private again
168   GameObjects gameobjects;
169   MovingObjects moving_objects;
170   SpawnPoints spawnpoints;                       
171
172   std::string music;
173   float gravity;
174
175   // some special objects, where we need direct access
176   // (try to avoid accessing them directly)
177   Player* player;
178   TileMap* solids;
179   Camera* camera;
180 };
181
182 #endif
183