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"
47 class ScriptInterpreter;
61 * This class holds a sector (a part of a level) and all the game objects in
67 Sector(Level* parent);
73 /// read sector from lisp file
74 void parse(const lisp::Lisp& lisp);
75 void parse_old_format(const lisp::Lisp& lisp);
76 /// write sector to lisp file
77 void write(lisp::Writer& writer);
79 /// activates this sector (change music, intialize player class, ...)
80 void activate(const std::string& spawnpoint);
81 void activate(const Vector& player_pos);
84 void update(float elapsed_time);
85 void update_game_objects();
87 void draw(DrawingContext& context);
90 * runs a script in the context of the sector (sector_table will be the
91 * roottable of this squirrel VM)
93 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
96 void add_object(GameObject* object);
98 void set_name(const std::string& name)
99 { this->name = name; }
100 const std::string& get_name() const
104 * tests if a given rectangle is inside the sector
105 * (a rectangle that is on top of the sector is considered inside)
107 bool inside(const Rect& rectangle) const;
109 void play_music(MusicType musictype);
110 MusicType get_music_type();
112 bool add_bullet(const Vector& pos, float xm, Direction dir);
113 bool add_smoke_cloud(const Vector& pos);
115 /** get currently activated sector. */
116 static Sector* current()
119 /** Get total number of badguys */
120 int get_total_badguys();
122 /** Get total number of GameObjects of given type */
123 template<class T> int get_total_count()
126 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
127 if (dynamic_cast<T*>(*i)) total++;
132 void collision_tilemap(collision::Constraints* constraints,
133 const Vector& movement, const Rect& dest) const;
135 /** Checks if at the specified rectangle are gameobjects with STATIC flag set
136 * (or solid tiles from the tilemap).
137 * This does not(!) include badguys or players.
139 bool is_free_space(const Rect& rect) const;
142 * returns a list of players currently in the sector
144 std::vector<Player*> get_players() {
145 return std::vector<Player*>(1, this->player);
148 Rect get_active_region();
151 * returns the width (in px) of a sector)
153 float get_width() const;
156 * returns the height (in px) of a sector)
158 float get_height() const;
161 * globally changes solid tilemaps' tile ids
163 void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
165 typedef std::vector<GameObject*> GameObjects;
166 typedef std::vector<MovingObject*> MovingObjects;
167 typedef std::vector<SpawnPoint*> SpawnPoints;
168 typedef std::vector<Portable*> Portables;
171 Level* level; /**< Parent level containing this sector */
172 uint32_t collision_tile_attributes(const Rect& dest) const;
174 void before_object_remove(GameObject* object);
175 bool before_object_add(GameObject* object);
177 void try_expose(GameObject* object);
178 void try_unexpose(GameObject* object);
180 /** Checks for all possible collisions. And calls the
181 collision_handlers, which the collision_objects provide for this
183 void handle_collisions();
186 * Does collision detection between 2 objects and does instant
187 * collision response handling in case of a collision
189 void collision_object(MovingObject* object1, MovingObject* object2) const;
192 * Does collision detection of an object against all other static
193 * objects (and the tilemap) in the level. Collision response is done
194 * for the first hit in time. (other hits get ignored, the function
195 * should be called repeatedly to resolve those)
197 * returns true if the collision detection should be aborted for this object
198 * (because of ABORT_MOVE in the collision response or no collisions)
200 void collision_static(collision::Constraints* constraints,
201 const Vector& movement, const Rect& dest, GameObject& object);
203 void collision_static_constrains(MovingObject& object);
205 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
207 void fix_old_tiles();
209 static Sector* _current;
213 std::vector<Bullet*> bullets;
215 std::string init_script;
217 /// container for newly created objects, they'll be added in Sector::update
218 GameObjects gameobjects_new;
220 MusicType currentmusic;
222 HSQOBJECT sector_table;
224 typedef std::vector<HSQOBJECT> ScriptList;
229 public: // TODO make this private again
230 /// show collision rectangles of moving objects (for debugging)
231 static bool show_collrects;
232 static bool draw_solids_only;
234 GameObjects gameobjects;
235 MovingObjects moving_objects;
236 SpawnPoints spawnpoints;
242 // some special objects, where we need direct access
243 // (try to avoid accessing them directly)
245 std::list<TileMap*> solid_tilemaps;