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 "math/vector.hpp"
30 #include "video/drawing_context.hpp"
31 #include "script_interface.hpp"
32 #include "scripting/ssector.hpp"
49 class ScriptInterpreter;
63 * This class holds a sector (a part of a level) and all the game objects in
66 class Sector : public Scripting::SSector
69 Sector(Level* parent);
75 /// read sector from lisp file
76 void parse(const lisp::Lisp& lisp);
77 void parse_old_format(const lisp::Lisp& lisp);
78 /// write sector to lisp file
79 void write(lisp::Writer& writer);
81 /// activates this sector (change music, intialize player class, ...)
82 void activate(const std::string& spawnpoint);
83 void activate(const Vector& player_pos);
86 void update(float elapsed_time);
87 void update_game_objects();
89 void draw(DrawingContext& context);
92 * runs a script in the context of the sector (sector_table will be the
93 * roottable of this squirrel VM)
95 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
98 void add_object(GameObject* object);
100 void set_name(const std::string& name)
101 { this->name = name; }
102 const std::string& get_name() const
106 * tests if a given rectangle is inside the sector
107 * (a rectangle that is on top of the sector is considered inside)
109 bool inside(const Rect& rectangle) const;
111 void play_music(MusicType musictype);
112 MusicType get_music_type();
114 bool add_bullet(const Vector& pos, float xm, Direction dir);
115 bool add_smoke_cloud(const Vector& pos);
117 /** get currently activated sector. */
118 static Sector* current()
121 /** Get total number of badguys */
122 int get_total_badguys();
124 /** Get total number of GameObjects of given type */
125 template<class T> int get_total_count()
128 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
129 if (dynamic_cast<T*>(*i)) total++;
134 void collision_tilemap(collision::Constraints* constraints,
135 const Vector& movement, const Rect& dest) const;
138 * Checks if the specified rectangle is free of (solid) tiles.
139 * Note that this does not include static objects, e.g. bonus blocks.
141 bool is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid = false) const;
143 * Checks if the specified rectangle is free of both
144 * 1.) solid tiles and
145 * 2.) MovingObjects in COLGROUP_STATIC.
146 * Note that this does not include badguys or players.
148 bool is_free_of_statics(const Rect& rect, const MovingObject* ignore_object = 0, const bool ignoreUnisolid = false) const;
150 * Checks if the specified rectangle is free of both
151 * 1.) solid tiles and
152 * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
153 * This includes badguys and players.
155 bool is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object = 0) const;
158 * returns a list of players currently in the sector
160 std::vector<Player*> get_players() {
161 return std::vector<Player*>(1, this->player);
164 Rect get_active_region();
167 * returns the width (in px) of a sector)
169 float get_width() const;
172 * returns the height (in px) of a sector)
174 float get_height() const;
177 * globally changes solid tilemaps' tile ids
179 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
181 typedef std::vector<GameObject*> GameObjects;
182 typedef std::vector<MovingObject*> MovingObjects;
183 typedef std::vector<SpawnPoint*> SpawnPoints;
184 typedef std::vector<Portable*> Portables;
188 * get/set color of ambient light
190 void set_ambient_light(float red, float green, float blue);
191 float get_ambient_red();
192 float get_ambient_green();
193 float get_ambient_blue();
196 Level* level; /**< Parent level containing this sector */
197 uint32_t collision_tile_attributes(const Rect& dest) const;
199 void before_object_remove(GameObject* object);
200 bool before_object_add(GameObject* object);
202 void try_expose(GameObject* object);
203 void try_unexpose(GameObject* object);
204 void try_expose_me();
205 void try_unexpose_me();
207 /** Checks for all possible collisions. And calls the
208 collision_handlers, which the collision_objects provide for this
210 void handle_collisions();
213 * Does collision detection between 2 objects and does instant
214 * collision response handling in case of a collision
216 void collision_object(MovingObject* object1, MovingObject* object2) const;
219 * Does collision detection of an object against all other static
220 * objects (and the tilemap) in the level. Collision response is done
221 * for the first hit in time. (other hits get ignored, the function
222 * should be called repeatedly to resolve those)
224 * returns true if the collision detection should be aborted for this object
225 * (because of ABORT_MOVE in the collision response or no collisions)
227 void collision_static(collision::Constraints* constraints,
228 const Vector& movement, const Rect& dest, GameObject& object);
230 void collision_static_constrains(MovingObject& object);
232 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
234 void fix_old_tiles();
236 static Sector* _current;
240 std::vector<Bullet*> bullets;
242 std::string init_script;
244 /// container for newly created objects, they'll be added in Sector::update
245 GameObjects gameobjects_new;
247 MusicType currentmusic;
249 HSQOBJECT sector_table;
251 typedef std::vector<HSQOBJECT> ScriptList;
256 public: // TODO make this private again
257 /// show collision rectangles of moving objects (for debugging)
258 static bool show_collrects;
259 static bool draw_solids_only;
261 GameObjects gameobjects;
262 MovingObjects moving_objects;
263 SpawnPoints spawnpoints;
269 // some special objects, where we need direct access
270 // (try to avoid accessing them directly)
272 std::list<TileMap*> solid_tilemaps;