Replaced Ref and RefCounter with std::shared_ptr<>
[supertux.git] / src / supertux / sector.hpp
1 //  SuperTux -  A Jump'n Run
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
19
20 #include <list>
21 #include <squirrel.h>
22 #include <stdint.h>
23
24 #include "scripting/ssector.hpp"
25 #include "supertux/direction.hpp"
26 #include "supertux/game_object_ptr.hpp"
27 #include "util/reader_fwd.hpp"
28 #include "util/writer_fwd.hpp"
29 #include "util/currenton.hpp"
30 #include "video/color.hpp"
31 #include "object/anchor_point.hpp"
32
33 namespace collision {
34 class Constraints;
35 }
36
37 class Vector;
38 class Rectf;
39 class Sprite;
40 class GameObject;
41 class Player;
42 class PlayerStatus;
43 class Camera;
44 class TileMap;
45 class Bullet;
46 class ScriptInterpreter;
47 class SpawnPoint;
48 class MovingObject;
49 class CollisionHit;
50 class Level;
51 class Portable;
52 class DrawingContext;
53 class DisplayEffect;
54
55 enum MusicType {
56   LEVEL_MUSIC,
57   HERRING_MUSIC,
58   HERRING_WARNING_MUSIC
59 };
60
61 /**
62  * Represents one of (potentially) multiple, separate parts of a Level.
63  *
64  * Sectors contain GameObjects, e.g. Badguys and Players.
65  */
66 class Sector : public scripting::SSector,
67                public Currenton<Sector>
68 {
69 public:
70   Sector(Level* parent);
71   ~Sector();
72
73   /// get parent level
74   Level* get_level();
75
76   /// read sector from lisp file
77   void parse(const Reader& lisp);
78   void parse_old_format(const Reader& lisp);
79
80   /// activates this sector (change music, initialize player class, ...)
81   void activate(const std::string& spawnpoint);
82   void activate(const Vector& player_pos);
83   void deactivate();
84
85   void update(float elapsed_time);
86   void update_game_objects();
87
88   void draw(DrawingContext& context);
89
90   /**
91    * runs a script in the context of the sector (sector_table will be the
92    * roottable of this squirrel VM)
93    */
94   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
95
96   /// adds a gameobject
97   void add_object(GameObjectPtr object);
98
99   void set_name(const std::string& name_)
100   { this->name = name_; }
101   const std::string& get_name() const
102   { return name; }
103
104   /**
105    * tests if a given rectangle is inside the sector
106    * (a rectangle that is on top of the sector is considered inside)
107    */
108   bool inside(const Rectf& rectangle) const;
109
110   void play_music(MusicType musictype);
111   MusicType get_music_type();
112
113   bool add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir);
114   bool add_smoke_cloud(const Vector& pos);
115
116   /** get currently activated sector. */
117   static Sector* current()
118   { return _current; }
119
120   /** Get total number of badguys */
121   int get_total_badguys();
122
123   /** Get total number of GameObjects of given type */
124   template<class T> int get_total_count()
125   {
126     int total = 0;
127     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
128       if (dynamic_cast<T*>(i->get())) total++;
129     }
130     return total;
131   }
132
133   void collision_tilemap(collision::Constraints* constraints,
134                          const Vector& movement, const Rectf& dest,
135                          MovingObject &object) const;
136
137   /**
138    * Checks if the specified rectangle is free of (solid) tiles.
139    * Note that this does not include static objects, e.g. bonus blocks.
140    */
141   bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false) const;
142   /**
143    * Checks if the specified rectangle is free of both
144    * 1.) solid tiles and
145    * 2.) MovingObjects in COLGROUP_STATIC.
146    * Note that this does not include badguys or players.
147    */
148   bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
149   /**
150    * Checks if the specified rectangle is free of both
151    * 1.) solid tiles and
152    * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
153    * This includes badguys and players.
154    */
155   bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = 0) const;
156
157   /**
158    * returns a list of players currently in the sector
159    */
160   std::vector<Player*> get_players() {
161     return std::vector<Player*>(1, this->player);
162   }
163   Player *get_nearest_player (const Vector& pos);
164   Player *get_nearest_player (const Rectf& pos)
165   {
166     return (get_nearest_player (get_anchor_pos (pos, ANCHOR_MIDDLE)));
167   }
168
169   std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance);
170
171   Rectf get_active_region();
172
173   /**
174    * returns the width (in px) of a sector)
175    */
176   float get_width() const;
177
178   /**
179    * returns the height (in px) of a sector)
180    */
181   float get_height() const;
182
183   /**
184    * globally changes solid tilemaps' tile ids
185    */
186   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
187
188   typedef std::vector<GameObjectPtr> GameObjects;
189   typedef std::vector<MovingObject*> MovingObjects;
190   typedef std::vector<std::shared_ptr<SpawnPoint> > SpawnPoints;
191   typedef std::vector<Portable*> Portables;
192
193   // --- scripting ---
194   /**
195    *  get/set color of ambient light
196    */
197   void set_ambient_light(float red, float green, float blue);
198   float get_ambient_red();
199   float get_ambient_green();
200   float get_ambient_blue();
201
202   /**
203    *  set gravity throughout sector
204    */
205   void set_gravity(float gravity);
206   float get_gravity() const;
207
208 private:
209   uint32_t collision_tile_attributes(const Rectf& dest) const;
210
211   void before_object_remove(GameObjectPtr object);
212   bool before_object_add(GameObjectPtr object);
213
214   void try_expose(GameObjectPtr object);
215   void try_unexpose(GameObjectPtr object);
216   void try_expose_me();
217   void try_unexpose_me();
218
219   /** Checks for all possible collisions. And calls the
220       collision_handlers, which the collision_objects provide for this
221       case (or not). */
222   void handle_collisions();
223
224   /**
225    * Does collision detection between 2 objects and does instant
226    * collision response handling in case of a collision
227    */
228   void collision_object(MovingObject* object1, MovingObject* object2) const;
229
230   /**
231    * Does collision detection of an object against all other static
232    * objects (and the tilemap) in the level. Collision response is done
233    * for the first hit in time. (other hits get ignored, the function
234    * should be called repeatedly to resolve those)
235    *
236    * returns true if the collision detection should be aborted for this object
237    * (because of ABORT_MOVE in the collision response or no collisions)
238    */
239   void collision_static(collision::Constraints* constraints,
240                         const Vector& movement, const Rectf& dest, MovingObject& object);
241
242   void collision_static_constrains(MovingObject& object);
243
244   GameObjectPtr parse_object(const std::string& name, const Reader& lisp);
245
246   void fix_old_tiles();
247
248 private:
249   static Sector* _current;
250
251   Level* level; /**< Parent level containing this sector */
252
253   std::string name;
254
255   std::vector<Bullet*> bullets;
256
257   std::string init_script;
258
259   /// container for newly created objects, they'll be added in Sector::update
260   GameObjects gameobjects_new;
261
262   MusicType currentmusic;
263
264   HSQOBJECT sector_table;
265   /// sector scripts
266   typedef std::vector<HSQOBJECT> ScriptList;
267   ScriptList scripts;
268
269   Color ambient_light;
270
271 public: // TODO make this private again
272   /// show collision rectangles of moving objects (for debugging)
273   static bool show_collrects;
274   static bool draw_solids_only;
275
276   GameObjects gameobjects;
277   MovingObjects moving_objects;
278   SpawnPoints spawnpoints;
279   Portables portables;
280
281   std::string music;
282   float gravity;
283
284   // some special objects, where we need direct access
285   // (try to avoid accessing them directly)
286   Player* player;
287   std::list<TileMap*> solid_tilemaps;
288   Camera* camera;
289   DisplayEffect* effect;
290
291 private:
292   Sector(const Sector&);
293   Sector& operator=(const Sector&);
294 };
295
296 #endif
297
298 /* EOF */