3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 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.
31 #include "player_status.hpp"
32 #include "object/gameobjs.hpp"
33 #include "object/camera.hpp"
34 #include "object/background.hpp"
35 #include "object/gradient.hpp"
36 #include "object/particlesystem.hpp"
37 #include "object/particlesystem_interactive.hpp"
38 #include "object/tilemap.hpp"
39 #include "lisp/parser.hpp"
40 #include "lisp/lisp.hpp"
41 #include "lisp/writer.hpp"
42 #include "lisp/list_iterator.hpp"
44 #include "audio/sound_manager.hpp"
45 #include "game_session.hpp"
46 #include "resources.hpp"
47 #include "statistics.hpp"
48 #include "collision_grid.hpp"
49 #include "collision_grid_iterator.hpp"
50 #include "object_factory.hpp"
51 #include "collision.hpp"
52 #include "spawn_point.hpp"
53 #include "math/rect.hpp"
54 #include "math/aatriangle.hpp"
55 #include "object/coin.hpp"
56 #include "object/block.hpp"
57 #include "object/invisible_block.hpp"
58 #include "object/bullet.hpp"
59 #include "object/text_object.hpp"
60 #include "badguy/jumpy.hpp"
61 #include "trigger/sequence_trigger.hpp"
62 #include "player_status.hpp"
63 #include "script_manager.hpp"
64 #include "scripting/wrapper_util.hpp"
65 #include "script_interface.hpp"
68 Sector* Sector::_current = 0;
71 : currentmusic(LEVEL_MUSIC), gravity(10),
72 player(0), solids(0), camera(0)
74 add_object(new Player(player_status));
75 add_object(new DisplayEffect());
76 add_object(new TextObject());
79 grid.reset(new CollisionGrid(32000, 32000));
82 script_manager.reset(new ScriptManager(ScriptManager::instance));
84 // create a new squirrel table for the sector
85 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
89 if(SQ_FAILED(sq_setdelegate(vm, -2)))
90 throw Scripting::SquirrelError(vm, "Couldn't set sector_table delegate");
92 sq_resetobject(§or_table);
93 if(SQ_FAILED(sq_getstackobj(vm, -1, §or_table)))
94 throw Scripting::SquirrelError(vm, "Couldn't get sector table");
95 sq_addref(vm, §or_table);
103 script_manager.reset(NULL);
104 sq_release(ScriptManager::instance->get_vm(), §or_table);
106 update_game_objects();
107 assert(gameobjects_new.size() == 0);
109 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
111 before_object_remove(*i);
115 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
121 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
123 if(name == "camera") {
124 Camera* camera = new Camera(this);
125 camera->parse(reader);
127 } else if(name == "particles-snow") {
128 SnowParticleSystem* partsys = new SnowParticleSystem();
129 partsys->parse(reader);
131 } else if(name == "particles-rain") {
132 RainParticleSystem* partsys = new RainParticleSystem();
133 partsys->parse(reader);
135 } else if(name == "particles-comets") {
136 CometParticleSystem* partsys = new CometParticleSystem();
137 partsys->parse(reader);
139 } else if(name == "particles-ghosts") {
140 GhostParticleSystem* partsys = new GhostParticleSystem();
141 partsys->parse(reader);
143 } else if(name == "particles-clouds") {
144 CloudParticleSystem* partsys = new CloudParticleSystem();
145 partsys->parse(reader);
147 } else if(name == "money") { // for compatibility with old maps
148 return new Jumpy(reader);
152 return create_object(name, reader);
153 } catch(std::exception& e) {
154 msg_warning << e.what() << "" << std::endl;
161 Sector::parse(const lisp::Lisp& sector)
163 lisp::ListIterator iter(§or);
165 const std::string& token = iter.item();
166 if(token == "name") {
167 iter.value()->get(name);
168 } else if(token == "gravity") {
169 iter.value()->get(gravity);
170 } else if(token == "music") {
171 iter.value()->get(music);
172 } else if(token == "spawnpoint") {
173 SpawnPoint* sp = new SpawnPoint(iter.lisp());
174 spawnpoints.push_back(sp);
175 } else if(token == "init-script") {
176 iter.value()->get(init_script);
178 GameObject* object = parse_object(token, *(iter.lisp()));
185 update_game_objects();
188 throw std::runtime_error("sector does not contain a solid tile layer.");
192 msg_warning << "sector '" << name << "' does not contain a camera." << std::endl;
193 update_game_objects();
194 add_object(new Camera(this));
197 update_game_objects();
201 Sector::parse_old_format(const lisp::Lisp& reader)
204 reader.get("gravity", gravity);
206 std::string backgroundimage;
207 reader.get("background", backgroundimage);
209 reader.get("bkgd_speed", bgspeed);
212 Color bkgd_top, bkgd_bottom;
213 int r = 0, g = 0, b = 128;
214 reader.get("bkgd_red_top", r);
215 reader.get("bkgd_green_top", g);
216 reader.get("bkgd_blue_top", b);
217 bkgd_top.red = static_cast<float> (r) / 255.0f;
218 bkgd_top.green = static_cast<float> (g) / 255.0f;
219 bkgd_top.blue = static_cast<float> (b) / 255.0f;
221 reader.get("bkgd_red_bottom", r);
222 reader.get("bkgd_green_bottom", g);
223 reader.get("bkgd_blue_bottom", b);
224 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
225 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
226 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
228 if(backgroundimage != "") {
229 Background* background = new Background();
230 background->set_image(
231 std::string("images/background/") + backgroundimage, bgspeed);
232 add_object(background);
234 Gradient* gradient = new Gradient();
235 gradient->set_gradient(bkgd_top, bkgd_bottom);
236 add_object(gradient);
239 std::string particlesystem;
240 reader.get("particle_system", particlesystem);
241 if(particlesystem == "clouds")
242 add_object(new CloudParticleSystem());
243 else if(particlesystem == "snow")
244 add_object(new SnowParticleSystem());
245 else if(particlesystem == "rain")
246 add_object(new RainParticleSystem());
248 Vector startpos(100, 170);
249 reader.get("start_pos_x", startpos.x);
250 reader.get("start_pos_y", startpos.y);
252 SpawnPoint* spawn = new SpawnPoint;
253 spawn->pos = startpos;
254 spawn->name = "main";
255 spawnpoints.push_back(spawn);
257 music = "chipdisko.ogg";
258 reader.get("music", music);
259 music = "music/" + music;
261 int width = 30, height = 15;
262 reader.get("width", width);
263 reader.get("height", height);
265 std::vector<unsigned int> tiles;
266 if(reader.get_vector("interactive-tm", tiles)
267 || reader.get_vector("tilemap", tiles)) {
268 TileMap* tilemap = new TileMap();
269 tilemap->set(width, height, tiles, LAYER_TILES, true);
273 if(reader.get_vector("background-tm", tiles)) {
274 TileMap* tilemap = new TileMap();
275 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
279 if(reader.get_vector("foreground-tm", tiles)) {
280 TileMap* tilemap = new TileMap();
281 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
285 // read reset-points (now spawn-points)
286 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
288 lisp::ListIterator iter(resetpoints);
290 if(iter.item() == "point") {
292 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
294 SpawnPoint* sp = new SpawnPoint;
297 spawnpoints.push_back(sp);
300 msg_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
306 const lisp::Lisp* objects = reader.get_lisp("objects");
308 lisp::ListIterator iter(objects);
310 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
314 msg_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
320 Camera* camera = new Camera(this);
323 update_game_objects();
326 throw std::runtime_error("sector does not contain a solid tile layer.");
329 update_game_objects();
333 Sector::fix_old_tiles()
336 for(size_t x=0; x < solids->get_width(); ++x) {
337 for(size_t y=0; y < solids->get_height(); ++y) {
338 const Tile* tile = solids->get_tile(x, y);
339 Vector pos(x*32, y*32);
341 if(tile->getID() == 112) {
342 add_object(new InvisibleBlock(pos));
343 solids->change(x, y, 0);
344 } else if(tile->getAttributes() & Tile::COIN) {
345 add_object(new Coin(pos));
346 solids->change(x, y, 0);
347 } else if(tile->getAttributes() & Tile::FULLBOX) {
348 add_object(new BonusBlock(pos, tile->getData()));
349 solids->change(x, y, 0);
350 } else if(tile->getAttributes() & Tile::BRICK) {
351 add_object(new Brick(pos, tile->getData()));
352 solids->change(x, y, 0);
353 } else if(tile->getAttributes() & Tile::GOAL) {
354 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
355 add_object(new SequenceTrigger(pos, sequence));
356 solids->change(x, y, 0);
363 Sector::write(lisp::Writer& writer)
365 writer.write_string("name", name);
366 writer.write_float("gravity", gravity);
367 writer.write_string("music", music);
370 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
372 SpawnPoint* spawn = *i;
373 writer.start_list("spawn-points");
374 writer.write_string("name", spawn->name);
375 writer.write_float("x", spawn->pos.x);
376 writer.write_float("y", spawn->pos.y);
377 writer.end_list("spawn-points");
381 for(GameObjects::iterator i = gameobjects.begin();
382 i != gameobjects.end(); ++i) {
383 Serializable* serializable = dynamic_cast<Serializable*> (*i);
385 serializable->write(writer);
390 Sector::run_script(std::istream& in, const std::string& sourcename)
392 // create new thread and keep a weakref
393 HSQUIRRELVM vm = script_manager->create_thread();
395 // set sector_table as roottable for the thread
396 sq_pushobject(vm, sector_table);
399 Scripting::compile_and_run(vm, in, sourcename);
405 Sector::add_object(GameObject* object)
407 // make sure the object isn't already in the list
409 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
412 assert("object already added to sector" == 0);
415 for(GameObjects::iterator i = gameobjects_new.begin();
416 i != gameobjects_new.end(); ++i) {
418 assert("object already added to sector" == 0);
423 gameobjects_new.push_back(object);
427 Sector::activate(const std::string& spawnpoint)
430 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
432 if((*i)->name == spawnpoint) {
438 msg_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
439 if(spawnpoint != "main") {
442 activate(Vector(0, 0));
450 Sector::activate(const Vector& player_pos)
452 if(_current != this) {
454 _current->deactivate();
457 // register sectortable as current_sector in scripting
458 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
459 sq_pushroottable(vm);
460 sq_pushstring(vm, "sector", -1);
461 sq_pushobject(vm, sector_table);
462 if(SQ_FAILED(sq_createslot(vm, -3)))
463 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
466 for(GameObjects::iterator i = gameobjects.begin();
467 i != gameobjects.end(); ++i) {
468 GameObject* object = *i;
474 player->move(player_pos);
475 camera->reset(player->get_pos());
478 if(init_script != "") {
479 std::istringstream in(init_script);
480 run_script(in, std::string("Sector(") + name + ") - init");
490 // remove sector entry from global vm
491 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
492 sq_pushroottable(vm);
493 sq_pushstring(vm, "sector", -1);
494 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
495 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
498 for(GameObjects::iterator i = gameobjects.begin();
499 i != gameobjects.end(); ++i) {
500 GameObject* object = *i;
502 try_unexpose(object);
509 Sector::get_active_region()
512 camera->get_translation() - Vector(1600, 1200),
513 camera->get_translation() + Vector(1600, 1200));
517 Sector::update(float elapsed_time)
519 script_manager->update();
521 player->check_bounds(camera);
524 CollisionGridIterator iter(*grid, get_active_region());
525 while(MovingObject* object = iter.next()) {
526 if(!object->is_valid())
529 object->update(elapsed_time);
533 for(GameObjects::iterator i = gameobjects.begin();
534 i != gameobjects.end(); ++i) {
535 GameObject* object = *i;
536 if(!object->is_valid())
539 object->update(elapsed_time);
543 /* Handle all possible collisions. */
545 update_game_objects();
549 Sector::update_game_objects()
551 /** cleanup marked objects */
552 for(std::vector<Bullet*>::iterator i = bullets.begin();
553 i != bullets.end(); /* nothing */) {
555 if(bullet->is_valid()) {
560 i = bullets.erase(i);
562 for(MovingObjects::iterator i = moving_objects.begin();
563 i != moving_objects.end(); /* nothing */) {
564 MovingObject* moving_object = *i;
565 if(moving_object->is_valid()) {
571 grid->remove_object(moving_object);
574 i = moving_objects.erase(i);
576 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
577 i != gameobjects.end(); /* nothing */) {
578 GameObject* object = *i;
580 if(object->is_valid()) {
585 before_object_remove(object);
588 i = gameobjects.erase(i);
591 /* add newly created objects */
592 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
593 i != gameobjects_new.end(); ++i)
595 GameObject* object = *i;
597 before_object_add(object);
599 gameobjects.push_back(object);
601 gameobjects_new.clear();
605 Sector::before_object_add(GameObject* object)
607 Bullet* bullet = dynamic_cast<Bullet*> (object);
609 bullets.push_back(bullet);
611 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
613 moving_objects.push_back(movingobject);
615 grid->add_object(movingobject);
619 TileMap* tilemap = dynamic_cast<TileMap*> (object);
620 if(tilemap && tilemap->is_solid()) {
624 msg_warning << "Another solid tilemaps added. Ignoring" << std::endl;
628 Camera* camera = dynamic_cast<Camera*> (object);
630 if(this->camera != 0) {
631 msg_warning << "Multiple cameras added. Ignoring" << std::endl;
634 this->camera = camera;
637 Player* player = dynamic_cast<Player*> (object);
639 if(this->player != 0) {
640 msg_warning << "Multiple players added. Ignoring" << std::endl;
643 this->player = player;
646 if(_current == this) {
654 Sector::try_expose(GameObject* object)
656 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
657 if(interface != NULL) {
658 HSQUIRRELVM vm = script_manager->get_vm();
659 sq_pushobject(vm, sector_table);
660 interface->expose(vm, -1);
666 Sector::before_object_remove(GameObject* object)
669 try_unexpose(object);
673 Sector::try_unexpose(GameObject* object)
675 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
676 if(interface != NULL) {
677 HSQUIRRELVM vm = script_manager->get_vm();
678 sq_pushobject(vm, sector_table);
679 interface->unexpose(vm, -1);
685 Sector::draw(DrawingContext& context)
687 context.push_transform();
688 context.set_translation(camera->get_translation());
690 for(GameObjects::iterator i = gameobjects.begin();
691 i != gameobjects.end(); ++i) {
692 GameObject* object = *i;
693 if(!object->is_valid())
696 object->draw(context);
699 context.pop_transform();
702 static const float DELTA = .001;
705 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
706 CollisionHit& hit) const
708 // calculate rectangle where the object will move
709 float x1 = dest.get_left();
710 float x2 = dest.get_right();
711 float y1 = dest.get_top();
712 float y2 = dest.get_bottom();
714 // test with all tiles in this rectangle
715 int starttilex = int(x1) / 32;
716 int starttiley = int(y1) / 32;
717 int max_x = int(x2 + (1 - DELTA));
718 int max_y = int(y2 + (1 - DELTA));
720 CollisionHit temphit;
721 for(int x = starttilex; x*32 < max_x; ++x) {
722 for(int y = starttiley; y*32 < max_y; ++y) {
723 const Tile* tile = solids->get_tile(x, y);
726 // skip non-solid tiles
727 if(tile->getAttributes() == 0)
729 // only handle unisolid when the player is falling down and when he was
730 // above the tile before
731 if(tile->getAttributes() & Tile::UNISOLID) {
732 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
736 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
738 Vector p1(x*32, y*32);
739 Vector p2((x+1)*32, (y+1)*32);
740 triangle = AATriangle(p1, p2, tile->getData());
742 if(Collision::rectangle_aatriangle(temphit, dest, movement,
744 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
748 } else { // normal rectangular tile
749 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
750 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
751 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
761 Sector::collision_tile_attributes(const Rect& dest) const
763 /** XXX This function doesn't work correctly as it will check all tiles
764 * in the bounding box of the object movement, this might include tiles
765 * that have actually never been touched by the object
766 * (though this only occures for very fast objects...)
770 // calculate rectangle where the object will move
772 if(object->get_movement().x >= 0) {
773 x1 = object->get_bbox().p1.x;
774 x2 = object->get_bbox().p2.x + object->get_movement().x;
776 x1 = object->get_bbox().p1.x + object->get_movement().x;
777 x2 = object->get_bbox().p2.x;
780 if(object->get_movement().y >= 0) {
781 y1 = object->get_bbox().p1.y;
782 y2 = object->get_bbox().p2.y + object->get_movement().y;
784 y1 = object->get_bbox().p1.y + object->get_movement().y;
785 y2 = object->get_bbox().p2.y;
788 float x1 = dest.p1.x;
789 float y1 = dest.p1.y;
790 float x2 = dest.p2.x;
791 float y2 = dest.p2.y;
793 // test with all tiles in this rectangle
794 int starttilex = int(x1) / 32;
795 int starttiley = int(y1) / 32;
800 for(int x = starttilex; x*32 < max_x; ++x) {
801 for(int y = starttiley; y*32 < max_y; ++y) {
802 const Tile* tile = solids->get_tile(x, y);
805 result |= tile->getAttributes();
813 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
817 Vector movement = object1->get_movement() - object2->get_movement();
818 if(Collision::rectangle_rectangle(hit, object1->dest, movement, object2->dest)) {
819 HitResponse response1 = object1->collision(*object2, hit);
821 HitResponse response2 = object2->collision(*object1, hit);
823 if(response1 != CONTINUE) {
824 if(response1 == ABORT_MOVE)
825 object1->dest = object1->get_bbox();
826 if(response2 == CONTINUE)
827 object2->dest.move(hit.normal * (hit.depth + DELTA));
828 } else if(response2 != CONTINUE) {
829 if(response2 == ABORT_MOVE)
830 object2->dest = object2->get_bbox();
831 if(response1 == CONTINUE)
832 object1->dest.move(-hit.normal * (hit.depth + DELTA));
834 object1->dest.move(-hit.normal * (hit.depth/2 + DELTA));
835 object2->dest.move(hit.normal * (hit.depth/2 + DELTA));
841 Sector::collision_static(MovingObject* object, const Vector& movement)
843 GameObject* collided_with = solids;
847 collision_tilemap(object->dest, movement, hit);
849 // collision with other (static) objects
850 CollisionHit temphit;
851 for(MovingObjects::iterator i2 = moving_objects.begin();
852 i2 != moving_objects.end(); ++i2) {
853 MovingObject* moving_object_2 = *i2;
854 if(moving_object_2->get_group() != COLGROUP_STATIC
855 || !moving_object_2->is_valid())
858 Rect dest = moving_object_2->dest;
861 = movement - moving_object_2->get_movement();
863 if(Collision::rectangle_rectangle(temphit, object->dest, rel_movement, dest)
864 && temphit.time > hit.time) {
866 collided_with = moving_object_2;
873 HitResponse response = object->collision(*collided_with, hit);
875 if(collided_with != solids) {
876 MovingObject* moving_object = (MovingObject*) collided_with;
877 HitResponse other_response = moving_object->collision(*object, hit);
878 if(other_response == ABORT_MOVE) {
879 moving_object->dest = moving_object->get_bbox();
880 } else if(other_response == FORCE_MOVE) {
881 // the static object "wins" move tux out of the collision
882 object->dest.move(-hit.normal * (hit.depth + DELTA));
884 } else if(other_response == PASS_MOVEMENT) {
885 object->dest.move(moving_object->get_movement());
886 //object->movement += moving_object->get_movement();
890 if(response == CONTINUE) {
891 object->dest.move(-hit.normal * (hit.depth + DELTA));
893 } else if(response == ABORT_MOVE) {
894 object->dest = object->get_bbox();
903 Sector::handle_collisions()
905 // calculate destination positions of the objects
906 for(MovingObjects::iterator i = moving_objects.begin();
907 i != moving_objects.end(); ++i) {
908 MovingObject* moving_object = *i;
910 moving_object->dest = moving_object->get_bbox();
911 moving_object->dest.move(moving_object->get_movement());
914 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
915 // we do this up to 4 times and have to sort all results for the smallest
916 // one before we can continue here
917 for(MovingObjects::iterator i = moving_objects.begin();
918 i != moving_objects.end(); ++i) {
919 MovingObject* moving_object = *i;
920 if((moving_object->get_group() != COLGROUP_MOVING
921 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
922 || !moving_object->is_valid())
925 Vector movement = moving_object->get_movement();
927 // test if x or y movement is dominant
928 if(fabsf(moving_object->get_movement().x) < fabsf(moving_object->get_movement().y)) {
930 // test in x direction first, then y direction
931 moving_object->dest.move(Vector(0, -movement.y));
932 for(int i = 0; i < 2; ++i) {
933 bool res = collision_static(moving_object, Vector(movement.x, 0));
937 moving_object->dest.move(Vector(0, movement.y));
938 for(int i = 0; i < 2; ++i) {
939 bool res = collision_static(moving_object, Vector(0, movement.y));
946 // test in y direction first, then x direction
947 moving_object->dest.move(Vector(-movement.x, 0));
948 for(int i = 0; i < 2; ++i) {
949 bool res = collision_static(moving_object, Vector(0, movement.y));
953 moving_object->dest.move(Vector(movement.x, 0));
954 for(int i = 0; i < 2; ++i) {
955 bool res = collision_static(moving_object, Vector(movement.x, 0));
962 // part2: COLGROUP_MOVING vs tile attributes
963 for(MovingObjects::iterator i = moving_objects.begin();
964 i != moving_objects.end(); ++i) {
965 MovingObject* moving_object = *i;
966 if((moving_object->get_group() != COLGROUP_MOVING
967 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
968 || !moving_object->is_valid())
971 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
972 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
973 moving_object->collision_tile(tile_attributes);
977 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
978 for(MovingObjects::iterator i = moving_objects.begin();
979 i != moving_objects.end(); ++i) {
980 MovingObject* moving_object = *i;
981 if(moving_object->get_group() != COLGROUP_MOVING
982 || !moving_object->is_valid())
985 for(MovingObjects::iterator i2 = moving_objects.begin();
986 i2 != moving_objects.end(); ++i2) {
987 MovingObject* moving_object_2 = *i2;
988 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
989 || !moving_object_2->is_valid())
992 collision_object(moving_object, moving_object_2);
996 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
997 for(MovingObjects::iterator i = moving_objects.begin();
998 i != moving_objects.end(); ++i) {
999 MovingObject* moving_object = *i;
1001 if(moving_object->get_group() != COLGROUP_MOVING
1002 || !moving_object->is_valid())
1005 for(MovingObjects::iterator i2 = i+1;
1006 i2 != moving_objects.end(); ++i2) {
1007 MovingObject* moving_object_2 = *i2;
1008 if(moving_object_2->get_group() != COLGROUP_MOVING
1009 || !moving_object_2->is_valid())
1012 collision_object(moving_object, moving_object_2);
1016 // apply object movement
1017 for(MovingObjects::iterator i = moving_objects.begin();
1018 i != moving_objects.end(); ++i) {
1019 MovingObject* moving_object = *i;
1021 moving_object->bbox = moving_object->dest;
1022 moving_object->movement = Vector(0, 0);
1027 Sector::is_free_space(const Rect& rect) const
1029 // test with all tiles in this rectangle
1030 int starttilex = int(rect.p1.x) / 32;
1031 int starttiley = int(rect.p1.y) / 32;
1032 int max_x = int(rect.p2.x);
1033 int max_y = int(rect.p2.y);
1035 for(int x = starttilex; x*32 < max_x; ++x) {
1036 for(int y = starttiley; y*32 < max_y; ++y) {
1037 const Tile* tile = solids->get_tile(x, y);
1040 if(tile->getAttributes() & Tile::SOLID)
1045 for(MovingObjects::const_iterator i = moving_objects.begin();
1046 i != moving_objects.end(); ++i) {
1047 const MovingObject* moving_object = *i;
1048 if(moving_object->get_group() != COLGROUP_STATIC
1049 || !moving_object->is_valid())
1052 if(Collision::intersects(rect, moving_object->get_bbox()))
1060 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1062 // TODO remove this function and move these checks elsewhere...
1063 static const size_t MAX_FIRE_BULLETS = 2;
1064 static const size_t MAX_ICE_BULLETS = 1;
1066 Bullet* new_bullet = 0;
1067 if(player_status->bonus == FIRE_BONUS) {
1068 if(bullets.size() > MAX_FIRE_BULLETS-1)
1070 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
1071 } else if(player_status->bonus == ICE_BONUS) {
1072 if(bullets.size() > MAX_ICE_BULLETS-1)
1074 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
1078 add_object(new_bullet);
1080 sound_manager->play("sounds/shoot.wav");
1086 Sector::add_smoke_cloud(const Vector& pos)
1088 add_object(new SmokeCloud(pos));
1093 Sector::add_floating_text(const Vector& pos, const std::string& text)
1095 add_object(new FloatingText(pos, text));
1099 Sector::play_music(MusicType type)
1101 currentmusic = type;
1102 switch(currentmusic) {
1104 sound_manager->play_music(music);
1107 sound_manager->play_music("music/salcon.ogg");
1110 sound_manager->play_music("");
1116 Sector::get_music_type()
1118 return currentmusic;
1122 Sector::get_total_badguys()
1124 int total_badguys = 0;
1125 for(GameObjects::iterator i = gameobjects.begin();
1126 i != gameobjects.end(); ++i) {
1127 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1128 if (badguy && badguy->countMe)
1132 return total_badguys;
1136 Sector::inside(const Rect& rect) const
1138 if(rect.p1.x > solids->get_width() * 32
1139 || rect.p1.y > solids->get_height() * 32
1140 || rect.p2.x < 0 || rect.p2.y < 0)