3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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
28 #include "direction.hpp"
29 #include "video/color.hpp"
30 #include "scripting/ssector.hpp"
48 class ScriptInterpreter;
64 * Represents one of (potentially) multiple, separate parts of a Level.
66 * Sectors contain GameObjects, e.g. Badguys and Players.
68 class Sector : public Scripting::SSector
71 Sector(Level* parent);
77 /// read sector from lisp file
78 void parse(const lisp::Lisp& lisp);
79 void parse_old_format(const lisp::Lisp& lisp);
80 /// write sector to lisp file
81 void write(lisp::Writer& writer);
83 /// activates this sector (change music, initialize player class, ...)
84 void activate(const std::string& spawnpoint);
85 void activate(const Vector& player_pos);
88 void update(float elapsed_time);
89 void update_game_objects();
91 void draw(DrawingContext& context);
94 * runs a script in the context of the sector (sector_table will be the
95 * roottable of this squirrel VM)
97 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
100 void add_object(GameObject* object);
102 void set_name(const std::string& name)
103 { this->name = name; }
104 const std::string& get_name() const
108 * tests if a given rectangle is inside the sector
109 * (a rectangle that is on top of the sector is considered inside)
111 bool inside(const Rect& rectangle) const;
113 void play_music(MusicType musictype);
114 MusicType get_music_type();
116 bool add_bullet(const Vector& pos, float xm, Direction dir);
117 bool add_smoke_cloud(const Vector& pos);
119 /** get currently activated sector. */
120 static Sector* current()
123 /** Get total number of badguys */
124 int get_total_badguys();
126 /** Get total number of GameObjects of given type */
127 template<class T> int get_total_count()
130 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
131 if (dynamic_cast<T*>(*i)) total++;
136 void collision_tilemap(collision::Constraints* constraints,
137 const Vector& movement, const Rect& dest) 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 Rect& 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 Rect& 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 Rect& 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);
166 Rect get_active_region();
169 * returns the width (in px) of a sector)
171 float get_width() const;
174 * returns the height (in px) of a sector)
176 float get_height() const;
179 * globally changes solid tilemaps' tile ids
181 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
183 typedef std::vector<GameObject*> GameObjects;
184 typedef std::vector<MovingObject*> MovingObjects;
185 typedef std::vector<SpawnPoint*> SpawnPoints;
186 typedef std::vector<Portable*> Portables;
190 * get/set color of ambient light
192 void set_ambient_light(float red, float green, float blue);
193 float get_ambient_red();
194 float get_ambient_green();
195 float get_ambient_blue();
198 * set gravity throughout sector
200 void set_gravity(float gravity);
203 Level* level; /**< Parent level containing this sector */
204 uint32_t collision_tile_attributes(const Rect& dest) const;
206 void before_object_remove(GameObject* object);
207 bool before_object_add(GameObject* object);
209 void try_expose(GameObject* object);
210 void try_unexpose(GameObject* object);
211 void try_expose_me();
212 void try_unexpose_me();
214 /** Checks for all possible collisions. And calls the
215 collision_handlers, which the collision_objects provide for this
217 void handle_collisions();
220 * Does collision detection between 2 objects and does instant
221 * collision response handling in case of a collision
223 void collision_object(MovingObject* object1, MovingObject* object2) const;
226 * Does collision detection of an object against all other static
227 * objects (and the tilemap) in the level. Collision response is done
228 * for the first hit in time. (other hits get ignored, the function
229 * should be called repeatedly to resolve those)
231 * returns true if the collision detection should be aborted for this object
232 * (because of ABORT_MOVE in the collision response or no collisions)
234 void collision_static(collision::Constraints* constraints,
235 const Vector& movement, const Rect& dest, GameObject& object);
237 void collision_static_constrains(MovingObject& object);
239 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
241 void fix_old_tiles();
243 static Sector* _current;
247 std::vector<Bullet*> bullets;
249 std::string init_script;
251 /// container for newly created objects, they'll be added in Sector::update
252 GameObjects gameobjects_new;
254 MusicType currentmusic;
256 HSQOBJECT sector_table;
258 typedef std::vector<HSQOBJECT> ScriptList;
263 public: // TODO make this private again
264 /// show collision rectangles of moving objects (for debugging)
265 static bool show_collrects;
266 static bool draw_solids_only;
268 GameObjects gameobjects;
269 MovingObjects moving_objects;
270 SpawnPoints spawnpoints;
276 // some special objects, where we need direct access
277 // (try to avoid accessing them directly)
279 std::list<TileMap*> solid_tilemaps;
281 DisplayEffect* effect;