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.
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 "scripting/squirrel_util.hpp"
64 #include "script_interface.hpp"
67 Sector* Sector::_current = 0;
69 bool Sector::show_collrects = false;
70 bool Sector::draw_solids_only = false;
72 Sector::Sector(Level* parent)
73 : level(parent), currentmusic(LEVEL_MUSIC), gravity(10),
74 player(0), solids(0), camera(0)
76 add_object(new Player(player_status));
77 add_object(new DisplayEffect());
78 add_object(new TextObject());
81 grid.reset(new CollisionGrid(32000, 32000));
84 // create a new squirrel table for the sector
85 using namespace Scripting;
87 sq_newtable(global_vm);
88 sq_pushroottable(global_vm);
89 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
90 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
92 sq_resetobject(§or_table);
93 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
94 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
95 sq_addref(global_vm, §or_table);
101 using namespace Scripting;
105 for(ScriptList::iterator i = scripts.begin();
106 i != scripts.end(); ++i) {
107 HSQOBJECT& object = *i;
108 sq_release(global_vm, &object);
110 sq_release(global_vm, §or_table);
112 update_game_objects();
113 assert(gameobjects_new.size() == 0);
115 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
117 before_object_remove(*i);
121 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
133 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
135 if(name == "camera") {
136 Camera* camera = new Camera(this);
137 camera->parse(reader);
139 } else if(name == "particles-snow") {
140 SnowParticleSystem* partsys = new SnowParticleSystem();
141 partsys->parse(reader);
143 } else if(name == "particles-rain") {
144 RainParticleSystem* partsys = new RainParticleSystem();
145 partsys->parse(reader);
147 } else if(name == "particles-comets") {
148 CometParticleSystem* partsys = new CometParticleSystem();
149 partsys->parse(reader);
151 } else if(name == "particles-ghosts") {
152 GhostParticleSystem* partsys = new GhostParticleSystem();
153 partsys->parse(reader);
155 } else if(name == "particles-clouds") {
156 CloudParticleSystem* partsys = new CloudParticleSystem();
157 partsys->parse(reader);
159 } else if(name == "money") { // for compatibility with old maps
160 return new Jumpy(reader);
164 return create_object(name, reader);
165 } catch(std::exception& e) {
166 log_warning << e.what() << "" << std::endl;
173 Sector::parse(const lisp::Lisp& sector)
175 lisp::ListIterator iter(§or);
177 const std::string& token = iter.item();
178 if(token == "name") {
179 iter.value()->get(name);
180 } else if(token == "gravity") {
181 iter.value()->get(gravity);
182 } else if(token == "music") {
183 iter.value()->get(music);
184 } else if(token == "spawnpoint") {
185 SpawnPoint* sp = new SpawnPoint(iter.lisp());
186 spawnpoints.push_back(sp);
187 } else if(token == "init-script") {
188 iter.value()->get(init_script);
190 GameObject* object = parse_object(token, *(iter.lisp()));
197 update_game_objects();
200 throw std::runtime_error("sector does not contain a solid tile layer.");
204 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
205 update_game_objects();
206 add_object(new Camera(this));
209 update_game_objects();
213 Sector::parse_old_format(const lisp::Lisp& reader)
216 reader.get("gravity", gravity);
218 std::string backgroundimage;
219 reader.get("background", backgroundimage);
221 reader.get("bkgd_speed", bgspeed);
224 Color bkgd_top, bkgd_bottom;
225 int r = 0, g = 0, b = 128;
226 reader.get("bkgd_red_top", r);
227 reader.get("bkgd_green_top", g);
228 reader.get("bkgd_blue_top", b);
229 bkgd_top.red = static_cast<float> (r) / 255.0f;
230 bkgd_top.green = static_cast<float> (g) / 255.0f;
231 bkgd_top.blue = static_cast<float> (b) / 255.0f;
233 reader.get("bkgd_red_bottom", r);
234 reader.get("bkgd_green_bottom", g);
235 reader.get("bkgd_blue_bottom", b);
236 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
237 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
238 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
240 if(backgroundimage != "") {
241 Background* background = new Background();
242 background->set_image(
243 std::string("images/background/") + backgroundimage, bgspeed);
244 add_object(background);
246 Gradient* gradient = new Gradient();
247 gradient->set_gradient(bkgd_top, bkgd_bottom);
248 add_object(gradient);
251 std::string particlesystem;
252 reader.get("particle_system", particlesystem);
253 if(particlesystem == "clouds")
254 add_object(new CloudParticleSystem());
255 else if(particlesystem == "snow")
256 add_object(new SnowParticleSystem());
257 else if(particlesystem == "rain")
258 add_object(new RainParticleSystem());
260 Vector startpos(100, 170);
261 reader.get("start_pos_x", startpos.x);
262 reader.get("start_pos_y", startpos.y);
264 SpawnPoint* spawn = new SpawnPoint;
265 spawn->pos = startpos;
266 spawn->name = "main";
267 spawnpoints.push_back(spawn);
269 music = "chipdisko.ogg";
270 reader.get("music", music);
271 music = "music/" + music;
273 int width = 30, height = 15;
274 reader.get("width", width);
275 reader.get("height", height);
277 std::vector<unsigned int> tiles;
278 if(reader.get_vector("interactive-tm", tiles)
279 || reader.get_vector("tilemap", tiles)) {
280 TileMap* tilemap = new TileMap();
281 tilemap->set(width, height, tiles, LAYER_TILES, true);
285 if(reader.get_vector("background-tm", tiles)) {
286 TileMap* tilemap = new TileMap();
287 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
291 if(reader.get_vector("foreground-tm", tiles)) {
292 TileMap* tilemap = new TileMap();
293 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
297 // read reset-points (now spawn-points)
298 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
300 lisp::ListIterator iter(resetpoints);
302 if(iter.item() == "point") {
304 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
306 SpawnPoint* sp = new SpawnPoint;
309 spawnpoints.push_back(sp);
312 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
318 const lisp::Lisp* objects = reader.get_lisp("objects");
320 lisp::ListIterator iter(objects);
322 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
326 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
332 Camera* camera = new Camera(this);
335 update_game_objects();
338 throw std::runtime_error("sector does not contain a solid tile layer.");
341 update_game_objects();
345 Sector::fix_old_tiles()
348 for(size_t x=0; x < solids->get_width(); ++x) {
349 for(size_t y=0; y < solids->get_height(); ++y) {
350 const Tile* tile = solids->get_tile(x, y);
351 Vector pos(x*32, y*32);
353 if(tile->getID() == 112) {
354 add_object(new InvisibleBlock(pos));
355 solids->change(x, y, 0);
356 } else if(tile->getAttributes() & Tile::COIN) {
357 add_object(new Coin(pos));
358 solids->change(x, y, 0);
359 } else if(tile->getAttributes() & Tile::FULLBOX) {
360 add_object(new BonusBlock(pos, tile->getData()));
361 solids->change(x, y, 0);
362 } else if(tile->getAttributes() & Tile::BRICK) {
363 add_object(new Brick(pos, tile->getData()));
364 solids->change(x, y, 0);
365 } else if(tile->getAttributes() & Tile::GOAL) {
366 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
367 add_object(new SequenceTrigger(pos, sequence));
368 solids->change(x, y, 0);
375 Sector::write(lisp::Writer& writer)
377 writer.write_string("name", name);
378 writer.write_float("gravity", gravity);
379 writer.write_string("music", music);
382 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
384 SpawnPoint* spawn = *i;
385 writer.start_list("spawn-points");
386 writer.write_string("name", spawn->name);
387 writer.write_float("x", spawn->pos.x);
388 writer.write_float("y", spawn->pos.y);
389 writer.end_list("spawn-points");
393 for(GameObjects::iterator i = gameobjects.begin();
394 i != gameobjects.end(); ++i) {
395 Serializable* serializable = dynamic_cast<Serializable*> (*i);
397 serializable->write(writer);
402 Sector::run_script(std::istream& in, const std::string& sourcename)
404 using namespace Scripting;
406 // garbage collect thread list
407 for(ScriptList::iterator i = scripts.begin();
408 i != scripts.end(); ) {
409 HSQOBJECT& object = *i;
410 HSQUIRRELVM vm = object_to_vm(object);
412 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
413 sq_release(global_vm, &object);
414 i = scripts.erase(i);
421 HSQOBJECT object = create_thread(global_vm);
422 scripts.push_back(object);
424 HSQUIRRELVM vm = object_to_vm(object);
426 // set sector_table as roottable for the thread
427 sq_pushobject(vm, sector_table);
430 compile_and_run(vm, in, sourcename);
436 Sector::add_object(GameObject* object)
438 // make sure the object isn't already in the list
440 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
443 assert("object already added to sector" == 0);
446 for(GameObjects::iterator i = gameobjects_new.begin();
447 i != gameobjects_new.end(); ++i) {
449 assert("object already added to sector" == 0);
454 gameobjects_new.push_back(object);
458 Sector::activate(const std::string& spawnpoint)
461 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
463 if((*i)->name == spawnpoint) {
469 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
470 if(spawnpoint != "main") {
473 activate(Vector(0, 0));
481 Sector::activate(const Vector& player_pos)
483 if(_current != this) {
485 _current->deactivate();
488 // register sectortable as current_sector in scripting
489 HSQUIRRELVM vm = Scripting::global_vm;
490 sq_pushroottable(vm);
491 sq_pushstring(vm, "sector", -1);
492 sq_pushobject(vm, sector_table);
493 if(SQ_FAILED(sq_createslot(vm, -3)))
494 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
497 for(GameObjects::iterator i = gameobjects.begin();
498 i != gameobjects.end(); ++i) {
499 GameObject* object = *i;
505 player->move(player_pos);
506 camera->reset(player->get_pos());
507 update_game_objects();
510 if(init_script != "") {
511 std::istringstream in(init_script);
512 run_script(in, std::string("Sector(") + name + ") - init");
522 // remove sector entry from global vm
523 HSQUIRRELVM vm = Scripting::global_vm;
524 sq_pushroottable(vm);
525 sq_pushstring(vm, "sector", -1);
526 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
527 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
530 for(GameObjects::iterator i = gameobjects.begin();
531 i != gameobjects.end(); ++i) {
532 GameObject* object = *i;
534 try_unexpose(object);
541 Sector::get_active_region()
544 camera->get_translation() - Vector(1600, 1200),
545 camera->get_translation() + Vector(1600, 1200));
549 Sector::update(float elapsed_time)
551 player->check_bounds(camera);
554 CollisionGridIterator iter(*grid, get_active_region());
555 while(MovingObject* object = iter.next()) {
556 if(!object->is_valid())
559 object->update(elapsed_time);
563 for(GameObjects::iterator i = gameobjects.begin();
564 i != gameobjects.end(); ++i) {
565 GameObject* object = *i;
566 if(!object->is_valid())
569 object->update(elapsed_time);
573 /* Handle all possible collisions. */
575 update_game_objects();
579 Sector::update_game_objects()
581 /** cleanup marked objects */
582 for(std::vector<Bullet*>::iterator i = bullets.begin();
583 i != bullets.end(); /* nothing */) {
585 if(bullet->is_valid()) {
590 i = bullets.erase(i);
592 for(MovingObjects::iterator i = moving_objects.begin();
593 i != moving_objects.end(); /* nothing */) {
594 MovingObject* moving_object = *i;
595 if(moving_object->is_valid()) {
601 grid->remove_object(moving_object);
604 i = moving_objects.erase(i);
606 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
607 i != gameobjects.end(); /* nothing */) {
608 GameObject* object = *i;
610 if(object->is_valid()) {
615 before_object_remove(object);
618 i = gameobjects.erase(i);
621 /* add newly created objects */
622 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
623 i != gameobjects_new.end(); ++i)
625 GameObject* object = *i;
627 before_object_add(object);
629 gameobjects.push_back(object);
631 gameobjects_new.clear();
635 Sector::before_object_add(GameObject* object)
637 Bullet* bullet = dynamic_cast<Bullet*> (object);
639 bullets.push_back(bullet);
641 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
643 moving_objects.push_back(movingobject);
645 grid->add_object(movingobject);
649 TileMap* tilemap = dynamic_cast<TileMap*> (object);
650 if(tilemap && tilemap->is_solid()) {
654 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
658 Camera* camera = dynamic_cast<Camera*> (object);
660 if(this->camera != 0) {
661 log_warning << "Multiple cameras added. Ignoring" << std::endl;
664 this->camera = camera;
667 Player* player = dynamic_cast<Player*> (object);
669 if(this->player != 0) {
670 log_warning << "Multiple players added. Ignoring" << std::endl;
673 this->player = player;
676 if(_current == this) {
684 Sector::try_expose(GameObject* object)
686 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
687 if(interface != NULL) {
688 HSQUIRRELVM vm = Scripting::global_vm;
689 sq_pushobject(vm, sector_table);
690 interface->expose(vm, -1);
696 Sector::before_object_remove(GameObject* object)
699 try_unexpose(object);
703 Sector::try_unexpose(GameObject* object)
705 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
706 if(interface != NULL) {
707 HSQUIRRELVM vm = Scripting::global_vm;
708 int oldtop = sq_gettop(vm);
709 sq_pushobject(vm, sector_table);
711 interface->unexpose(vm, -1);
712 } catch(std::exception& e) {
713 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
715 sq_settop(vm, oldtop);
720 Sector::draw(DrawingContext& context)
722 context.push_transform();
723 context.set_translation(camera->get_translation());
725 for(GameObjects::iterator i = gameobjects.begin();
726 i != gameobjects.end(); ++i) {
727 GameObject* object = *i;
728 if(!object->is_valid())
731 if (draw_solids_only)
733 TileMap* tm = dynamic_cast<TileMap*>(object);
734 if (tm && !tm->is_solid())
738 object->draw(context);
742 Color col(0.2, 0.2, 0.2, 0.7);
743 for(MovingObjects::iterator i = moving_objects.begin();
744 i != moving_objects.end(); ++i) {
745 MovingObject* object = *i;
746 const Rect& rect = object->get_bbox();
748 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
752 context.pop_transform();
755 static const float DELTA = .001;
758 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
759 CollisionHit& hit) const
761 // calculate rectangle where the object will move
762 float x1 = dest.get_left();
763 float x2 = dest.get_right();
764 float y1 = dest.get_top();
765 float y2 = dest.get_bottom();
767 // test with all tiles in this rectangle
768 int starttilex = int(x1) / 32;
769 int starttiley = int(y1) / 32;
770 int max_x = int(x2 + (1 - DELTA));
771 int max_y = int(y2 + (1 - DELTA));
773 CollisionHit temphit;
774 for(int x = starttilex; x*32 < max_x; ++x) {
775 for(int y = starttiley; y*32 < max_y; ++y) {
776 const Tile* tile = solids->get_tile(x, y);
779 // skip non-solid tiles
780 if(tile->getAttributes() == 0)
782 // only handle unisolid when the player is falling down and when he was
783 // above the tile before
784 if(tile->getAttributes() & Tile::UNISOLID) {
785 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
789 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
791 Vector p1(x*32, y*32);
792 Vector p2((x+1)*32, (y+1)*32);
793 triangle = AATriangle(p1, p2, tile->getData());
795 if(Collision::rectangle_aatriangle(temphit, dest, movement,
797 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
801 } else { // normal rectangular tile
802 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
803 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
804 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
814 Sector::collision_tile_attributes(const Rect& dest) const
816 /** XXX This function doesn't work correctly as it will check all tiles
817 * in the bounding box of the object movement, this might include tiles
818 * that have actually never been touched by the object
819 * (though this only occures for very fast objects...)
823 // calculate rectangle where the object will move
825 if(object->get_movement().x >= 0) {
826 x1 = object->get_bbox().p1.x;
827 x2 = object->get_bbox().p2.x + object->get_movement().x;
829 x1 = object->get_bbox().p1.x + object->get_movement().x;
830 x2 = object->get_bbox().p2.x;
833 if(object->get_movement().y >= 0) {
834 y1 = object->get_bbox().p1.y;
835 y2 = object->get_bbox().p2.y + object->get_movement().y;
837 y1 = object->get_bbox().p1.y + object->get_movement().y;
838 y2 = object->get_bbox().p2.y;
841 float x1 = dest.p1.x;
842 float y1 = dest.p1.y;
843 float x2 = dest.p2.x;
844 float y2 = dest.p2.y;
846 // test with all tiles in this rectangle
847 int starttilex = int(x1) / 32;
848 int starttiley = int(y1) / 32;
853 for(int x = starttilex; x*32 < max_x; ++x) {
854 for(int y = starttiley; y*32 < max_y; ++y) {
855 const Tile* tile = solids->get_tile(x, y);
858 result |= tile->getAttributes();
866 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
870 Vector movement = object1->get_movement() - object2->get_movement();
871 if(Collision::rectangle_rectangle(hit, object1->dest, movement, object2->dest)) {
872 HitResponse response1 = object1->collision(*object2, hit);
874 HitResponse response2 = object2->collision(*object1, hit);
876 if(response1 != CONTINUE) {
877 if(response1 == ABORT_MOVE)
878 object1->dest = object1->get_bbox();
879 if(response2 == CONTINUE)
880 object2->dest.move(hit.normal * (hit.depth + DELTA));
881 } else if(response2 != CONTINUE) {
882 if(response2 == ABORT_MOVE)
883 object2->dest = object2->get_bbox();
884 if(response1 == CONTINUE)
885 object1->dest.move(-hit.normal * (hit.depth + DELTA));
887 object1->dest.move(-hit.normal * (hit.depth/2 + DELTA));
888 object2->dest.move(hit.normal * (hit.depth/2 + DELTA));
894 Sector::collision_static(MovingObject* object, const Vector& movement)
896 GameObject* collided_with = solids;
900 collision_tilemap(object->dest, movement, hit);
902 // collision with other (static) objects
903 CollisionHit temphit;
904 for(MovingObjects::iterator i2 = moving_objects.begin();
905 i2 != moving_objects.end(); ++i2) {
906 MovingObject* moving_object_2 = *i2;
907 if(moving_object_2->get_group() != COLGROUP_STATIC
908 || !moving_object_2->is_valid())
911 Rect dest = moving_object_2->dest;
914 = movement - moving_object_2->get_movement();
916 if(Collision::rectangle_rectangle(temphit, object->dest, rel_movement, dest)
917 && temphit.time > hit.time) {
919 collided_with = moving_object_2;
926 HitResponse response = object->collision(*collided_with, hit);
928 if(collided_with != solids) {
929 MovingObject* moving_object = (MovingObject*) collided_with;
930 HitResponse other_response = moving_object->collision(*object, hit);
931 if(other_response == ABORT_MOVE) {
932 moving_object->dest = moving_object->get_bbox();
933 } else if(other_response == FORCE_MOVE) {
934 // the static object "wins" move tux out of the collision
935 object->dest.move(-hit.normal * (hit.depth + DELTA));
937 } else if(other_response == PASS_MOVEMENT) {
938 object->dest.move(moving_object->get_movement());
939 //object->movement += moving_object->get_movement();
943 if(response == CONTINUE) {
944 object->dest.move(-hit.normal * (hit.depth + DELTA));
946 } else if(response == ABORT_MOVE) {
947 object->dest = object->get_bbox();
956 Sector::handle_collisions()
958 // calculate destination positions of the objects
959 for(MovingObjects::iterator i = moving_objects.begin();
960 i != moving_objects.end(); ++i) {
961 MovingObject* moving_object = *i;
963 moving_object->dest = moving_object->get_bbox();
964 moving_object->dest.move(moving_object->get_movement());
967 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
968 // we do this up to 4 times and have to sort all results for the smallest
969 // one before we can continue here
970 for(MovingObjects::iterator i = moving_objects.begin();
971 i != moving_objects.end(); ++i) {
972 MovingObject* moving_object = *i;
973 if((moving_object->get_group() != COLGROUP_MOVING
974 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
975 || !moving_object->is_valid())
978 Vector movement = moving_object->get_movement();
980 // test if x or y movement is dominant
981 if(fabsf(moving_object->get_movement().x) < fabsf(moving_object->get_movement().y)) {
983 // test in x direction first, then y direction
984 moving_object->dest.move(Vector(0, -movement.y));
985 for(int i = 0; i < 2; ++i) {
986 bool res = collision_static(moving_object, Vector(movement.x, 0));
990 moving_object->dest.move(Vector(0, movement.y));
991 for(int i = 0; i < 2; ++i) {
992 bool res = collision_static(moving_object, Vector(0, movement.y));
999 // test in y direction first, then x direction
1000 moving_object->dest.move(Vector(-movement.x, 0));
1001 for(int i = 0; i < 2; ++i) {
1002 bool res = collision_static(moving_object, Vector(0, movement.y));
1006 moving_object->dest.move(Vector(movement.x, 0));
1007 for(int i = 0; i < 2; ++i) {
1008 bool res = collision_static(moving_object, Vector(movement.x, 0));
1015 // part2: COLGROUP_MOVING vs tile attributes
1016 for(MovingObjects::iterator i = moving_objects.begin();
1017 i != moving_objects.end(); ++i) {
1018 MovingObject* moving_object = *i;
1019 if((moving_object->get_group() != COLGROUP_MOVING
1020 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1021 || !moving_object->is_valid())
1024 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1025 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1026 moving_object->collision_tile(tile_attributes);
1030 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1031 for(MovingObjects::iterator i = moving_objects.begin();
1032 i != moving_objects.end(); ++i) {
1033 MovingObject* moving_object = *i;
1034 if(moving_object->get_group() != COLGROUP_MOVING
1035 || !moving_object->is_valid())
1038 for(MovingObjects::iterator i2 = moving_objects.begin();
1039 i2 != moving_objects.end(); ++i2) {
1040 MovingObject* moving_object_2 = *i2;
1041 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1042 || !moving_object_2->is_valid())
1045 collision_object(moving_object, moving_object_2);
1049 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1050 for(MovingObjects::iterator i = moving_objects.begin();
1051 i != moving_objects.end(); ++i) {
1052 MovingObject* moving_object = *i;
1054 if(moving_object->get_group() != COLGROUP_MOVING
1055 || !moving_object->is_valid())
1058 for(MovingObjects::iterator i2 = i+1;
1059 i2 != moving_objects.end(); ++i2) {
1060 MovingObject* moving_object_2 = *i2;
1061 if(moving_object_2->get_group() != COLGROUP_MOVING
1062 || !moving_object_2->is_valid())
1065 collision_object(moving_object, moving_object_2);
1069 // apply object movement
1070 for(MovingObjects::iterator i = moving_objects.begin();
1071 i != moving_objects.end(); ++i) {
1072 MovingObject* moving_object = *i;
1074 moving_object->bbox = moving_object->dest;
1075 moving_object->movement = Vector(0, 0);
1080 Sector::is_free_space(const Rect& rect) const
1082 // test with all tiles in this rectangle
1083 int starttilex = int(rect.p1.x) / 32;
1084 int starttiley = int(rect.p1.y) / 32;
1085 int max_x = int(rect.p2.x);
1086 int max_y = int(rect.p2.y);
1088 for(int x = starttilex; x*32 <= max_x; ++x) {
1089 for(int y = starttiley; y*32 <= max_y; ++y) {
1090 const Tile* tile = solids->get_tile(x, y);
1093 if(tile->getAttributes() & Tile::SOLID)
1098 for(MovingObjects::const_iterator i = moving_objects.begin();
1099 i != moving_objects.end(); ++i) {
1100 const MovingObject* moving_object = *i;
1101 if(moving_object->get_group() != COLGROUP_STATIC
1102 || !moving_object->is_valid())
1105 if(Collision::intersects(rect, moving_object->get_bbox()))
1113 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1115 // TODO remove this function and move these checks elsewhere...
1117 Bullet* new_bullet = 0;
1118 if(player_status->bonus == FIRE_BONUS) {
1119 if((int)bullets.size() >= player_status->max_fire_bullets)
1121 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
1122 } else if(player_status->bonus == ICE_BONUS) {
1123 if((int)bullets.size() >= player_status->max_ice_bullets)
1125 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
1129 add_object(new_bullet);
1131 sound_manager->play("sounds/shoot.wav");
1137 Sector::add_smoke_cloud(const Vector& pos)
1139 add_object(new SmokeCloud(pos));
1144 Sector::add_floating_text(const Vector& pos, const std::string& text)
1146 add_object(new FloatingText(pos, text));
1150 Sector::play_music(MusicType type)
1152 currentmusic = type;
1153 switch(currentmusic) {
1155 sound_manager->play_music(music);
1158 sound_manager->play_music("music/salcon.ogg");
1160 case HERRING_WARNING_MUSIC:
1161 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1164 sound_manager->play_music("");
1170 Sector::get_music_type()
1172 return currentmusic;
1176 Sector::get_total_badguys()
1178 int total_badguys = 0;
1179 for(GameObjects::iterator i = gameobjects.begin();
1180 i != gameobjects.end(); ++i) {
1181 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1182 if (badguy && badguy->countMe)
1186 return total_badguys;
1190 Sector::inside(const Rect& rect) const
1192 if(rect.p1.x > solids->get_width() * 32
1193 || rect.p1.y > solids->get_height() * 32