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 "math/vector.hpp"
29 #include "video/drawing_context.hpp"
44 class ScriptInterpreter;
57 * This class holds a sector (a part of a level) and all the game objects in
63 Sector(Level* parent);
69 /// read sector from lisp file
70 void parse(const lisp::Lisp& lisp);
71 void parse_old_format(const lisp::Lisp& lisp);
72 /// write sector to lisp file
73 void write(lisp::Writer& writer);
75 /// activates this sector (change music, intialize player class, ...)
76 void activate(const std::string& spawnpoint);
77 void activate(const Vector& player_pos);
80 void update(float elapsed_time);
81 void update_game_objects();
83 void draw(DrawingContext& context);
86 * runs a script in the context of the sector (sector_table will be the
87 * roottable of this squirrel VM)
89 HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
92 void add_object(GameObject* object);
94 void set_name(const std::string& name)
95 { this->name = name; }
96 const std::string& get_name() const
100 * tests if a given rectangle is inside the sector
101 * (a rectangle that is on top of the sector is considered inside)
103 bool inside(const Rect& rectangle) const;
105 void play_music(MusicType musictype);
106 MusicType get_music_type();
108 bool add_bullet(const Vector& pos, float xm, Direction dir);
109 bool add_smoke_cloud(const Vector& pos);
110 void add_floating_text(const Vector& pos, const std::string& text);
112 /** get currently activated sector. */
113 static Sector* current()
116 /** Get total number of badguys */
117 int get_total_badguys();
119 /** Get total number of GameObjects of given type */
120 template<class T> int get_total_count()
123 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
124 if (dynamic_cast<T*>(*i)) total++;
129 void collision_tilemap(const Rect& dest, const Vector& movement, CollisionHit& hit) const;
131 /** Checks if at the specified rectangle are gameobjects with STATIC flag set
132 * (or solid tiles from the tilemap).
133 * This does not(!) include badguys or players.
135 bool is_free_space(const Rect& rect) const;
138 * returns a list of players currently in the sector
140 std::vector<Player*> get_players() {
141 return std::vector<Player*>(1, this->player);
144 Rect get_active_region();
146 typedef std::vector<GameObject*> GameObjects;
147 typedef std::vector<MovingObject*> MovingObjects;
148 typedef std::vector<SpawnPoint*> SpawnPoints;
151 Level* level; /**< Parent level containing this sector */
152 uint32_t collision_tile_attributes(const Rect& dest) const;
154 void before_object_remove(GameObject* object);
155 bool before_object_add(GameObject* object);
157 void try_expose(GameObject* object);
158 void try_unexpose(GameObject* object);
160 /** Checks for all possible collisions. And calls the
161 collision_handlers, which the collision_objects provide for this
163 void handle_collisions();
166 * Does collision detection between 2 objects and does instant
167 * collision response handling in case of a collision
169 void collision_object(MovingObject* object1, MovingObject* object2) const;
172 * Does collision detection of an object against all other static
173 * objects (and the tilemap) in the level. Collision response is done
174 * for the first hit in time. (other hits get ignored, the function
175 * should be called repeatedly to resolve those)
177 * returns true if the collision detection should be aborted for this object
178 * (because of ABORT_MOVE in the collision response or no collisions)
180 bool collision_static(MovingObject* object, const Vector& movement);
183 GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
185 void fix_old_tiles();
187 static Sector* _current;
191 std::vector<Bullet*> bullets;
193 std::string init_script;
195 /// container for newly created objects, they'll be added in Sector::update
196 GameObjects gameobjects_new;
198 MusicType currentmusic;
200 std::auto_ptr<CollisionGrid> grid;
202 HSQOBJECT sector_table;
204 typedef std::vector<HSQOBJECT> ScriptList;
207 public: // TODO make this private again
208 /// show collision rectangles of moving objects (for debugging)
209 static bool show_collrects;
210 static bool draw_solids_only;
212 GameObjects gameobjects;
213 MovingObjects moving_objects;
214 SpawnPoints spawnpoints;
219 // some special objects, where we need direct access
220 // (try to avoid accessing them directly)