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 "script_manager.hpp"
64 #include "scripting/wrapper_util.hpp"
65 #include "script_interface.hpp"
68 Sector* Sector::_current = 0;
70 bool Sector::show_collrects = false;
71 bool Sector::draw_solids_only = false;
74 : currentmusic(LEVEL_MUSIC), gravity(10),
75 player(0), solids(0), camera(0)
77 add_object(new Player(player_status));
78 add_object(new DisplayEffect());
79 add_object(new TextObject());
82 grid.reset(new CollisionGrid(32000, 32000));
85 script_manager.reset(new ScriptManager(ScriptManager::instance));
87 // create a new squirrel table for the sector
88 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
92 if(SQ_FAILED(sq_setdelegate(vm, -2)))
93 throw Scripting::SquirrelError(vm, "Couldn't set sector_table delegate");
95 sq_resetobject(§or_table);
96 if(SQ_FAILED(sq_getstackobj(vm, -1, §or_table)))
97 throw Scripting::SquirrelError(vm, "Couldn't get sector table");
98 sq_addref(vm, §or_table);
106 script_manager.reset(NULL);
107 sq_release(ScriptManager::instance->get_vm(), §or_table);
109 update_game_objects();
110 assert(gameobjects_new.size() == 0);
112 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
114 before_object_remove(*i);
118 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
124 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
126 if(name == "camera") {
127 Camera* camera = new Camera(this);
128 camera->parse(reader);
130 } else if(name == "particles-snow") {
131 SnowParticleSystem* partsys = new SnowParticleSystem();
132 partsys->parse(reader);
134 } else if(name == "particles-rain") {
135 RainParticleSystem* partsys = new RainParticleSystem();
136 partsys->parse(reader);
138 } else if(name == "particles-comets") {
139 CometParticleSystem* partsys = new CometParticleSystem();
140 partsys->parse(reader);
142 } else if(name == "particles-ghosts") {
143 GhostParticleSystem* partsys = new GhostParticleSystem();
144 partsys->parse(reader);
146 } else if(name == "particles-clouds") {
147 CloudParticleSystem* partsys = new CloudParticleSystem();
148 partsys->parse(reader);
150 } else if(name == "money") { // for compatibility with old maps
151 return new Jumpy(reader);
155 return create_object(name, reader);
156 } catch(std::exception& e) {
157 log_warning << e.what() << "" << std::endl;
164 Sector::parse(const lisp::Lisp& sector)
166 lisp::ListIterator iter(§or);
168 const std::string& token = iter.item();
169 if(token == "name") {
170 iter.value()->get(name);
171 } else if(token == "gravity") {
172 iter.value()->get(gravity);
173 } else if(token == "music") {
174 iter.value()->get(music);
175 } else if(token == "spawnpoint") {
176 SpawnPoint* sp = new SpawnPoint(iter.lisp());
177 spawnpoints.push_back(sp);
178 } else if(token == "init-script") {
179 iter.value()->get(init_script);
181 GameObject* object = parse_object(token, *(iter.lisp()));
188 update_game_objects();
191 throw std::runtime_error("sector does not contain a solid tile layer.");
195 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
196 update_game_objects();
197 add_object(new Camera(this));
200 update_game_objects();
204 Sector::parse_old_format(const lisp::Lisp& reader)
207 reader.get("gravity", gravity);
209 std::string backgroundimage;
210 reader.get("background", backgroundimage);
212 reader.get("bkgd_speed", bgspeed);
215 Color bkgd_top, bkgd_bottom;
216 int r = 0, g = 0, b = 128;
217 reader.get("bkgd_red_top", r);
218 reader.get("bkgd_green_top", g);
219 reader.get("bkgd_blue_top", b);
220 bkgd_top.red = static_cast<float> (r) / 255.0f;
221 bkgd_top.green = static_cast<float> (g) / 255.0f;
222 bkgd_top.blue = static_cast<float> (b) / 255.0f;
224 reader.get("bkgd_red_bottom", r);
225 reader.get("bkgd_green_bottom", g);
226 reader.get("bkgd_blue_bottom", b);
227 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
228 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
229 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
231 if(backgroundimage != "") {
232 Background* background = new Background();
233 background->set_image(
234 std::string("images/background/") + backgroundimage, bgspeed);
235 add_object(background);
237 Gradient* gradient = new Gradient();
238 gradient->set_gradient(bkgd_top, bkgd_bottom);
239 add_object(gradient);
242 std::string particlesystem;
243 reader.get("particle_system", particlesystem);
244 if(particlesystem == "clouds")
245 add_object(new CloudParticleSystem());
246 else if(particlesystem == "snow")
247 add_object(new SnowParticleSystem());
248 else if(particlesystem == "rain")
249 add_object(new RainParticleSystem());
251 Vector startpos(100, 170);
252 reader.get("start_pos_x", startpos.x);
253 reader.get("start_pos_y", startpos.y);
255 SpawnPoint* spawn = new SpawnPoint;
256 spawn->pos = startpos;
257 spawn->name = "main";
258 spawnpoints.push_back(spawn);
260 music = "chipdisko.ogg";
261 reader.get("music", music);
262 music = "music/" + music;
264 int width = 30, height = 15;
265 reader.get("width", width);
266 reader.get("height", height);
268 std::vector<unsigned int> tiles;
269 if(reader.get_vector("interactive-tm", tiles)
270 || reader.get_vector("tilemap", tiles)) {
271 TileMap* tilemap = new TileMap();
272 tilemap->set(width, height, tiles, LAYER_TILES, true);
276 if(reader.get_vector("background-tm", tiles)) {
277 TileMap* tilemap = new TileMap();
278 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
282 if(reader.get_vector("foreground-tm", tiles)) {
283 TileMap* tilemap = new TileMap();
284 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
288 // read reset-points (now spawn-points)
289 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
291 lisp::ListIterator iter(resetpoints);
293 if(iter.item() == "point") {
295 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
297 SpawnPoint* sp = new SpawnPoint;
300 spawnpoints.push_back(sp);
303 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
309 const lisp::Lisp* objects = reader.get_lisp("objects");
311 lisp::ListIterator iter(objects);
313 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
317 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
323 Camera* camera = new Camera(this);
326 update_game_objects();
329 throw std::runtime_error("sector does not contain a solid tile layer.");
332 update_game_objects();
336 Sector::fix_old_tiles()
339 for(size_t x=0; x < solids->get_width(); ++x) {
340 for(size_t y=0; y < solids->get_height(); ++y) {
341 const Tile* tile = solids->get_tile(x, y);
342 Vector pos(x*32, y*32);
344 if(tile->getID() == 112) {
345 add_object(new InvisibleBlock(pos));
346 solids->change(x, y, 0);
347 } else if(tile->getAttributes() & Tile::COIN) {
348 add_object(new Coin(pos));
349 solids->change(x, y, 0);
350 } else if(tile->getAttributes() & Tile::FULLBOX) {
351 add_object(new BonusBlock(pos, tile->getData()));
352 solids->change(x, y, 0);
353 } else if(tile->getAttributes() & Tile::BRICK) {
354 add_object(new Brick(pos, tile->getData()));
355 solids->change(x, y, 0);
356 } else if(tile->getAttributes() & Tile::GOAL) {
357 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
358 add_object(new SequenceTrigger(pos, sequence));
359 solids->change(x, y, 0);
366 Sector::write(lisp::Writer& writer)
368 writer.write_string("name", name);
369 writer.write_float("gravity", gravity);
370 writer.write_string("music", music);
373 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
375 SpawnPoint* spawn = *i;
376 writer.start_list("spawn-points");
377 writer.write_string("name", spawn->name);
378 writer.write_float("x", spawn->pos.x);
379 writer.write_float("y", spawn->pos.y);
380 writer.end_list("spawn-points");
384 for(GameObjects::iterator i = gameobjects.begin();
385 i != gameobjects.end(); ++i) {
386 Serializable* serializable = dynamic_cast<Serializable*> (*i);
388 serializable->write(writer);
393 Sector::run_script(std::istream& in, const std::string& sourcename)
395 // create new thread and keep a weakref
396 HSQUIRRELVM vm = script_manager->create_thread();
398 // set sector_table as roottable for the thread
399 sq_pushobject(vm, sector_table);
402 Scripting::compile_and_run(vm, in, sourcename);
408 Sector::add_object(GameObject* object)
410 // make sure the object isn't already in the list
412 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
415 assert("object already added to sector" == 0);
418 for(GameObjects::iterator i = gameobjects_new.begin();
419 i != gameobjects_new.end(); ++i) {
421 assert("object already added to sector" == 0);
426 gameobjects_new.push_back(object);
430 Sector::activate(const std::string& spawnpoint)
433 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
435 if((*i)->name == spawnpoint) {
441 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
442 if(spawnpoint != "main") {
445 activate(Vector(0, 0));
453 Sector::activate(const Vector& player_pos)
455 if(_current != this) {
457 _current->deactivate();
460 // register sectortable as current_sector in scripting
461 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
462 sq_pushroottable(vm);
463 sq_pushstring(vm, "sector", -1);
464 sq_pushobject(vm, sector_table);
465 if(SQ_FAILED(sq_createslot(vm, -3)))
466 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
469 for(GameObjects::iterator i = gameobjects.begin();
470 i != gameobjects.end(); ++i) {
471 GameObject* object = *i;
477 player->move(player_pos);
478 camera->reset(player->get_pos());
479 update_game_objects();
482 if(init_script != "") {
483 std::istringstream in(init_script);
484 run_script(in, std::string("Sector(") + name + ") - init");
494 // remove sector entry from global vm
495 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
496 sq_pushroottable(vm);
497 sq_pushstring(vm, "sector", -1);
498 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
499 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
502 for(GameObjects::iterator i = gameobjects.begin();
503 i != gameobjects.end(); ++i) {
504 GameObject* object = *i;
506 try_unexpose(object);
513 Sector::get_active_region()
516 camera->get_translation() - Vector(1600, 1200),
517 camera->get_translation() + Vector(1600, 1200));
521 Sector::update(float elapsed_time)
523 script_manager->update();
525 player->check_bounds(camera);
528 CollisionGridIterator iter(*grid, get_active_region());
529 while(MovingObject* object = iter.next()) {
530 if(!object->is_valid())
533 object->update(elapsed_time);
537 for(GameObjects::iterator i = gameobjects.begin();
538 i != gameobjects.end(); ++i) {
539 GameObject* object = *i;
540 if(!object->is_valid())
543 object->update(elapsed_time);
547 /* Handle all possible collisions. */
549 update_game_objects();
553 Sector::update_game_objects()
555 /** cleanup marked objects */
556 for(std::vector<Bullet*>::iterator i = bullets.begin();
557 i != bullets.end(); /* nothing */) {
559 if(bullet->is_valid()) {
564 i = bullets.erase(i);
566 for(MovingObjects::iterator i = moving_objects.begin();
567 i != moving_objects.end(); /* nothing */) {
568 MovingObject* moving_object = *i;
569 if(moving_object->is_valid()) {
575 grid->remove_object(moving_object);
578 i = moving_objects.erase(i);
580 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
581 i != gameobjects.end(); /* nothing */) {
582 GameObject* object = *i;
584 if(object->is_valid()) {
589 before_object_remove(object);
592 i = gameobjects.erase(i);
595 /* add newly created objects */
596 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
597 i != gameobjects_new.end(); ++i)
599 GameObject* object = *i;
601 before_object_add(object);
603 gameobjects.push_back(object);
605 gameobjects_new.clear();
609 Sector::before_object_add(GameObject* object)
611 Bullet* bullet = dynamic_cast<Bullet*> (object);
613 bullets.push_back(bullet);
615 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
617 moving_objects.push_back(movingobject);
619 grid->add_object(movingobject);
623 TileMap* tilemap = dynamic_cast<TileMap*> (object);
624 if(tilemap && tilemap->is_solid()) {
628 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
632 Camera* camera = dynamic_cast<Camera*> (object);
634 if(this->camera != 0) {
635 log_warning << "Multiple cameras added. Ignoring" << std::endl;
638 this->camera = camera;
641 Player* player = dynamic_cast<Player*> (object);
643 if(this->player != 0) {
644 log_warning << "Multiple players added. Ignoring" << std::endl;
647 this->player = player;
650 if(_current == this) {
658 Sector::try_expose(GameObject* object)
660 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
661 if(interface != NULL) {
662 HSQUIRRELVM vm = script_manager->get_vm();
663 sq_pushobject(vm, sector_table);
664 interface->expose(vm, -1);
670 Sector::before_object_remove(GameObject* object)
673 try_unexpose(object);
677 Sector::try_unexpose(GameObject* object)
679 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
680 if(interface != NULL) {
681 HSQUIRRELVM vm = script_manager->get_vm();
682 int oldtop = sq_gettop(vm);
683 sq_pushobject(vm, sector_table);
685 interface->unexpose(vm, -1);
686 } catch(std::exception& e) {
687 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
689 sq_settop(vm, oldtop);
694 Sector::draw(DrawingContext& context)
696 context.push_transform();
697 context.set_translation(camera->get_translation());
699 for(GameObjects::iterator i = gameobjects.begin();
700 i != gameobjects.end(); ++i) {
701 GameObject* object = *i;
702 if(!object->is_valid())
705 if (draw_solids_only)
707 TileMap* tm = dynamic_cast<TileMap*>(object);
708 if (tm && !tm->is_solid())
712 object->draw(context);
716 Color col(0.2, 0.2, 0.2, 0.7);
717 for(MovingObjects::iterator i = moving_objects.begin();
718 i != moving_objects.end(); ++i) {
719 MovingObject* object = *i;
720 const Rect& rect = object->get_bbox();
722 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
726 context.pop_transform();
729 static const float DELTA = .001;
732 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
733 CollisionHit& hit) const
735 // calculate rectangle where the object will move
736 float x1 = dest.get_left();
737 float x2 = dest.get_right();
738 float y1 = dest.get_top();
739 float y2 = dest.get_bottom();
741 // test with all tiles in this rectangle
742 int starttilex = int(x1) / 32;
743 int starttiley = int(y1) / 32;
744 int max_x = int(x2 + (1 - DELTA));
745 int max_y = int(y2 + (1 - DELTA));
747 CollisionHit temphit;
748 for(int x = starttilex; x*32 < max_x; ++x) {
749 for(int y = starttiley; y*32 < max_y; ++y) {
750 const Tile* tile = solids->get_tile(x, y);
753 // skip non-solid tiles
754 if(tile->getAttributes() == 0)
756 // only handle unisolid when the player is falling down and when he was
757 // above the tile before
758 if(tile->getAttributes() & Tile::UNISOLID) {
759 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
763 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
765 Vector p1(x*32, y*32);
766 Vector p2((x+1)*32, (y+1)*32);
767 triangle = AATriangle(p1, p2, tile->getData());
769 if(Collision::rectangle_aatriangle(temphit, dest, movement,
771 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
775 } else { // normal rectangular tile
776 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
777 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
778 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
788 Sector::collision_tile_attributes(const Rect& dest) const
790 /** XXX This function doesn't work correctly as it will check all tiles
791 * in the bounding box of the object movement, this might include tiles
792 * that have actually never been touched by the object
793 * (though this only occures for very fast objects...)
797 // calculate rectangle where the object will move
799 if(object->get_movement().x >= 0) {
800 x1 = object->get_bbox().p1.x;
801 x2 = object->get_bbox().p2.x + object->get_movement().x;
803 x1 = object->get_bbox().p1.x + object->get_movement().x;
804 x2 = object->get_bbox().p2.x;
807 if(object->get_movement().y >= 0) {
808 y1 = object->get_bbox().p1.y;
809 y2 = object->get_bbox().p2.y + object->get_movement().y;
811 y1 = object->get_bbox().p1.y + object->get_movement().y;
812 y2 = object->get_bbox().p2.y;
815 float x1 = dest.p1.x;
816 float y1 = dest.p1.y;
817 float x2 = dest.p2.x;
818 float y2 = dest.p2.y;
820 // test with all tiles in this rectangle
821 int starttilex = int(x1) / 32;
822 int starttiley = int(y1) / 32;
827 for(int x = starttilex; x*32 < max_x; ++x) {
828 for(int y = starttiley; y*32 < max_y; ++y) {
829 const Tile* tile = solids->get_tile(x, y);
832 result |= tile->getAttributes();
840 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
844 Vector movement = object1->get_movement() - object2->get_movement();
845 if(Collision::rectangle_rectangle(hit, object1->dest, movement, object2->dest)) {
846 HitResponse response1 = object1->collision(*object2, hit);
848 HitResponse response2 = object2->collision(*object1, hit);
850 if(response1 != CONTINUE) {
851 if(response1 == ABORT_MOVE)
852 object1->dest = object1->get_bbox();
853 if(response2 == CONTINUE)
854 object2->dest.move(hit.normal * (hit.depth + DELTA));
855 } else if(response2 != CONTINUE) {
856 if(response2 == ABORT_MOVE)
857 object2->dest = object2->get_bbox();
858 if(response1 == CONTINUE)
859 object1->dest.move(-hit.normal * (hit.depth + DELTA));
861 object1->dest.move(-hit.normal * (hit.depth/2 + DELTA));
862 object2->dest.move(hit.normal * (hit.depth/2 + DELTA));
868 Sector::collision_static(MovingObject* object, const Vector& movement)
870 GameObject* collided_with = solids;
874 collision_tilemap(object->dest, movement, hit);
876 // collision with other (static) objects
877 CollisionHit temphit;
878 for(MovingObjects::iterator i2 = moving_objects.begin();
879 i2 != moving_objects.end(); ++i2) {
880 MovingObject* moving_object_2 = *i2;
881 if(moving_object_2->get_group() != COLGROUP_STATIC
882 || !moving_object_2->is_valid())
885 Rect dest = moving_object_2->dest;
888 = movement - moving_object_2->get_movement();
890 if(Collision::rectangle_rectangle(temphit, object->dest, rel_movement, dest)
891 && temphit.time > hit.time) {
893 collided_with = moving_object_2;
900 HitResponse response = object->collision(*collided_with, hit);
902 if(collided_with != solids) {
903 MovingObject* moving_object = (MovingObject*) collided_with;
904 HitResponse other_response = moving_object->collision(*object, hit);
905 if(other_response == ABORT_MOVE) {
906 moving_object->dest = moving_object->get_bbox();
907 } else if(other_response == FORCE_MOVE) {
908 // the static object "wins" move tux out of the collision
909 object->dest.move(-hit.normal * (hit.depth + DELTA));
911 } else if(other_response == PASS_MOVEMENT) {
912 object->dest.move(moving_object->get_movement());
913 //object->movement += moving_object->get_movement();
917 if(response == CONTINUE) {
918 object->dest.move(-hit.normal * (hit.depth + DELTA));
920 } else if(response == ABORT_MOVE) {
921 object->dest = object->get_bbox();
930 Sector::handle_collisions()
932 // calculate destination positions of the objects
933 for(MovingObjects::iterator i = moving_objects.begin();
934 i != moving_objects.end(); ++i) {
935 MovingObject* moving_object = *i;
937 moving_object->dest = moving_object->get_bbox();
938 moving_object->dest.move(moving_object->get_movement());
941 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
942 // we do this up to 4 times and have to sort all results for the smallest
943 // one before we can continue here
944 for(MovingObjects::iterator i = moving_objects.begin();
945 i != moving_objects.end(); ++i) {
946 MovingObject* moving_object = *i;
947 if((moving_object->get_group() != COLGROUP_MOVING
948 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
949 || !moving_object->is_valid())
952 Vector movement = moving_object->get_movement();
954 // test if x or y movement is dominant
955 if(fabsf(moving_object->get_movement().x) < fabsf(moving_object->get_movement().y)) {
957 // test in x direction first, then y direction
958 moving_object->dest.move(Vector(0, -movement.y));
959 for(int i = 0; i < 2; ++i) {
960 bool res = collision_static(moving_object, Vector(movement.x, 0));
964 moving_object->dest.move(Vector(0, movement.y));
965 for(int i = 0; i < 2; ++i) {
966 bool res = collision_static(moving_object, Vector(0, movement.y));
973 // test in y direction first, then x direction
974 moving_object->dest.move(Vector(-movement.x, 0));
975 for(int i = 0; i < 2; ++i) {
976 bool res = collision_static(moving_object, Vector(0, movement.y));
980 moving_object->dest.move(Vector(movement.x, 0));
981 for(int i = 0; i < 2; ++i) {
982 bool res = collision_static(moving_object, Vector(movement.x, 0));
989 // part2: COLGROUP_MOVING vs tile attributes
990 for(MovingObjects::iterator i = moving_objects.begin();
991 i != moving_objects.end(); ++i) {
992 MovingObject* moving_object = *i;
993 if((moving_object->get_group() != COLGROUP_MOVING
994 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
995 || !moving_object->is_valid())
998 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
999 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1000 moving_object->collision_tile(tile_attributes);
1004 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1005 for(MovingObjects::iterator i = moving_objects.begin();
1006 i != moving_objects.end(); ++i) {
1007 MovingObject* moving_object = *i;
1008 if(moving_object->get_group() != COLGROUP_MOVING
1009 || !moving_object->is_valid())
1012 for(MovingObjects::iterator i2 = moving_objects.begin();
1013 i2 != moving_objects.end(); ++i2) {
1014 MovingObject* moving_object_2 = *i2;
1015 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1016 || !moving_object_2->is_valid())
1019 collision_object(moving_object, moving_object_2);
1023 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1024 for(MovingObjects::iterator i = moving_objects.begin();
1025 i != moving_objects.end(); ++i) {
1026 MovingObject* moving_object = *i;
1028 if(moving_object->get_group() != COLGROUP_MOVING
1029 || !moving_object->is_valid())
1032 for(MovingObjects::iterator i2 = i+1;
1033 i2 != moving_objects.end(); ++i2) {
1034 MovingObject* moving_object_2 = *i2;
1035 if(moving_object_2->get_group() != COLGROUP_MOVING
1036 || !moving_object_2->is_valid())
1039 collision_object(moving_object, moving_object_2);
1043 // apply object movement
1044 for(MovingObjects::iterator i = moving_objects.begin();
1045 i != moving_objects.end(); ++i) {
1046 MovingObject* moving_object = *i;
1048 moving_object->bbox = moving_object->dest;
1049 moving_object->movement = Vector(0, 0);
1054 Sector::is_free_space(const Rect& rect) const
1056 // test with all tiles in this rectangle
1057 int starttilex = int(rect.p1.x) / 32;
1058 int starttiley = int(rect.p1.y) / 32;
1059 int max_x = int(rect.p2.x);
1060 int max_y = int(rect.p2.y);
1062 for(int x = starttilex; x*32 < max_x; ++x) {
1063 for(int y = starttiley; y*32 < max_y; ++y) {
1064 const Tile* tile = solids->get_tile(x, y);
1067 if(tile->getAttributes() & Tile::SOLID)
1072 for(MovingObjects::const_iterator i = moving_objects.begin();
1073 i != moving_objects.end(); ++i) {
1074 const MovingObject* moving_object = *i;
1075 if(moving_object->get_group() != COLGROUP_STATIC
1076 || !moving_object->is_valid())
1079 if(Collision::intersects(rect, moving_object->get_bbox()))
1087 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1089 // TODO remove this function and move these checks elsewhere...
1090 static const size_t MAX_FIRE_BULLETS = 2;
1091 static const size_t MAX_ICE_BULLETS = 1;
1093 Bullet* new_bullet = 0;
1094 if(player_status->bonus == FIRE_BONUS) {
1095 if(bullets.size() > MAX_FIRE_BULLETS-1)
1097 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
1098 } else if(player_status->bonus == ICE_BONUS) {
1099 if(bullets.size() > MAX_ICE_BULLETS-1)
1101 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
1105 add_object(new_bullet);
1107 sound_manager->play("sounds/shoot.wav");
1113 Sector::add_smoke_cloud(const Vector& pos)
1115 add_object(new SmokeCloud(pos));
1120 Sector::add_floating_text(const Vector& pos, const std::string& text)
1122 add_object(new FloatingText(pos, text));
1126 Sector::play_music(MusicType type)
1128 currentmusic = type;
1129 switch(currentmusic) {
1131 sound_manager->play_music(music);
1134 sound_manager->play_music("music/salcon.ogg");
1136 case HERRING_WARNING_MUSIC:
1137 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1140 sound_manager->play_music("");
1146 Sector::get_music_type()
1148 return currentmusic;
1152 Sector::get_total_badguys()
1154 int total_badguys = 0;
1155 for(GameObjects::iterator i = gameobjects.begin();
1156 i != gameobjects.end(); ++i) {
1157 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1158 if (badguy && badguy->countMe)
1162 return total_badguys;
1166 Sector::inside(const Rect& rect) const
1168 if(rect.p1.x > solids->get_width() * 32
1169 || rect.p1.y > solids->get_height() * 32
1170 || rect.p2.x < 0 || rect.p2.y < 0)