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
27 #include "direction.hpp"
28 #include "video/color.hpp"
29 #include "scripting/ssector.hpp"
47 class ScriptInterpreter;
62 * This class holds a sector (a part of a level) and all the game objects in
65 class Sector : public Scripting::SSector
68 Sector(Level* parent);
74 /// read sector from lisp file
75 void parse(const lisp::Lisp& lisp);
76 void parse_old_format(const lisp::Lisp& lisp);
77 /// write sector to lisp file
78 void write(lisp::Writer& writer);
80 /// activates this sector (change music, intialize player class, ...)
81 void activate(const std::string& spawnpoint);
82 void activate(const Vector& player_pos);
85 void update(float elapsed_time);
86 void update_game_objects();
88 void draw(DrawingContext& context);
91 * runs a script in the context of the sector (sector_table will be the
92 * roottable of this squirrel VM)
94 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
97 void add_object(GameObject* object);
99 void set_name(const std::string& name)
100 { this->name = name; }
101 const std::string& get_name() const
105 * tests if a given rectangle is inside the sector
106 * (a rectangle that is on top of the sector is considered inside)
108 bool inside(const Rect& rectangle) const;
110 void play_music(MusicType musictype);
111 MusicType get_music_type();
113 bool add_bullet(const Vector& pos, float xm, Direction dir);
114 bool add_smoke_cloud(const Vector& pos);
116 /** get currently activated sector. */
117 static Sector* current()
120 /** Get total number of badguys */
121 int get_total_badguys();
123 /** Get total number of GameObjects of given type */
124 template<class T> int get_total_count()
127 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
128 if (dynamic_cast<T*>(*i)) total++;
133 void collision_tilemap(collision::Constraints* constraints,
134 const Vector& movement, const Rect& dest) 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 Rect& 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 Rect& 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 Rect& 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);
163 Rect get_active_region();
166 * returns the width (in px) of a sector)
168 float get_width() const;
171 * returns the height (in px) of a sector)
173 float get_height() const;
176 * globally changes solid tilemaps' tile ids
178 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
180 typedef std::vector<GameObject*> GameObjects;
181 typedef std::vector<MovingObject*> MovingObjects;
182 typedef std::vector<SpawnPoint*> SpawnPoints;
183 typedef std::vector<Portable*> Portables;
187 * get/set color of ambient light
189 void set_ambient_light(float red, float green, float blue);
190 float get_ambient_red();
191 float get_ambient_green();
192 float get_ambient_blue();
195 Level* level; /**< Parent level containing this sector */
196 uint32_t collision_tile_attributes(const Rect& dest) const;
198 void before_object_remove(GameObject* object);
199 bool before_object_add(GameObject* object);
201 void try_expose(GameObject* object);
202 void try_unexpose(GameObject* object);
203 void try_expose_me();
204 void try_unexpose_me();
206 /** Checks for all possible collisions. And calls the
207 collision_handlers, which the collision_objects provide for this
209 void handle_collisions();
212 * Does collision detection between 2 objects and does instant
213 * collision response handling in case of a collision
215 void collision_object(MovingObject* object1, MovingObject* object2) const;
218 * Does collision detection of an object against all other static
219 * objects (and the tilemap) in the level. Collision response is done
220 * for the first hit in time. (other hits get ignored, the function
221 * should be called repeatedly to resolve those)
223 * returns true if the collision detection should be aborted for this object
224 * (because of ABORT_MOVE in the collision response or no collisions)
226 void collision_static(collision::Constraints* constraints,
227 const Vector& movement, const Rect& dest, GameObject& object);
229 void collision_static_constrains(MovingObject& object);
231 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
233 void fix_old_tiles();
235 static Sector* _current;
239 std::vector<Bullet*> bullets;
241 std::string init_script;
243 /// container for newly created objects, they'll be added in Sector::update
244 GameObjects gameobjects_new;
246 MusicType currentmusic;
248 HSQOBJECT sector_table;
250 typedef std::vector<HSQOBJECT> ScriptList;
255 public: // TODO make this private again
256 /// show collision rectangles of moving objects (for debugging)
257 static bool show_collrects;
258 static bool draw_solids_only;
260 GameObjects gameobjects;
261 MovingObjects moving_objects;
262 SpawnPoints spawnpoints;
268 // some special objects, where we need direct access
269 // (try to avoid accessing them directly)
271 std::list<TileMap*> solid_tilemaps;