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"
43 class ScriptInterpreter;
59 * Represents one of (potentially) multiple, separate parts of a Level.
61 * Sectors contain GameObjects, e.g. Badguys and Players.
63 class Sector : public Scripting::SSector,
64 public Currenton<Sector>
67 Sector(Level* parent);
73 /// read sector from lisp file
74 void parse(const Reader& lisp);
75 void parse_old_format(const Reader& lisp);
77 /// activates this sector (change music, initialize player class, ...)
78 void activate(const std::string& spawnpoint);
79 void activate(const Vector& player_pos);
82 void update(float elapsed_time);
83 void update_game_objects();
85 void draw(DrawingContext& context);
88 * runs a script in the context of the sector (sector_table will be the
89 * roottable of this squirrel VM)
91 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
94 void add_object(GameObject* object);
96 void set_name(const std::string& name)
97 { this->name = name; }
98 const std::string& get_name() const
102 * tests if a given rectangle is inside the sector
103 * (a rectangle that is on top of the sector is considered inside)
105 bool inside(const Rect& rectangle) const;
107 void play_music(MusicType musictype);
108 MusicType get_music_type();
110 bool add_bullet(const Vector& pos, float xm, Direction dir);
111 bool add_smoke_cloud(const Vector& pos);
113 /** get currently activated sector. */
114 static Sector* current()
117 /** Get total number of badguys */
118 int get_total_badguys();
120 /** Get total number of GameObjects of given type */
121 template<class T> int get_total_count()
124 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
125 if (dynamic_cast<T*>(*i)) total++;
130 void collision_tilemap(collision::Constraints* constraints,
131 const Vector& movement, const Rect& dest) const;
134 * Checks if the specified rectangle is free of (solid) tiles.
135 * Note that this does not include static objects, e.g. bonus blocks.
137 bool is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid = false) const;
139 * Checks if the specified rectangle is free of both
140 * 1.) solid tiles and
141 * 2.) MovingObjects in COLGROUP_STATIC.
142 * Note that this does not include badguys or players.
144 bool is_free_of_statics(const Rect& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
146 * Checks if the specified rectangle is free of both
147 * 1.) solid tiles and
148 * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
149 * This includes badguys and players.
151 bool is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object = 0) const;
154 * returns a list of players currently in the sector
156 std::vector<Player*> get_players() {
157 return std::vector<Player*>(1, this->player);
160 Rect get_active_region();
163 * returns the width (in px) of a sector)
165 float get_width() const;
168 * returns the height (in px) of a sector)
170 float get_height() const;
173 * globally changes solid tilemaps' tile ids
175 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
177 typedef std::vector<GameObject*> GameObjects;
178 typedef std::vector<MovingObject*> MovingObjects;
179 typedef std::vector<SpawnPoint*> SpawnPoints;
180 typedef std::vector<Portable*> Portables;
184 * get/set color of ambient light
186 void set_ambient_light(float red, float green, float blue);
187 float get_ambient_red();
188 float get_ambient_green();
189 float get_ambient_blue();
192 * set gravity throughout sector
194 void set_gravity(float gravity);
195 float get_gravity() const;
198 Level* level; /**< Parent level containing this sector */
199 uint32_t collision_tile_attributes(const Rect& dest) const;
201 void before_object_remove(GameObject* object);
202 bool before_object_add(GameObject* object);
204 void try_expose(GameObject* object);
205 void try_unexpose(GameObject* object);
206 void try_expose_me();
207 void try_unexpose_me();
209 /** Checks for all possible collisions. And calls the
210 collision_handlers, which the collision_objects provide for this
212 void handle_collisions();
215 * Does collision detection between 2 objects and does instant
216 * collision response handling in case of a collision
218 void collision_object(MovingObject* object1, MovingObject* object2) const;
221 * Does collision detection of an object against all other static
222 * objects (and the tilemap) in the level. Collision response is done
223 * for the first hit in time. (other hits get ignored, the function
224 * should be called repeatedly to resolve those)
226 * returns true if the collision detection should be aborted for this object
227 * (because of ABORT_MOVE in the collision response or no collisions)
229 void collision_static(collision::Constraints* constraints,
230 const Vector& movement, const Rect& dest, GameObject& object);
232 void collision_static_constrains(MovingObject& object);
234 GameObject* parse_object(const std::string& name, const Reader& lisp);
236 void fix_old_tiles();
238 static Sector* _current;
242 std::vector<Bullet*> bullets;
244 std::string init_script;
246 /// container for newly created objects, they'll be added in Sector::update
247 GameObjects gameobjects_new;
249 MusicType currentmusic;
251 HSQOBJECT sector_table;
253 typedef std::vector<HSQOBJECT> ScriptList;
258 public: // TODO make this private again
259 /// show collision rectangles of moving objects (for debugging)
260 static bool show_collrects;
261 static bool draw_solids_only;
263 GameObjects gameobjects;
264 MovingObjects moving_objects;
265 SpawnPoints spawnpoints;
271 // some special objects, where we need direct access
272 // (try to avoid accessing them directly)
274 std::list<TileMap*> solid_tilemaps;
276 DisplayEffect* effect;
279 Sector(const Sector&);
280 Sector& operator=(const Sector&);