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;
73 Sector::Sector(Level* parent)
74 : level(parent), 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();
130 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
132 if(name == "camera") {
133 Camera* camera = new Camera(this);
134 camera->parse(reader);
136 } else if(name == "particles-snow") {
137 SnowParticleSystem* partsys = new SnowParticleSystem();
138 partsys->parse(reader);
140 } else if(name == "particles-rain") {
141 RainParticleSystem* partsys = new RainParticleSystem();
142 partsys->parse(reader);
144 } else if(name == "particles-comets") {
145 CometParticleSystem* partsys = new CometParticleSystem();
146 partsys->parse(reader);
148 } else if(name == "particles-ghosts") {
149 GhostParticleSystem* partsys = new GhostParticleSystem();
150 partsys->parse(reader);
152 } else if(name == "particles-clouds") {
153 CloudParticleSystem* partsys = new CloudParticleSystem();
154 partsys->parse(reader);
156 } else if(name == "money") { // for compatibility with old maps
157 return new Jumpy(reader);
161 return create_object(name, reader);
162 } catch(std::exception& e) {
163 log_warning << e.what() << "" << std::endl;
170 Sector::parse(const lisp::Lisp& sector)
172 lisp::ListIterator iter(§or);
174 const std::string& token = iter.item();
175 if(token == "name") {
176 iter.value()->get(name);
177 } else if(token == "gravity") {
178 iter.value()->get(gravity);
179 } else if(token == "music") {
180 iter.value()->get(music);
181 } else if(token == "spawnpoint") {
182 SpawnPoint* sp = new SpawnPoint(iter.lisp());
183 spawnpoints.push_back(sp);
184 } else if(token == "init-script") {
185 iter.value()->get(init_script);
187 GameObject* object = parse_object(token, *(iter.lisp()));
194 update_game_objects();
197 throw std::runtime_error("sector does not contain a solid tile layer.");
201 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
202 update_game_objects();
203 add_object(new Camera(this));
206 update_game_objects();
210 Sector::parse_old_format(const lisp::Lisp& reader)
213 reader.get("gravity", gravity);
215 std::string backgroundimage;
216 reader.get("background", backgroundimage);
218 reader.get("bkgd_speed", bgspeed);
221 Color bkgd_top, bkgd_bottom;
222 int r = 0, g = 0, b = 128;
223 reader.get("bkgd_red_top", r);
224 reader.get("bkgd_green_top", g);
225 reader.get("bkgd_blue_top", b);
226 bkgd_top.red = static_cast<float> (r) / 255.0f;
227 bkgd_top.green = static_cast<float> (g) / 255.0f;
228 bkgd_top.blue = static_cast<float> (b) / 255.0f;
230 reader.get("bkgd_red_bottom", r);
231 reader.get("bkgd_green_bottom", g);
232 reader.get("bkgd_blue_bottom", b);
233 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
234 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
235 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
237 if(backgroundimage != "") {
238 Background* background = new Background();
239 background->set_image(
240 std::string("images/background/") + backgroundimage, bgspeed);
241 add_object(background);
243 Gradient* gradient = new Gradient();
244 gradient->set_gradient(bkgd_top, bkgd_bottom);
245 add_object(gradient);
248 std::string particlesystem;
249 reader.get("particle_system", particlesystem);
250 if(particlesystem == "clouds")
251 add_object(new CloudParticleSystem());
252 else if(particlesystem == "snow")
253 add_object(new SnowParticleSystem());
254 else if(particlesystem == "rain")
255 add_object(new RainParticleSystem());
257 Vector startpos(100, 170);
258 reader.get("start_pos_x", startpos.x);
259 reader.get("start_pos_y", startpos.y);
261 SpawnPoint* spawn = new SpawnPoint;
262 spawn->pos = startpos;
263 spawn->name = "main";
264 spawnpoints.push_back(spawn);
266 music = "chipdisko.ogg";
267 reader.get("music", music);
268 music = "music/" + music;
270 int width = 30, height = 15;
271 reader.get("width", width);
272 reader.get("height", height);
274 std::vector<unsigned int> tiles;
275 if(reader.get_vector("interactive-tm", tiles)
276 || reader.get_vector("tilemap", tiles)) {
277 TileMap* tilemap = new TileMap();
278 tilemap->set(width, height, tiles, LAYER_TILES, true);
282 if(reader.get_vector("background-tm", tiles)) {
283 TileMap* tilemap = new TileMap();
284 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
288 if(reader.get_vector("foreground-tm", tiles)) {
289 TileMap* tilemap = new TileMap();
290 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
294 // read reset-points (now spawn-points)
295 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
297 lisp::ListIterator iter(resetpoints);
299 if(iter.item() == "point") {
301 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
303 SpawnPoint* sp = new SpawnPoint;
306 spawnpoints.push_back(sp);
309 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
315 const lisp::Lisp* objects = reader.get_lisp("objects");
317 lisp::ListIterator iter(objects);
319 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
323 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
329 Camera* camera = new Camera(this);
332 update_game_objects();
335 throw std::runtime_error("sector does not contain a solid tile layer.");
338 update_game_objects();
342 Sector::fix_old_tiles()
345 for(size_t x=0; x < solids->get_width(); ++x) {
346 for(size_t y=0; y < solids->get_height(); ++y) {
347 const Tile* tile = solids->get_tile(x, y);
348 Vector pos(x*32, y*32);
350 if(tile->getID() == 112) {
351 add_object(new InvisibleBlock(pos));
352 solids->change(x, y, 0);
353 } else if(tile->getAttributes() & Tile::COIN) {
354 add_object(new Coin(pos));
355 solids->change(x, y, 0);
356 } else if(tile->getAttributes() & Tile::FULLBOX) {
357 add_object(new BonusBlock(pos, tile->getData()));
358 solids->change(x, y, 0);
359 } else if(tile->getAttributes() & Tile::BRICK) {
360 add_object(new Brick(pos, tile->getData()));
361 solids->change(x, y, 0);
362 } else if(tile->getAttributes() & Tile::GOAL) {
363 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
364 add_object(new SequenceTrigger(pos, sequence));
365 solids->change(x, y, 0);
372 Sector::write(lisp::Writer& writer)
374 writer.write_string("name", name);
375 writer.write_float("gravity", gravity);
376 writer.write_string("music", music);
379 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
381 SpawnPoint* spawn = *i;
382 writer.start_list("spawn-points");
383 writer.write_string("name", spawn->name);
384 writer.write_float("x", spawn->pos.x);
385 writer.write_float("y", spawn->pos.y);
386 writer.end_list("spawn-points");
390 for(GameObjects::iterator i = gameobjects.begin();
391 i != gameobjects.end(); ++i) {
392 Serializable* serializable = dynamic_cast<Serializable*> (*i);
394 serializable->write(writer);
399 Sector::run_script(std::istream& in, const std::string& sourcename)
401 // create new thread and keep a weakref
402 HSQUIRRELVM vm = script_manager->create_thread();
404 // set sector_table as roottable for the thread
405 sq_pushobject(vm, sector_table);
408 Scripting::compile_and_run(vm, in, sourcename);
414 Sector::add_object(GameObject* object)
416 // make sure the object isn't already in the list
418 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
421 assert("object already added to sector" == 0);
424 for(GameObjects::iterator i = gameobjects_new.begin();
425 i != gameobjects_new.end(); ++i) {
427 assert("object already added to sector" == 0);
432 gameobjects_new.push_back(object);
436 Sector::activate(const std::string& spawnpoint)
439 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
441 if((*i)->name == spawnpoint) {
447 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
448 if(spawnpoint != "main") {
451 activate(Vector(0, 0));
459 Sector::activate(const Vector& player_pos)
461 if(_current != this) {
463 _current->deactivate();
466 // register sectortable as current_sector in scripting
467 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
468 sq_pushroottable(vm);
469 sq_pushstring(vm, "sector", -1);
470 sq_pushobject(vm, sector_table);
471 if(SQ_FAILED(sq_createslot(vm, -3)))
472 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
475 for(GameObjects::iterator i = gameobjects.begin();
476 i != gameobjects.end(); ++i) {
477 GameObject* object = *i;
483 player->move(player_pos);
484 camera->reset(player->get_pos());
485 update_game_objects();
488 if(init_script != "") {
489 std::istringstream in(init_script);
490 run_script(in, std::string("Sector(") + name + ") - init");
500 // remove sector entry from global vm
501 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
502 sq_pushroottable(vm);
503 sq_pushstring(vm, "sector", -1);
504 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
505 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
508 for(GameObjects::iterator i = gameobjects.begin();
509 i != gameobjects.end(); ++i) {
510 GameObject* object = *i;
512 try_unexpose(object);
519 Sector::get_active_region()
522 camera->get_translation() - Vector(1600, 1200),
523 camera->get_translation() + Vector(1600, 1200));
527 Sector::update(float elapsed_time)
529 script_manager->update();
531 player->check_bounds(camera);
534 CollisionGridIterator iter(*grid, get_active_region());
535 while(MovingObject* object = iter.next()) {
536 if(!object->is_valid())
539 object->update(elapsed_time);
543 for(GameObjects::iterator i = gameobjects.begin();
544 i != gameobjects.end(); ++i) {
545 GameObject* object = *i;
546 if(!object->is_valid())
549 object->update(elapsed_time);
553 /* Handle all possible collisions. */
555 update_game_objects();
559 Sector::update_game_objects()
561 /** cleanup marked objects */
562 for(std::vector<Bullet*>::iterator i = bullets.begin();
563 i != bullets.end(); /* nothing */) {
565 if(bullet->is_valid()) {
570 i = bullets.erase(i);
572 for(MovingObjects::iterator i = moving_objects.begin();
573 i != moving_objects.end(); /* nothing */) {
574 MovingObject* moving_object = *i;
575 if(moving_object->is_valid()) {
581 grid->remove_object(moving_object);
584 i = moving_objects.erase(i);
586 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
587 i != gameobjects.end(); /* nothing */) {
588 GameObject* object = *i;
590 if(object->is_valid()) {
595 before_object_remove(object);
598 i = gameobjects.erase(i);
601 /* add newly created objects */
602 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
603 i != gameobjects_new.end(); ++i)
605 GameObject* object = *i;
607 before_object_add(object);
609 gameobjects.push_back(object);
611 gameobjects_new.clear();
615 Sector::before_object_add(GameObject* object)
617 Bullet* bullet = dynamic_cast<Bullet*> (object);
619 bullets.push_back(bullet);
621 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
623 moving_objects.push_back(movingobject);
625 grid->add_object(movingobject);
629 TileMap* tilemap = dynamic_cast<TileMap*> (object);
630 if(tilemap && tilemap->is_solid()) {
634 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
638 Camera* camera = dynamic_cast<Camera*> (object);
640 if(this->camera != 0) {
641 log_warning << "Multiple cameras added. Ignoring" << std::endl;
644 this->camera = camera;
647 Player* player = dynamic_cast<Player*> (object);
649 if(this->player != 0) {
650 log_warning << "Multiple players added. Ignoring" << std::endl;
653 this->player = player;
656 if(_current == this) {
664 Sector::try_expose(GameObject* object)
666 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
667 if(interface != NULL) {
668 HSQUIRRELVM vm = script_manager->get_vm();
669 sq_pushobject(vm, sector_table);
670 interface->expose(vm, -1);
676 Sector::before_object_remove(GameObject* object)
679 try_unexpose(object);
683 Sector::try_unexpose(GameObject* object)
685 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
686 if(interface != NULL) {
687 HSQUIRRELVM vm = script_manager->get_vm();
688 int oldtop = sq_gettop(vm);
689 sq_pushobject(vm, sector_table);
691 interface->unexpose(vm, -1);
692 } catch(std::exception& e) {
693 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
695 sq_settop(vm, oldtop);
700 Sector::draw(DrawingContext& context)
702 context.push_transform();
703 context.set_translation(camera->get_translation());
705 for(GameObjects::iterator i = gameobjects.begin();
706 i != gameobjects.end(); ++i) {
707 GameObject* object = *i;
708 if(!object->is_valid())
711 if (draw_solids_only)
713 TileMap* tm = dynamic_cast<TileMap*>(object);
714 if (tm && !tm->is_solid())
718 object->draw(context);
722 Color col(0.2, 0.2, 0.2, 0.7);
723 for(MovingObjects::iterator i = moving_objects.begin();
724 i != moving_objects.end(); ++i) {
725 MovingObject* object = *i;
726 const Rect& rect = object->get_bbox();
728 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
732 context.pop_transform();
735 static const float DELTA = .001;
738 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
739 CollisionHit& hit) const
741 // calculate rectangle where the object will move
742 float x1 = dest.get_left();
743 float x2 = dest.get_right();
744 float y1 = dest.get_top();
745 float y2 = dest.get_bottom();
747 // test with all tiles in this rectangle
748 int starttilex = int(x1) / 32;
749 int starttiley = int(y1) / 32;
750 int max_x = int(x2 + (1 - DELTA));
751 int max_y = int(y2 + (1 - DELTA));
753 CollisionHit temphit;
754 for(int x = starttilex; x*32 < max_x; ++x) {
755 for(int y = starttiley; y*32 < max_y; ++y) {
756 const Tile* tile = solids->get_tile(x, y);
759 // skip non-solid tiles
760 if(tile->getAttributes() == 0)
762 // only handle unisolid when the player is falling down and when he was
763 // above the tile before
764 if(tile->getAttributes() & Tile::UNISOLID) {
765 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
769 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
771 Vector p1(x*32, y*32);
772 Vector p2((x+1)*32, (y+1)*32);
773 triangle = AATriangle(p1, p2, tile->getData());
775 if(Collision::rectangle_aatriangle(temphit, dest, movement,
777 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
781 } else { // normal rectangular tile
782 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
783 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
784 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
794 Sector::collision_tile_attributes(const Rect& dest) const
796 /** XXX This function doesn't work correctly as it will check all tiles
797 * in the bounding box of the object movement, this might include tiles
798 * that have actually never been touched by the object
799 * (though this only occures for very fast objects...)
803 // calculate rectangle where the object will move
805 if(object->get_movement().x >= 0) {
806 x1 = object->get_bbox().p1.x;
807 x2 = object->get_bbox().p2.x + object->get_movement().x;
809 x1 = object->get_bbox().p1.x + object->get_movement().x;
810 x2 = object->get_bbox().p2.x;
813 if(object->get_movement().y >= 0) {
814 y1 = object->get_bbox().p1.y;
815 y2 = object->get_bbox().p2.y + object->get_movement().y;
817 y1 = object->get_bbox().p1.y + object->get_movement().y;
818 y2 = object->get_bbox().p2.y;
821 float x1 = dest.p1.x;
822 float y1 = dest.p1.y;
823 float x2 = dest.p2.x;
824 float y2 = dest.p2.y;
826 // test with all tiles in this rectangle
827 int starttilex = int(x1) / 32;
828 int starttiley = int(y1) / 32;
833 for(int x = starttilex; x*32 < max_x; ++x) {
834 for(int y = starttiley; y*32 < max_y; ++y) {
835 const Tile* tile = solids->get_tile(x, y);
838 result |= tile->getAttributes();
846 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
850 Vector movement = object1->get_movement() - object2->get_movement();
851 if(Collision::rectangle_rectangle(hit, object1->dest, movement, object2->dest)) {
852 HitResponse response1 = object1->collision(*object2, hit);
854 HitResponse response2 = object2->collision(*object1, hit);
856 if(response1 != CONTINUE) {
857 if(response1 == ABORT_MOVE)
858 object1->dest = object1->get_bbox();
859 if(response2 == CONTINUE)
860 object2->dest.move(hit.normal * (hit.depth + DELTA));
861 } else if(response2 != CONTINUE) {
862 if(response2 == ABORT_MOVE)
863 object2->dest = object2->get_bbox();
864 if(response1 == CONTINUE)
865 object1->dest.move(-hit.normal * (hit.depth + DELTA));
867 object1->dest.move(-hit.normal * (hit.depth/2 + DELTA));
868 object2->dest.move(hit.normal * (hit.depth/2 + DELTA));
874 Sector::collision_static(MovingObject* object, const Vector& movement)
876 GameObject* collided_with = solids;
880 collision_tilemap(object->dest, movement, hit);
882 // collision with other (static) objects
883 CollisionHit temphit;
884 for(MovingObjects::iterator i2 = moving_objects.begin();
885 i2 != moving_objects.end(); ++i2) {
886 MovingObject* moving_object_2 = *i2;
887 if(moving_object_2->get_group() != COLGROUP_STATIC
888 || !moving_object_2->is_valid())
891 Rect dest = moving_object_2->dest;
894 = movement - moving_object_2->get_movement();
896 if(Collision::rectangle_rectangle(temphit, object->dest, rel_movement, dest)
897 && temphit.time > hit.time) {
899 collided_with = moving_object_2;
906 HitResponse response = object->collision(*collided_with, hit);
908 if(collided_with != solids) {
909 MovingObject* moving_object = (MovingObject*) collided_with;
910 HitResponse other_response = moving_object->collision(*object, hit);
911 if(other_response == ABORT_MOVE) {
912 moving_object->dest = moving_object->get_bbox();
913 } else if(other_response == FORCE_MOVE) {
914 // the static object "wins" move tux out of the collision
915 object->dest.move(-hit.normal * (hit.depth + DELTA));
917 } else if(other_response == PASS_MOVEMENT) {
918 object->dest.move(moving_object->get_movement());
919 //object->movement += moving_object->get_movement();
923 if(response == CONTINUE) {
924 object->dest.move(-hit.normal * (hit.depth + DELTA));
926 } else if(response == ABORT_MOVE) {
927 object->dest = object->get_bbox();
936 Sector::handle_collisions()
938 // calculate destination positions of the objects
939 for(MovingObjects::iterator i = moving_objects.begin();
940 i != moving_objects.end(); ++i) {
941 MovingObject* moving_object = *i;
943 moving_object->dest = moving_object->get_bbox();
944 moving_object->dest.move(moving_object->get_movement());
947 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
948 // we do this up to 4 times and have to sort all results for the smallest
949 // one before we can continue here
950 for(MovingObjects::iterator i = moving_objects.begin();
951 i != moving_objects.end(); ++i) {
952 MovingObject* moving_object = *i;
953 if((moving_object->get_group() != COLGROUP_MOVING
954 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
955 || !moving_object->is_valid())
958 Vector movement = moving_object->get_movement();
960 // test if x or y movement is dominant
961 if(fabsf(moving_object->get_movement().x) < fabsf(moving_object->get_movement().y)) {
963 // test in x direction first, then y direction
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(movement.x, 0));
970 moving_object->dest.move(Vector(0, movement.y));
971 for(int i = 0; i < 2; ++i) {
972 bool res = collision_static(moving_object, Vector(0, movement.y));
979 // test in y direction first, then x direction
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(0, movement.y));
986 moving_object->dest.move(Vector(movement.x, 0));
987 for(int i = 0; i < 2; ++i) {
988 bool res = collision_static(moving_object, Vector(movement.x, 0));
995 // part2: COLGROUP_MOVING vs tile attributes
996 for(MovingObjects::iterator i = moving_objects.begin();
997 i != moving_objects.end(); ++i) {
998 MovingObject* moving_object = *i;
999 if((moving_object->get_group() != COLGROUP_MOVING
1000 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1001 || !moving_object->is_valid())
1004 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1005 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1006 moving_object->collision_tile(tile_attributes);
1010 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1011 for(MovingObjects::iterator i = moving_objects.begin();
1012 i != moving_objects.end(); ++i) {
1013 MovingObject* moving_object = *i;
1014 if(moving_object->get_group() != COLGROUP_MOVING
1015 || !moving_object->is_valid())
1018 for(MovingObjects::iterator i2 = moving_objects.begin();
1019 i2 != moving_objects.end(); ++i2) {
1020 MovingObject* moving_object_2 = *i2;
1021 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1022 || !moving_object_2->is_valid())
1025 collision_object(moving_object, moving_object_2);
1029 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1030 for(MovingObjects::iterator i = moving_objects.begin();
1031 i != moving_objects.end(); ++i) {
1032 MovingObject* moving_object = *i;
1034 if(moving_object->get_group() != COLGROUP_MOVING
1035 || !moving_object->is_valid())
1038 for(MovingObjects::iterator i2 = i+1;
1039 i2 != moving_objects.end(); ++i2) {
1040 MovingObject* moving_object_2 = *i2;
1041 if(moving_object_2->get_group() != COLGROUP_MOVING
1042 || !moving_object_2->is_valid())
1045 collision_object(moving_object, moving_object_2);
1049 // apply object movement
1050 for(MovingObjects::iterator i = moving_objects.begin();
1051 i != moving_objects.end(); ++i) {
1052 MovingObject* moving_object = *i;
1054 moving_object->bbox = moving_object->dest;
1055 moving_object->movement = Vector(0, 0);
1060 Sector::is_free_space(const Rect& rect) const
1062 // test with all tiles in this rectangle
1063 int starttilex = int(rect.p1.x) / 32;
1064 int starttiley = int(rect.p1.y) / 32;
1065 int max_x = int(rect.p2.x);
1066 int max_y = int(rect.p2.y);
1068 for(int x = starttilex; x*32 <= max_x; ++x) {
1069 for(int y = starttiley; y*32 <= max_y; ++y) {
1070 const Tile* tile = solids->get_tile(x, y);
1073 if(tile->getAttributes() & Tile::SOLID)
1078 for(MovingObjects::const_iterator i = moving_objects.begin();
1079 i != moving_objects.end(); ++i) {
1080 const MovingObject* moving_object = *i;
1081 if(moving_object->get_group() != COLGROUP_STATIC
1082 || !moving_object->is_valid())
1085 if(Collision::intersects(rect, moving_object->get_bbox()))
1093 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1095 // TODO remove this function and move these checks elsewhere...
1097 Bullet* new_bullet = 0;
1098 if(player_status->bonus == FIRE_BONUS) {
1099 if((int)bullets.size() >= player_status->max_fire_bullets)
1101 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
1102 } else if(player_status->bonus == ICE_BONUS) {
1103 if((int)bullets.size() >= player_status->max_ice_bullets)
1105 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
1109 add_object(new_bullet);
1111 sound_manager->play("sounds/shoot.wav");
1117 Sector::add_smoke_cloud(const Vector& pos)
1119 add_object(new SmokeCloud(pos));
1124 Sector::add_floating_text(const Vector& pos, const std::string& text)
1126 add_object(new FloatingText(pos, text));
1130 Sector::play_music(MusicType type)
1132 currentmusic = type;
1133 switch(currentmusic) {
1135 sound_manager->play_music(music);
1138 sound_manager->play_music("music/salcon.ogg");
1140 case HERRING_WARNING_MUSIC:
1141 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1144 sound_manager->play_music("");
1150 Sector::get_music_type()
1152 return currentmusic;
1156 Sector::get_total_badguys()
1158 int total_badguys = 0;
1159 for(GameObjects::iterator i = gameobjects.begin();
1160 i != gameobjects.end(); ++i) {
1161 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1162 if (badguy && badguy->countMe)
1166 return total_badguys;
1170 Sector::inside(const Rect& rect) const
1172 if(rect.p1.x > solids->get_width() * 32
1173 || rect.p1.y > solids->get_height() * 32