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_collectgarbage(global_vm);
89 sq_newtable(global_vm);
90 sq_pushroottable(global_vm);
91 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
92 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
94 sq_resetobject(§or_table);
95 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
96 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
97 sq_addref(global_vm, §or_table);
103 using namespace Scripting;
107 for(ScriptList::iterator i = scripts.begin();
108 i != scripts.end(); ++i) {
109 HSQOBJECT& object = *i;
110 sq_release(global_vm, &object);
112 sq_release(global_vm, §or_table);
113 sq_collectgarbage(global_vm);
115 update_game_objects();
116 assert(gameobjects_new.size() == 0);
118 for(GameObjects::iterator i = gameobjects.begin();
119 i != gameobjects.end(); ++i) {
120 GameObject* object = *i;
121 before_object_remove(object);
125 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
137 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
139 if(name == "camera") {
140 Camera* camera = new Camera(this);
141 camera->parse(reader);
143 } else if(name == "particles-snow") {
144 SnowParticleSystem* partsys = new SnowParticleSystem();
145 partsys->parse(reader);
147 } else if(name == "particles-rain") {
148 RainParticleSystem* partsys = new RainParticleSystem();
149 partsys->parse(reader);
151 } else if(name == "particles-comets") {
152 CometParticleSystem* partsys = new CometParticleSystem();
153 partsys->parse(reader);
155 } else if(name == "particles-ghosts") {
156 GhostParticleSystem* partsys = new GhostParticleSystem();
157 partsys->parse(reader);
159 } else if(name == "particles-clouds") {
160 CloudParticleSystem* partsys = new CloudParticleSystem();
161 partsys->parse(reader);
163 } else if(name == "money") { // for compatibility with old maps
164 return new Jumpy(reader);
168 return create_object(name, reader);
169 } catch(std::exception& e) {
170 log_warning << e.what() << "" << std::endl;
177 Sector::parse(const lisp::Lisp& sector)
179 lisp::ListIterator iter(§or);
181 const std::string& token = iter.item();
182 if(token == "name") {
183 iter.value()->get(name);
184 } else if(token == "gravity") {
185 iter.value()->get(gravity);
186 } else if(token == "music") {
187 iter.value()->get(music);
188 } else if(token == "spawnpoint") {
189 SpawnPoint* sp = new SpawnPoint(iter.lisp());
190 spawnpoints.push_back(sp);
191 } else if(token == "init-script") {
192 iter.value()->get(init_script);
194 GameObject* object = parse_object(token, *(iter.lisp()));
201 update_game_objects();
204 throw std::runtime_error("sector does not contain a solid tile layer.");
208 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
209 update_game_objects();
210 add_object(new Camera(this));
213 update_game_objects();
217 Sector::parse_old_format(const lisp::Lisp& reader)
220 reader.get("gravity", gravity);
222 std::string backgroundimage;
223 reader.get("background", backgroundimage);
225 reader.get("bkgd_speed", bgspeed);
228 Color bkgd_top, bkgd_bottom;
229 int r = 0, g = 0, b = 128;
230 reader.get("bkgd_red_top", r);
231 reader.get("bkgd_green_top", g);
232 reader.get("bkgd_blue_top", b);
233 bkgd_top.red = static_cast<float> (r) / 255.0f;
234 bkgd_top.green = static_cast<float> (g) / 255.0f;
235 bkgd_top.blue = static_cast<float> (b) / 255.0f;
237 reader.get("bkgd_red_bottom", r);
238 reader.get("bkgd_green_bottom", g);
239 reader.get("bkgd_blue_bottom", b);
240 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
241 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
242 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
244 if(backgroundimage != "") {
245 Background* background = new Background();
246 background->set_image(
247 std::string("images/background/") + backgroundimage, bgspeed);
248 add_object(background);
250 Gradient* gradient = new Gradient();
251 gradient->set_gradient(bkgd_top, bkgd_bottom);
252 add_object(gradient);
255 std::string particlesystem;
256 reader.get("particle_system", particlesystem);
257 if(particlesystem == "clouds")
258 add_object(new CloudParticleSystem());
259 else if(particlesystem == "snow")
260 add_object(new SnowParticleSystem());
261 else if(particlesystem == "rain")
262 add_object(new RainParticleSystem());
264 Vector startpos(100, 170);
265 reader.get("start_pos_x", startpos.x);
266 reader.get("start_pos_y", startpos.y);
268 SpawnPoint* spawn = new SpawnPoint;
269 spawn->pos = startpos;
270 spawn->name = "main";
271 spawnpoints.push_back(spawn);
273 music = "chipdisko.ogg";
274 reader.get("music", music);
275 music = "music/" + music;
277 int width = 30, height = 15;
278 reader.get("width", width);
279 reader.get("height", height);
281 std::vector<unsigned int> tiles;
282 if(reader.get_vector("interactive-tm", tiles)
283 || reader.get_vector("tilemap", tiles)) {
284 TileMap* tilemap = new TileMap();
285 tilemap->set(width, height, tiles, LAYER_TILES, true);
289 if(reader.get_vector("background-tm", tiles)) {
290 TileMap* tilemap = new TileMap();
291 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
295 if(reader.get_vector("foreground-tm", tiles)) {
296 TileMap* tilemap = new TileMap();
297 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
301 // read reset-points (now spawn-points)
302 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
304 lisp::ListIterator iter(resetpoints);
306 if(iter.item() == "point") {
308 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
310 SpawnPoint* sp = new SpawnPoint;
313 spawnpoints.push_back(sp);
316 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
322 const lisp::Lisp* objects = reader.get_lisp("objects");
324 lisp::ListIterator iter(objects);
326 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
330 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
336 Camera* camera = new Camera(this);
339 update_game_objects();
342 throw std::runtime_error("sector does not contain a solid tile layer.");
345 update_game_objects();
349 Sector::fix_old_tiles()
352 for(size_t x=0; x < solids->get_width(); ++x) {
353 for(size_t y=0; y < solids->get_height(); ++y) {
354 const Tile* tile = solids->get_tile(x, y);
355 Vector pos(x*32, y*32);
357 if(tile->getID() == 112) {
358 add_object(new InvisibleBlock(pos));
359 solids->change(x, y, 0);
360 } else if(tile->getAttributes() & Tile::COIN) {
361 add_object(new Coin(pos));
362 solids->change(x, y, 0);
363 } else if(tile->getAttributes() & Tile::FULLBOX) {
364 add_object(new BonusBlock(pos, tile->getData()));
365 solids->change(x, y, 0);
366 } else if(tile->getAttributes() & Tile::BRICK) {
367 add_object(new Brick(pos, tile->getData()));
368 solids->change(x, y, 0);
369 } else if(tile->getAttributes() & Tile::GOAL) {
370 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
371 add_object(new SequenceTrigger(pos, sequence));
372 solids->change(x, y, 0);
379 Sector::write(lisp::Writer& writer)
381 writer.write_string("name", name);
382 writer.write_float("gravity", gravity);
383 writer.write_string("music", music);
386 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
388 SpawnPoint* spawn = *i;
389 writer.start_list("spawn-points");
390 writer.write_string("name", spawn->name);
391 writer.write_float("x", spawn->pos.x);
392 writer.write_float("y", spawn->pos.y);
393 writer.end_list("spawn-points");
397 for(GameObjects::iterator i = gameobjects.begin();
398 i != gameobjects.end(); ++i) {
399 Serializable* serializable = dynamic_cast<Serializable*> (*i);
401 serializable->write(writer);
406 Sector::run_script(std::istream& in, const std::string& sourcename)
408 using namespace Scripting;
410 // garbage collect thread list
411 for(ScriptList::iterator i = scripts.begin();
412 i != scripts.end(); ) {
413 HSQOBJECT& object = *i;
414 HSQUIRRELVM vm = object_to_vm(object);
416 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
417 sq_release(global_vm, &object);
418 i = scripts.erase(i);
425 HSQOBJECT object = create_thread(global_vm);
426 scripts.push_back(object);
428 HSQUIRRELVM vm = object_to_vm(object);
430 // set sector_table as roottable for the thread
431 sq_pushobject(vm, sector_table);
434 compile_and_run(vm, in, sourcename);
440 Sector::add_object(GameObject* object)
442 // make sure the object isn't already in the list
444 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
447 assert("object already added to sector" == 0);
450 for(GameObjects::iterator i = gameobjects_new.begin();
451 i != gameobjects_new.end(); ++i) {
453 assert("object already added to sector" == 0);
459 gameobjects_new.push_back(object);
463 Sector::activate(const std::string& spawnpoint)
466 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
468 if((*i)->name == spawnpoint) {
474 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
475 if(spawnpoint != "main") {
478 activate(Vector(0, 0));
486 Sector::activate(const Vector& player_pos)
488 if(_current != this) {
490 _current->deactivate();
493 // register sectortable as sector in scripting
494 HSQUIRRELVM vm = Scripting::global_vm;
495 sq_pushroottable(vm);
496 sq_pushstring(vm, "sector", -1);
497 sq_pushobject(vm, sector_table);
498 if(SQ_FAILED(sq_createslot(vm, -3)))
499 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
502 for(GameObjects::iterator i = gameobjects.begin();
503 i != gameobjects.end(); ++i) {
504 GameObject* object = *i;
510 player->move(player_pos);
511 camera->reset(player->get_pos());
512 update_game_objects();
515 if(init_script != "") {
516 std::istringstream in(init_script);
517 run_script(in, std::string("Sector(") + name + ") - init");
527 // remove sector entry from global vm
528 HSQUIRRELVM vm = Scripting::global_vm;
529 sq_pushroottable(vm);
530 sq_pushstring(vm, "sector", -1);
531 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
532 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
535 for(GameObjects::iterator i = gameobjects.begin();
536 i != gameobjects.end(); ++i) {
537 GameObject* object = *i;
539 try_unexpose(object);
546 Sector::get_active_region()
549 camera->get_translation() - Vector(1600, 1200),
550 camera->get_translation() + Vector(1600, 1200));
554 Sector::update(float elapsed_time)
556 player->check_bounds(camera);
559 CollisionGridIterator iter(*grid, get_active_region());
560 while(MovingObject* object = iter.next()) {
561 if(!object->is_valid())
564 object->update(elapsed_time);
568 for(GameObjects::iterator i = gameobjects.begin();
569 i != gameobjects.end(); ++i) {
570 GameObject* object = *i;
571 if(!object->is_valid())
574 object->update(elapsed_time);
578 /* Handle all possible collisions. */
580 update_game_objects();
584 Sector::update_game_objects()
586 /** cleanup marked objects */
587 for(std::vector<Bullet*>::iterator i = bullets.begin();
588 i != bullets.end(); /* nothing */) {
590 if(bullet->is_valid()) {
595 i = bullets.erase(i);
597 for(MovingObjects::iterator i = moving_objects.begin();
598 i != moving_objects.end(); /* nothing */) {
599 MovingObject* moving_object = *i;
600 if(moving_object->is_valid()) {
606 grid->remove_object(moving_object);
609 i = moving_objects.erase(i);
611 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
612 i != gameobjects.end(); /* nothing */) {
613 GameObject* object = *i;
615 if(object->is_valid()) {
620 before_object_remove(object);
623 i = gameobjects.erase(i);
626 /* add newly created objects */
627 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
628 i != gameobjects_new.end(); ++i)
630 GameObject* object = *i;
632 before_object_add(object);
634 gameobjects.push_back(object);
636 gameobjects_new.clear();
640 Sector::before_object_add(GameObject* object)
642 Bullet* bullet = dynamic_cast<Bullet*> (object);
644 bullets.push_back(bullet);
646 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
648 moving_objects.push_back(movingobject);
650 grid->add_object(movingobject);
654 TileMap* tilemap = dynamic_cast<TileMap*> (object);
655 if(tilemap && tilemap->is_solid()) {
659 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
663 Camera* camera = dynamic_cast<Camera*> (object);
665 if(this->camera != 0) {
666 log_warning << "Multiple cameras added. Ignoring" << std::endl;
669 this->camera = camera;
672 Player* player = dynamic_cast<Player*> (object);
674 if(this->player != 0) {
675 log_warning << "Multiple players added. Ignoring" << std::endl;
678 this->player = player;
681 if(_current == this) {
689 Sector::try_expose(GameObject* object)
691 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
692 if(interface != NULL) {
693 HSQUIRRELVM vm = Scripting::global_vm;
694 sq_pushobject(vm, sector_table);
695 interface->expose(vm, -1);
701 Sector::before_object_remove(GameObject* object)
704 try_unexpose(object);
708 Sector::try_unexpose(GameObject* object)
710 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
711 if(interface != NULL) {
712 HSQUIRRELVM vm = Scripting::global_vm;
713 SQInteger oldtop = sq_gettop(vm);
714 sq_pushobject(vm, sector_table);
716 interface->unexpose(vm, -1);
717 } catch(std::exception& e) {
718 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
720 sq_settop(vm, oldtop);
725 Sector::draw(DrawingContext& context)
727 context.push_transform();
728 context.set_translation(camera->get_translation());
730 for(GameObjects::iterator i = gameobjects.begin();
731 i != gameobjects.end(); ++i) {
732 GameObject* object = *i;
733 if(!object->is_valid())
736 if (draw_solids_only)
738 TileMap* tm = dynamic_cast<TileMap*>(object);
739 if (tm && !tm->is_solid())
743 object->draw(context);
747 Color col(0.2, 0.2, 0.2, 0.7);
748 for(MovingObjects::iterator i = moving_objects.begin();
749 i != moving_objects.end(); ++i) {
750 MovingObject* object = *i;
751 const Rect& rect = object->get_bbox();
753 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
757 context.pop_transform();
760 static const float DELTA = .001;
763 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
764 CollisionHit& hit) const
766 // calculate rectangle where the object will move
767 float x1 = dest.get_left();
768 float x2 = dest.get_right();
769 float y1 = dest.get_top();
770 float y2 = dest.get_bottom();
772 // test with all tiles in this rectangle
773 int starttilex = int(x1) / 32;
774 int starttiley = int(y1) / 32;
775 int max_x = int(x2 + (1 - DELTA));
776 int max_y = int(y2 + (1 - DELTA));
778 CollisionHit temphit;
779 for(int x = starttilex; x*32 < max_x; ++x) {
780 for(int y = starttiley; y*32 < max_y; ++y) {
781 const Tile* tile = solids->get_tile(x, y);
784 // skip non-solid tiles
785 if(tile->getAttributes() == 0)
787 // only handle unisolid when the player is falling down and when he was
788 // above the tile before
789 if(tile->getAttributes() & Tile::UNISOLID) {
790 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
794 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
796 Vector p1(x*32, y*32);
797 Vector p2((x+1)*32, (y+1)*32);
798 triangle = AATriangle(p1, p2, tile->getData());
800 if(Collision::rectangle_aatriangle(temphit, dest, movement,
802 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
806 } else { // normal rectangular tile
807 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
808 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
809 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
819 Sector::collision_tile_attributes(const Rect& dest) const
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