1 // SuperTux - A Jump'n Run
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #ifndef HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_SECTOR_HPP
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"
46 class ScriptInterpreter;
62 * Represents one of (potentially) multiple, separate parts of a Level.
64 * Sectors contain GameObjects, e.g. Badguys and Players.
66 class Sector : public scripting::SSector,
67 public Currenton<Sector>
70 Sector(Level* parent);
76 /// read sector from lisp file
77 void parse(const Reader& lisp);
78 void parse_old_format(const Reader& lisp);
80 /// activates this sector (change music, initialize player class, ...)
81 void activate(const std::string& spawnpoint);
82 void activate(const Vector& player_pos);
85 void update(float elapsed_time);
86 void update_game_objects();
88 void draw(DrawingContext& context);
91 * runs a script in the context of the sector (sector_table will be the
92 * roottable of this squirrel VM)
94 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
97 void add_object(GameObjectPtr object);
99 void set_name(const std::string& name_)
100 { this->name = name_; }
101 const std::string& get_name() const
105 * tests if a given rectangle is inside the sector
106 * (a rectangle that is on top of the sector is considered inside)
108 bool inside(const Rectf& rectangle) const;
110 void play_music(MusicType musictype);
112 MusicType get_music_type();
114 int get_active_bullets()
115 { return (int)bullets.size(); }
116 bool add_smoke_cloud(const Vector& pos);
118 /** get currently activated sector. */
119 static Sector* current()
122 /** Get total number of badguys */
123 int get_total_badguys();
125 /** Get total number of GameObjects of given type */
126 template<class T> int get_total_count()
129 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
130 if (dynamic_cast<T*>(i->get())) total++;
135 void collision_tilemap(collision::Constraints* constraints,
136 const Vector& movement, const Rectf& dest,
137 MovingObject &object) const;
140 * Checks if the specified rectangle is free of (solid) tiles.
141 * Note that this does not include static objects, e.g. bonus blocks.
143 bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false) const;
145 * Checks if the specified rectangle is free of both
146 * 1.) solid tiles and
147 * 2.) MovingObjects in COLGROUP_STATIC.
148 * Note that this does not include badguys or players.
150 bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
152 * Checks if the specified rectangle is free of both
153 * 1.) solid tiles and
154 * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
155 * This includes badguys and players.
157 bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = 0) const;
160 * returns a list of players currently in the sector
162 std::vector<Player*> get_players() {
163 return std::vector<Player*>(1, this->player);
165 Player *get_nearest_player (const Vector& pos);
166 Player *get_nearest_player (const Rectf& pos)
168 return (get_nearest_player (get_anchor_pos (pos, ANCHOR_MIDDLE)));
171 std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance);
173 Rectf get_active_region();
175 int get_foremost_layer();
178 * returns the width (in px) of a sector)
180 float get_width() const;
183 * returns the height (in px) of a sector)
185 float get_height() const;
188 * globally changes solid tilemaps' tile ids
190 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
192 typedef std::vector<GameObjectPtr> GameObjects;
193 typedef std::vector<MovingObject*> MovingObjects;
194 typedef std::vector<std::shared_ptr<SpawnPoint> > SpawnPoints;
195 typedef std::vector<Portable*> Portables;
199 * get/set color of ambient light
201 void set_ambient_light(float red, float green, float blue);
202 float get_ambient_red();
203 float get_ambient_green();
204 float get_ambient_blue();
207 * set gravity throughout sector
209 void set_gravity(float gravity);
210 float get_gravity() const;
213 uint32_t collision_tile_attributes(const Rectf& dest) const;
215 void before_object_remove(GameObjectPtr object);
216 bool before_object_add(GameObjectPtr object);
218 void try_expose(GameObjectPtr object);
219 void try_unexpose(GameObjectPtr object);
220 void try_expose_me();
221 void try_unexpose_me();
223 /** Checks for all possible collisions. And calls the
224 collision_handlers, which the collision_objects provide for this
226 void handle_collisions();
229 * Does collision detection between 2 objects and does instant
230 * collision response handling in case of a collision
232 void collision_object(MovingObject* object1, MovingObject* object2) const;
235 * Does collision detection of an object against all other static
236 * objects (and the tilemap) in the level. Collision response is done
237 * for the first hit in time. (other hits get ignored, the function
238 * should be called repeatedly to resolve those)
240 * returns true if the collision detection should be aborted for this object
241 * (because of ABORT_MOVE in the collision response or no collisions)
243 void collision_static(collision::Constraints* constraints,
244 const Vector& movement, const Rectf& dest, MovingObject& object);
246 void collision_static_constrains(MovingObject& object);
248 GameObjectPtr parse_object(const std::string& name, const Reader& lisp);
250 void fix_old_tiles();
252 int calculate_foremost_layer();
255 static Sector* _current;
257 Level* level; /**< Parent level containing this sector */
261 std::vector<Bullet*> bullets;
263 std::string init_script;
265 /// container for newly created objects, they'll be added in Sector::update
266 GameObjects gameobjects_new;
268 MusicType currentmusic;
270 HSQOBJECT sector_table;
272 typedef std::vector<HSQOBJECT> ScriptList;
279 public: // TODO make this private again
280 /// show collision rectangles of moving objects (for debugging)
281 static bool show_collrects;
282 static bool draw_solids_only;
284 GameObjects gameobjects;
285 MovingObjects moving_objects;
286 SpawnPoints spawnpoints;
292 // some special objects, where we need direct access
293 // (try to avoid accessing them directly)
295 std::list<TileMap*> solid_tilemaps;
297 DisplayEffect* effect;
300 Sector(const Sector&);
301 Sector& operator=(const Sector&);