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;
63 * Represents one of (potentially) multiple, separate parts of a Level.
65 * Sectors contain GameObjects, e.g. Badguys and Players.
67 class Sector : public Scripting::SSector
70 Sector(Level* parent);
76 /// read sector from lisp file
77 void parse(const lisp::Lisp& lisp);
78 void parse_old_format(const lisp::Lisp& lisp);
79 /// write sector to lisp file
80 void write(lisp::Writer& writer);
82 /// activates this sector (change music, intialize player class, ...)
83 void activate(const std::string& spawnpoint);
84 void activate(const Vector& player_pos);
87 void update(float elapsed_time);
88 void update_game_objects();
90 void draw(DrawingContext& context);
93 * runs a script in the context of the sector (sector_table will be the
94 * roottable of this squirrel VM)
96 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
99 void add_object(GameObject* object);
101 void set_name(const std::string& name)
102 { this->name = name; }
103 const std::string& get_name() const
107 * tests if a given rectangle is inside the sector
108 * (a rectangle that is on top of the sector is considered inside)
110 bool inside(const Rect& rectangle) const;
112 void play_music(MusicType musictype);
113 MusicType get_music_type();
115 bool add_bullet(const Vector& pos, float xm, Direction dir);
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)) total++;
135 void collision_tilemap(collision::Constraints* constraints,
136 const Vector& movement, const Rect& dest) const;
139 * Checks if the specified rectangle is free of (solid) tiles.
140 * Note that this does not include static objects, e.g. bonus blocks.
142 bool is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid = false) const;
144 * Checks if the specified rectangle is free of both
145 * 1.) solid tiles and
146 * 2.) MovingObjects in COLGROUP_STATIC.
147 * Note that this does not include badguys or players.
149 bool is_free_of_statics(const Rect& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
151 * Checks if the specified rectangle is free of both
152 * 1.) solid tiles and
153 * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
154 * This includes badguys and players.
156 bool is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object = 0) const;
159 * returns a list of players currently in the sector
161 std::vector<Player*> get_players() {
162 return std::vector<Player*>(1, this->player);
165 Rect get_active_region();
168 * returns the width (in px) of a sector)
170 float get_width() const;
173 * returns the height (in px) of a sector)
175 float get_height() const;
178 * globally changes solid tilemaps' tile ids
180 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
182 typedef std::vector<GameObject*> GameObjects;
183 typedef std::vector<MovingObject*> MovingObjects;
184 typedef std::vector<SpawnPoint*> SpawnPoints;
185 typedef std::vector<Portable*> Portables;
189 * get/set color of ambient light
191 void set_ambient_light(float red, float green, float blue);
192 float get_ambient_red();
193 float get_ambient_green();
194 float get_ambient_blue();
197 Level* level; /**< Parent level containing this sector */
198 uint32_t collision_tile_attributes(const Rect& dest) const;
200 void before_object_remove(GameObject* object);
201 bool before_object_add(GameObject* object);
203 void try_expose(GameObject* object);
204 void try_unexpose(GameObject* object);
205 void try_expose_me();
206 void try_unexpose_me();
208 /** Checks for all possible collisions. And calls the
209 collision_handlers, which the collision_objects provide for this
211 void handle_collisions();
214 * Does collision detection between 2 objects and does instant
215 * collision response handling in case of a collision
217 void collision_object(MovingObject* object1, MovingObject* object2) const;
220 * Does collision detection of an object against all other static
221 * objects (and the tilemap) in the level. Collision response is done
222 * for the first hit in time. (other hits get ignored, the function
223 * should be called repeatedly to resolve those)
225 * returns true if the collision detection should be aborted for this object
226 * (because of ABORT_MOVE in the collision response or no collisions)
228 void collision_static(collision::Constraints* constraints,
229 const Vector& movement, const Rect& dest, GameObject& object);
231 void collision_static_constrains(MovingObject& object);
233 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
235 void fix_old_tiles();
237 static Sector* _current;
241 std::vector<Bullet*> bullets;
243 std::string init_script;
245 /// container for newly created objects, they'll be added in Sector::update
246 GameObjects gameobjects_new;
248 MusicType currentmusic;
250 HSQOBJECT sector_table;
252 typedef std::vector<HSQOBJECT> ScriptList;
257 public: // TODO make this private again
258 /// show collision rectangles of moving objects (for debugging)
259 static bool show_collrects;
260 static bool draw_solids_only;
262 GameObjects gameobjects;
263 MovingObjects moving_objects;
264 SpawnPoints spawnpoints;
270 // some special objects, where we need direct access
271 // (try to avoid accessing them directly)
273 std::list<TileMap*> solid_tilemaps;