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(); i != gameobjects.end();
120 before_object_remove(*i);
124 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
136 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
138 if(name == "camera") {
139 Camera* camera = new Camera(this);
140 camera->parse(reader);
142 } else if(name == "particles-snow") {
143 SnowParticleSystem* partsys = new SnowParticleSystem();
144 partsys->parse(reader);
146 } else if(name == "particles-rain") {
147 RainParticleSystem* partsys = new RainParticleSystem();
148 partsys->parse(reader);
150 } else if(name == "particles-comets") {
151 CometParticleSystem* partsys = new CometParticleSystem();
152 partsys->parse(reader);
154 } else if(name == "particles-ghosts") {
155 GhostParticleSystem* partsys = new GhostParticleSystem();
156 partsys->parse(reader);
158 } else if(name == "particles-clouds") {
159 CloudParticleSystem* partsys = new CloudParticleSystem();
160 partsys->parse(reader);
162 } else if(name == "money") { // for compatibility with old maps
163 return new Jumpy(reader);
167 return create_object(name, reader);
168 } catch(std::exception& e) {
169 log_warning << e.what() << "" << std::endl;
176 Sector::parse(const lisp::Lisp& sector)
178 lisp::ListIterator iter(§or);
180 const std::string& token = iter.item();
181 if(token == "name") {
182 iter.value()->get(name);
183 } else if(token == "gravity") {
184 iter.value()->get(gravity);
185 } else if(token == "music") {
186 iter.value()->get(music);
187 } else if(token == "spawnpoint") {
188 SpawnPoint* sp = new SpawnPoint(iter.lisp());
189 spawnpoints.push_back(sp);
190 } else if(token == "init-script") {
191 iter.value()->get(init_script);
193 GameObject* object = parse_object(token, *(iter.lisp()));
200 update_game_objects();
203 throw std::runtime_error("sector does not contain a solid tile layer.");
207 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
208 update_game_objects();
209 add_object(new Camera(this));
212 update_game_objects();
216 Sector::parse_old_format(const lisp::Lisp& reader)
219 reader.get("gravity", gravity);
221 std::string backgroundimage;
222 reader.get("background", backgroundimage);
224 reader.get("bkgd_speed", bgspeed);
227 Color bkgd_top, bkgd_bottom;
228 int r = 0, g = 0, b = 128;
229 reader.get("bkgd_red_top", r);
230 reader.get("bkgd_green_top", g);
231 reader.get("bkgd_blue_top", b);
232 bkgd_top.red = static_cast<float> (r) / 255.0f;
233 bkgd_top.green = static_cast<float> (g) / 255.0f;
234 bkgd_top.blue = static_cast<float> (b) / 255.0f;
236 reader.get("bkgd_red_bottom", r);
237 reader.get("bkgd_green_bottom", g);
238 reader.get("bkgd_blue_bottom", b);
239 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
240 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
241 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
243 if(backgroundimage != "") {
244 Background* background = new Background();
245 background->set_image(
246 std::string("images/background/") + backgroundimage, bgspeed);
247 add_object(background);
249 Gradient* gradient = new Gradient();
250 gradient->set_gradient(bkgd_top, bkgd_bottom);
251 add_object(gradient);
254 std::string particlesystem;
255 reader.get("particle_system", particlesystem);
256 if(particlesystem == "clouds")
257 add_object(new CloudParticleSystem());
258 else if(particlesystem == "snow")
259 add_object(new SnowParticleSystem());
260 else if(particlesystem == "rain")
261 add_object(new RainParticleSystem());
263 Vector startpos(100, 170);
264 reader.get("start_pos_x", startpos.x);
265 reader.get("start_pos_y", startpos.y);
267 SpawnPoint* spawn = new SpawnPoint;
268 spawn->pos = startpos;
269 spawn->name = "main";
270 spawnpoints.push_back(spawn);
272 music = "chipdisko.ogg";
273 reader.get("music", music);
274 music = "music/" + music;
276 int width = 30, height = 15;
277 reader.get("width", width);
278 reader.get("height", height);
280 std::vector<unsigned int> tiles;
281 if(reader.get_vector("interactive-tm", tiles)
282 || reader.get_vector("tilemap", tiles)) {
283 TileMap* tilemap = new TileMap();
284 tilemap->set(width, height, tiles, LAYER_TILES, true);
288 if(reader.get_vector("background-tm", tiles)) {
289 TileMap* tilemap = new TileMap();
290 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
294 if(reader.get_vector("foreground-tm", tiles)) {
295 TileMap* tilemap = new TileMap();
296 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
300 // read reset-points (now spawn-points)
301 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
303 lisp::ListIterator iter(resetpoints);
305 if(iter.item() == "point") {
307 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
309 SpawnPoint* sp = new SpawnPoint;
312 spawnpoints.push_back(sp);
315 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
321 const lisp::Lisp* objects = reader.get_lisp("objects");
323 lisp::ListIterator iter(objects);
325 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
329 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
335 Camera* camera = new Camera(this);
338 update_game_objects();
341 throw std::runtime_error("sector does not contain a solid tile layer.");
344 update_game_objects();
348 Sector::fix_old_tiles()
351 for(size_t x=0; x < solids->get_width(); ++x) {
352 for(size_t y=0; y < solids->get_height(); ++y) {
353 const Tile* tile = solids->get_tile(x, y);
354 Vector pos(x*32, y*32);
356 if(tile->getID() == 112) {
357 add_object(new InvisibleBlock(pos));
358 solids->change(x, y, 0);
359 } else if(tile->getAttributes() & Tile::COIN) {
360 add_object(new Coin(pos));
361 solids->change(x, y, 0);
362 } else if(tile->getAttributes() & Tile::FULLBOX) {
363 add_object(new BonusBlock(pos, tile->getData()));
364 solids->change(x, y, 0);
365 } else if(tile->getAttributes() & Tile::BRICK) {
366 add_object(new Brick(pos, tile->getData()));
367 solids->change(x, y, 0);
368 } else if(tile->getAttributes() & Tile::GOAL) {
369 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
370 add_object(new SequenceTrigger(pos, sequence));
371 solids->change(x, y, 0);
378 Sector::write(lisp::Writer& writer)
380 writer.write_string("name", name);
381 writer.write_float("gravity", gravity);
382 writer.write_string("music", music);
385 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
387 SpawnPoint* spawn = *i;
388 writer.start_list("spawn-points");
389 writer.write_string("name", spawn->name);
390 writer.write_float("x", spawn->pos.x);
391 writer.write_float("y", spawn->pos.y);
392 writer.end_list("spawn-points");
396 for(GameObjects::iterator i = gameobjects.begin();
397 i != gameobjects.end(); ++i) {
398 Serializable* serializable = dynamic_cast<Serializable*> (*i);
400 serializable->write(writer);
405 Sector::run_script(std::istream& in, const std::string& sourcename)
407 using namespace Scripting;
409 // garbage collect thread list
410 for(ScriptList::iterator i = scripts.begin();
411 i != scripts.end(); ) {
412 HSQOBJECT& object = *i;
413 HSQUIRRELVM vm = object_to_vm(object);
415 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
416 sq_release(global_vm, &object);
417 i = scripts.erase(i);
424 HSQOBJECT object = create_thread(global_vm);
425 scripts.push_back(object);
427 HSQUIRRELVM vm = object_to_vm(object);
429 // set sector_table as roottable for the thread
430 sq_pushobject(vm, sector_table);
433 compile_and_run(vm, in, sourcename);
439 Sector::add_object(GameObject* object)
441 // make sure the object isn't already in the list
443 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
446 assert("object already added to sector" == 0);
449 for(GameObjects::iterator i = gameobjects_new.begin();
450 i != gameobjects_new.end(); ++i) {
452 assert("object already added to sector" == 0);
457 gameobjects_new.push_back(object);
461 Sector::activate(const std::string& spawnpoint)
464 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
466 if((*i)->name == spawnpoint) {
472 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
473 if(spawnpoint != "main") {
476 activate(Vector(0, 0));
484 Sector::activate(const Vector& player_pos)
486 if(_current != this) {
488 _current->deactivate();
491 // register sectortable as sector in scripting
492 HSQUIRRELVM vm = Scripting::global_vm;
493 sq_pushroottable(vm);
494 sq_pushstring(vm, "sector", -1);
495 sq_pushobject(vm, sector_table);
496 if(SQ_FAILED(sq_createslot(vm, -3)))
497 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
500 for(GameObjects::iterator i = gameobjects.begin();
501 i != gameobjects.end(); ++i) {
502 GameObject* object = *i;
508 player->move(player_pos);
509 camera->reset(player->get_pos());
510 update_game_objects();
513 if(init_script != "") {
514 std::istringstream in(init_script);
515 run_script(in, std::string("Sector(") + name + ") - init");
525 // remove sector entry from global vm
526 HSQUIRRELVM vm = Scripting::global_vm;
527 sq_pushroottable(vm);
528 sq_pushstring(vm, "sector", -1);
529 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
530 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
533 for(GameObjects::iterator i = gameobjects.begin();
534 i != gameobjects.end(); ++i) {
535 GameObject* object = *i;
537 try_unexpose(object);
544 Sector::get_active_region()
547 camera->get_translation() - Vector(1600, 1200),
548 camera->get_translation() + Vector(1600, 1200));
552 Sector::update(float elapsed_time)
554 player->check_bounds(camera);
557 CollisionGridIterator iter(*grid, get_active_region());
558 while(MovingObject* object = iter.next()) {
559 if(!object->is_valid())
562 object->update(elapsed_time);
566 for(GameObjects::iterator i = gameobjects.begin();
567 i != gameobjects.end(); ++i) {
568 GameObject* object = *i;
569 if(!object->is_valid())
572 object->update(elapsed_time);
576 /* Handle all possible collisions. */
578 update_game_objects();
582 Sector::update_game_objects()
584 /** cleanup marked objects */
585 for(std::vector<Bullet*>::iterator i = bullets.begin();
586 i != bullets.end(); /* nothing */) {
588 if(bullet->is_valid()) {
593 i = bullets.erase(i);
595 for(MovingObjects::iterator i = moving_objects.begin();
596 i != moving_objects.end(); /* nothing */) {
597 MovingObject* moving_object = *i;
598 if(moving_object->is_valid()) {
604 grid->remove_object(moving_object);
607 i = moving_objects.erase(i);
609 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
610 i != gameobjects.end(); /* nothing */) {
611 GameObject* object = *i;
613 if(object->is_valid()) {
618 before_object_remove(object);
621 i = gameobjects.erase(i);
624 /* add newly created objects */
625 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
626 i != gameobjects_new.end(); ++i)
628 GameObject* object = *i;
630 before_object_add(object);
632 gameobjects.push_back(object);
634 gameobjects_new.clear();
638 Sector::before_object_add(GameObject* object)
640 Bullet* bullet = dynamic_cast<Bullet*> (object);
642 bullets.push_back(bullet);
644 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
646 moving_objects.push_back(movingobject);
648 grid->add_object(movingobject);
652 TileMap* tilemap = dynamic_cast<TileMap*> (object);
653 if(tilemap && tilemap->is_solid()) {
657 log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
661 Camera* camera = dynamic_cast<Camera*> (object);
663 if(this->camera != 0) {
664 log_warning << "Multiple cameras added. Ignoring" << std::endl;
667 this->camera = camera;
670 Player* player = dynamic_cast<Player*> (object);
672 if(this->player != 0) {
673 log_warning << "Multiple players added. Ignoring" << std::endl;
676 this->player = player;
679 if(_current == this) {
687 Sector::try_expose(GameObject* object)
689 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
690 if(interface != NULL) {
691 HSQUIRRELVM vm = Scripting::global_vm;
692 sq_pushobject(vm, sector_table);
693 interface->expose(vm, -1);
699 Sector::before_object_remove(GameObject* object)
702 try_unexpose(object);
706 Sector::try_unexpose(GameObject* object)
708 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
709 if(interface != NULL) {
710 HSQUIRRELVM vm = Scripting::global_vm;
711 int oldtop = sq_gettop(vm);
712 sq_pushobject(vm, sector_table);
714 interface->unexpose(vm, -1);
715 } catch(std::exception& e) {
716 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
718 sq_settop(vm, oldtop);
723 Sector::draw(DrawingContext& context)
725 context.push_transform();
726 context.set_translation(camera->get_translation());
728 for(GameObjects::iterator i = gameobjects.begin();
729 i != gameobjects.end(); ++i) {
730 GameObject* object = *i;
731 if(!object->is_valid())
734 if (draw_solids_only)
736 TileMap* tm = dynamic_cast<TileMap*>(object);
737 if (tm && !tm->is_solid())
741 object->draw(context);
745 Color col(0.2, 0.2, 0.2, 0.7);
746 for(MovingObjects::iterator i = moving_objects.begin();
747 i != moving_objects.end(); ++i) {
748 MovingObject* object = *i;
749 const Rect& rect = object->get_bbox();
751 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
755 context.pop_transform();
758 static const float DELTA = .001;
761 Sector::collision_tilemap(const Rect& dest, const Vector& movement,
762 CollisionHit& hit) const
764 // calculate rectangle where the object will move
765 float x1 = dest.get_left();
766 float x2 = dest.get_right();
767 float y1 = dest.get_top();
768 float y2 = dest.get_bottom();
770 // test with all tiles in this rectangle
771 int starttilex = int(x1) / 32;
772 int starttiley = int(y1) / 32;
773 int max_x = int(x2 + (1 - DELTA));
774 int max_y = int(y2 + (1 - DELTA));
776 CollisionHit temphit;
777 for(int x = starttilex; x*32 < max_x; ++x) {
778 for(int y = starttiley; y*32 < max_y; ++y) {
779 const Tile* tile = solids->get_tile(x, y);
782 // skip non-solid tiles
783 if(tile->getAttributes() == 0)
785 // only handle unisolid when the player is falling down and when he was
786 // above the tile before
787 if(tile->getAttributes() & Tile::UNISOLID) {
788 if(movement.y < 0 || dest.get_top() - movement.y > y*32)
792 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
794 Vector p1(x*32, y*32);
795 Vector p2((x+1)*32, (y+1)*32);
796 triangle = AATriangle(p1, p2, tile->getData());
798 if(Collision::rectangle_aatriangle(temphit, dest, movement,
800 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
804 } else { // normal rectangular tile
805 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
806 if(Collision::rectangle_rectangle(temphit, dest, movement, rect)) {
807 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
817 Sector::collision_tile_attributes(const Rect& dest) const
819 /** XXX This function doesn't work correctly as it will check all tiles
820 * in the bounding box of the object movement, this might include tiles
821 * that have actually never been touched by the object
822 * (though this only occures for very fast objects...)
826 // calculate rectangle where the object will move
828 if(object->get_movement().x >= 0) {
829 x1 = object->get_bbox().p1.x;
830 x2 = object->get_bbox().p2.x + object->get_movement().x;
832 x1 = object->get_bbox().p1.x + object->get_movement().x;
833 x2 = object->get_bbox().p2.x;
836 if(object->get_movement().y >= 0) {
837 y1 = object->get_bbox().p1.y;
838 y2 = object->get_bbox().p2.y + object->get_movement().y;
840 y1 = object->get_bbox().p1.y + object->get_movement().y;
841 y2 = object->get_bbox().p2.y;
844 float x1 = dest.p1.x;
845 float y1 = dest.p1.y;
846 float x2 = dest.p2.x;
847 float y2 = dest.p2.y;
849 // test with all tiles in this rectangle
850 int starttilex = int(x1) / 32;
851 int starttiley = int(y1) / 32;
856 for(int x = starttilex; x*32 < max_x; ++x) {
857 for(int y = starttiley; y*32 < max_y; ++y) {
858 const Tile* tile = solids->get_tile(x, y);
861 result |= tile->getAttributes();
869 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
873 Vector movement = object1->get_movement() - object2->get_movement();
874 if(Collision::rectangle_rectangle(hit, object1->dest, movement, object2->dest)) {
875 HitResponse response1 = object1->collision(*object2, hit);
877 HitResponse response2 = object2->collision(*object1, hit);
879 if(response1 != CONTINUE) {
880 if(response1 == ABORT_MOVE)
881 object1->dest = object1->get_bbox();
882 if(response2 == CONTINUE)
883 object2->dest.move(hit.normal * (hit.depth + DELTA));
884 } else if(response2 != CONTINUE) {
885 if(response2 == ABORT_MOVE)
886 object2->dest = object2->get_bbox();
887 if(response1 == CONTINUE)
888 object1->dest.move(-hit.normal * (hit.depth + DELTA));
890 object1->dest.move(-hit.normal * (hit.depth/2 + DELTA));
891 object2->dest.move(hit.normal * (hit.depth/2 + DELTA));
897 Sector::collision_static(MovingObject* object, const Vector& movement)
899 GameObject* collided_with = solids;
903 collision_tilemap(object->dest, movement, hit);
905 // collision with other (static) objects
906 CollisionHit temphit;
907 for(MovingObjects::iterator i2 = moving_objects.begin();
908 i2 != moving_objects.end(); ++i2) {
909 MovingObject* moving_object_2 = *i2;
910 if(moving_object_2->get_group() != COLGROUP_STATIC
911 || !moving_object_2->is_valid())
914 Rect dest = moving_object_2->dest;
917 = movement - moving_object_2->get_movement();
919 if(Collision::rectangle_rectangle(temphit, object->dest, rel_movement, dest)
920 && temphit.time > hit.time) {
922 collided_with = moving_object_2;
929 HitResponse response = object->collision(*collided_with, hit);
931 if(collided_with != solids) {
932 MovingObject* moving_object = (MovingObject*) collided_with;
933 HitResponse other_response = moving_object->collision(*object, hit);
934 if(other_response == ABORT_MOVE) {
935 moving_object->dest = moving_object->get_bbox();
936 } else if(other_response == FORCE_MOVE) {
937 // the static object "wins" move tux out of the collision
938 object->dest.move(-hit.normal * (hit.depth + DELTA));
940 } else if(other_response == PASS_MOVEMENT) {
941 object->dest.move(moving_object->get_movement());
942 //object->movement += moving_object->get_movement();
946 if(response == CONTINUE) {
947 object->dest.move(-hit.normal * (hit.depth + DELTA));
949 } else if(response == ABORT_MOVE) {
950 object->dest = object->get_bbox();
959 Sector::handle_collisions()
961 // calculate destination positions of the objects
962 for(MovingObjects::iterator i = moving_objects.begin();
963 i != moving_objects.end(); ++i) {
964 MovingObject* moving_object = *i;
966 moving_object->dest = moving_object->get_bbox();
967 moving_object->dest.move(moving_object->get_movement());
970 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
971 // we do this up to 4 times and have to sort all results for the smallest
972 // one before we can continue here
973 for(MovingObjects::iterator i = moving_objects.begin();
974 i != moving_objects.end(); ++i) {
975 MovingObject* moving_object = *i;
976 if((moving_object->get_group() != COLGROUP_MOVING
977 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
978 || !moving_object->is_valid())
981 Vector movement = moving_object->get_movement();
983 // test if x or y movement is dominant
984 if(fabsf(moving_object->get_movement().x) < fabsf(moving_object->get_movement().y)) {
986 // test in x direction first, then y direction
987 moving_object->dest.move(Vector(0, -movement.y));
988 for(int i = 0; i < 2; ++i) {
989 bool res = collision_static(moving_object, Vector(movement.x, 0));
993 moving_object->dest.move(Vector(0, movement.y));
994 for(int i = 0; i < 2; ++i) {
995 bool res = collision_static(moving_object, Vector(0, movement.y));
1002 // test in y direction first, then x direction
1003 moving_object->dest.move(Vector(-movement.x, 0));
1004 for(int i = 0; i < 2; ++i) {
1005 bool res = collision_static(moving_object, Vector(0, movement.y));
1009 moving_object->dest.move(Vector(movement.x, 0));
1010 for(int i = 0; i < 2; ++i) {
1011 bool res = collision_static(moving_object, Vector(movement.x, 0));
1018 // part2: COLGROUP_MOVING vs tile attributes
1019 for(MovingObjects::iterator i = moving_objects.begin();
1020 i != moving_objects.end(); ++i) {
1021 MovingObject* moving_object = *i;
1022 if((moving_object->get_group() != COLGROUP_MOVING
1023 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1024 || !moving_object->is_valid())
1027 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1028 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1029 moving_object->collision_tile(tile_attributes);
1033 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1034 for(MovingObjects::iterator i = moving_objects.begin();
1035 i != moving_objects.end(); ++i) {
1036 MovingObject* moving_object = *i;
1037 if(moving_object->get_group() != COLGROUP_MOVING
1038 || !moving_object->is_valid())
1041 for(MovingObjects::iterator i2 = moving_objects.begin();
1042 i2 != moving_objects.end(); ++i2) {
1043 MovingObject* moving_object_2 = *i2;
1044 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1045 || !moving_object_2->is_valid())
1048 collision_object(moving_object, moving_object_2);
1052 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1053 for(MovingObjects::iterator i = moving_objects.begin();
1054 i != moving_objects.end(); ++i) {
1055 MovingObject* moving_object = *i;
1057 if(moving_object->get_group() != COLGROUP_MOVING
1058 || !moving_object->is_valid())
1061 for(MovingObjects::iterator i2 = i+1;
1062 i2 != moving_objects.end(); ++i2) {
1063 MovingObject* moving_object_2 = *i2;
1064 if(moving_object_2->get_group() != COLGROUP_MOVING
1065 || !moving_object_2->is_valid())
1068 collision_object(moving_object, moving_object_2);
1072 // apply object movement
1073 for(MovingObjects::iterator i = moving_objects.begin();
1074 i != moving_objects.end(); ++i) {
1075 MovingObject* moving_object = *i;
1077 moving_object->bbox = moving_object->dest;
1078 moving_object->movement = Vector(0, 0);
1083 Sector::is_free_space(const Rect& rect) const
1085 // test with all tiles in this rectangle
1086 int starttilex = int(rect.p1.x) / 32;
1087 int starttiley = int(rect.p1.y) / 32;
1088 int max_x = int(rect.p2.x);
1089 int max_y = int(rect.p2.y);
1091 for(int x = starttilex; x*32 <= max_x; ++x) {
1092 for(int y = starttiley; y*32 <= max_y; ++y) {
1093 const Tile* tile = solids->get_tile(x, y);
1096 if(tile->getAttributes() & Tile::SOLID)
1101 for(MovingObjects::const_iterator i = moving_objects.begin();
1102 i != moving_objects.end(); ++i) {
1103 const MovingObject* moving_object = *i;
1104 if(moving_object->get_group() != COLGROUP_STATIC
1105 || !moving_object->is_valid())
1108 if(Collision::intersects(rect, moving_object->get_bbox()))
1116 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1118 // TODO remove this function and move these checks elsewhere...
1120 Bullet* new_bullet = 0;
1121 if(player_status->bonus == FIRE_BONUS) {
1122 if((int)bullets.size() >= player_status->max_fire_bullets)
1124 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
1125 } else if(player_status->bonus == ICE_BONUS) {
1126 if((int)bullets.size() >= player_status->max_ice_bullets)
1128 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
1132 add_object(new_bullet);
1134 sound_manager->play("sounds/shoot.wav");
1140 Sector::add_smoke_cloud(const Vector& pos)
1142 add_object(new SmokeCloud(pos));
1147 Sector::add_floating_text(const Vector& pos, const std::string& text)
1149 add_object(new FloatingText(pos, text));
1153 Sector::play_music(MusicType type)
1155 currentmusic = type;
1156 switch(currentmusic) {
1158 sound_manager->play_music(music);
1161 sound_manager->play_music("music/salcon.ogg");
1163 case HERRING_WARNING_MUSIC:
1164 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1167 sound_manager->play_music("");
1173 Sector::get_music_type()
1175 return currentmusic;
1179 Sector::get_total_badguys()
1181 int total_badguys = 0;
1182 for(GameObjects::iterator i = gameobjects.begin();
1183 i != gameobjects.end(); ++i) {
1184 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1185 if (badguy && badguy->countMe)
1189 return total_badguys;
1193 Sector::inside(const Rect& rect) const
1195 if(rect.p1.x > solids->get_width() * 32
1196 || rect.p1.y > solids->get_height() * 32