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 "util/reader_fwd.hpp"
27 #include "util/writer_fwd.hpp"
28 #include "util/currenton.hpp"
29 #include "video/color.hpp"
30 #include "object/anchor_point.hpp"
45 class ScriptInterpreter;
61 * Represents one of (potentially) multiple, separate parts of a Level.
63 * Sectors contain GameObjects, e.g. Badguys and Players.
65 class Sector : public scripting::SSector,
66 public Currenton<Sector>
69 Sector(Level* parent);
75 /// read sector from lisp file
76 void parse(const Reader& lisp);
77 void parse_old_format(const Reader& lisp);
79 /// activates this sector (change music, initialize player class, ...)
80 void activate(const std::string& spawnpoint);
81 void activate(const Vector& player_pos);
84 void update(float elapsed_time);
85 void update_game_objects();
87 void draw(DrawingContext& context);
90 * runs a script in the context of the sector (sector_table will be the
91 * roottable of this squirrel VM)
93 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
96 void add_object(GameObject* object);
98 void set_name(const std::string& name_)
99 { this->name = name_; }
100 const std::string& get_name() const
104 * tests if a given rectangle is inside the sector
105 * (a rectangle that is on top of the sector is considered inside)
107 bool inside(const Rectf& rectangle) const;
109 void play_music(MusicType musictype);
110 MusicType get_music_type();
112 bool add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir);
113 bool add_smoke_cloud(const Vector& pos);
115 /** get currently activated sector. */
116 static Sector* current()
119 /** Get total number of badguys */
120 int get_total_badguys();
122 /** Get total number of GameObjects of given type */
123 template<class T> int get_total_count()
126 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
127 if (dynamic_cast<T*>(*i)) total++;
132 void collision_tilemap(collision::Constraints* constraints,
133 const Vector& movement, const Rectf& dest,
134 MovingObject &object) const;
137 * Checks if the specified rectangle is free of (solid) tiles.
138 * Note that this does not include static objects, e.g. bonus blocks.
140 bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false) const;
142 * Checks if the specified rectangle is free of both
143 * 1.) solid tiles and
144 * 2.) MovingObjects in COLGROUP_STATIC.
145 * Note that this does not include badguys or players.
147 bool is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
149 * Checks if the specified rectangle is free of both
150 * 1.) solid tiles and
151 * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
152 * This includes badguys and players.
154 bool is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object = 0) const;
157 * returns a list of players currently in the sector
159 std::vector<Player*> get_players() {
160 return std::vector<Player*>(1, this->player);
162 Player *get_nearest_player (const Vector& pos);
163 Player *get_nearest_player (const Rectf& pos)
165 return (get_nearest_player (get_anchor_pos (pos, ANCHOR_MIDDLE)));
168 std::vector<MovingObject*> get_nearby_objects (const Vector& center, float max_distance);
170 Rectf get_active_region();
173 * returns the width (in px) of a sector)
175 float get_width() const;
178 * returns the height (in px) of a sector)
180 float get_height() const;
183 * globally changes solid tilemaps' tile ids
185 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
187 typedef std::vector<GameObject*> GameObjects;
188 typedef std::vector<MovingObject*> MovingObjects;
189 typedef std::vector<SpawnPoint*> SpawnPoints;
190 typedef std::vector<Portable*> Portables;
194 * get/set color of ambient light
196 void set_ambient_light(float red, float green, float blue);
197 float get_ambient_red();
198 float get_ambient_green();
199 float get_ambient_blue();
202 * set gravity throughout sector
204 void set_gravity(float gravity);
205 float get_gravity() const;
208 uint32_t collision_tile_attributes(const Rectf& dest) const;
210 void before_object_remove(GameObject* object);
211 bool before_object_add(GameObject* object);
213 void try_expose(GameObject* object);
214 void try_unexpose(GameObject* object);
215 void try_expose_me();
216 void try_unexpose_me();
218 /** Checks for all possible collisions. And calls the
219 collision_handlers, which the collision_objects provide for this
221 void handle_collisions();
224 * Does collision detection between 2 objects and does instant
225 * collision response handling in case of a collision
227 void collision_object(MovingObject* object1, MovingObject* object2) const;
230 * Does collision detection of an object against all other static
231 * objects (and the tilemap) in the level. Collision response is done
232 * for the first hit in time. (other hits get ignored, the function
233 * should be called repeatedly to resolve those)
235 * returns true if the collision detection should be aborted for this object
236 * (because of ABORT_MOVE in the collision response or no collisions)
238 void collision_static(collision::Constraints* constraints,
239 const Vector& movement, const Rectf& dest, MovingObject& object);
241 void collision_static_constrains(MovingObject& object);
243 GameObject* parse_object(const std::string& name, const Reader& lisp);
245 void fix_old_tiles();
248 static Sector* _current;
250 Level* level; /**< Parent level containing this sector */
254 std::vector<Bullet*> bullets;
256 std::string init_script;
258 /// container for newly created objects, they'll be added in Sector::update
259 GameObjects gameobjects_new;
261 MusicType currentmusic;
263 HSQOBJECT sector_table;
265 typedef std::vector<HSQOBJECT> ScriptList;
270 public: // TODO make this private again
271 /// show collision rectangles of moving objects (for debugging)
272 static bool show_collrects;
273 static bool draw_solids_only;
275 GameObjects gameobjects;
276 MovingObjects moving_objects;
277 SpawnPoints spawnpoints;
283 // some special objects, where we need direct access
284 // (try to avoid accessing them directly)
286 std::list<TileMap*> solid_tilemaps;
288 DisplayEffect* effect;
291 Sector(const Sector&);
292 Sector& operator=(const Sector&);