Fix endsequence walking direction
[supertux.git] / src / game_session.cpp
index c4ee383..2abb68c 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "game_session.hpp"
 #include "log.hpp"
+#include "console.hpp"
 #include "worldmap/worldmap.hpp"
 #include "mainloop.hpp"
 #include "audio/sound_manager.hpp"
@@ -55,7 +56,6 @@
 #include "statistics.hpp"
 #include "timer.hpp"
 #include "options_menu.hpp"
-#include "object/fireworks.hpp"
 #include "textscroller.hpp"
 #include "control/codecontroller.hpp"
 #include "control/joystickkeyboardcontroller.hpp"
@@ -69,6 +69,9 @@
 #include "trigger/sequence_trigger.hpp"
 #include "random_generator.hpp"
 #include "scripting/squirrel_util.hpp"
+#include "object/endsequence_walkright.hpp"
+#include "object/endsequence_walkleft.hpp"
+#include "object/endsequence_fireworks.hpp"
 #include "direction.hpp"
 
 // the engine will be run with a logical framerate of 64fps.
@@ -97,7 +100,7 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
 
   statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png"));
 
-  restart_level(true);
+  restart_level();
 
   game_menu.reset(new Menu());
   game_menu->add_label(_("Pause"));
@@ -109,7 +112,7 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
 }
 
 void
-GameSession::restart_level(bool fromBeginning)
+GameSession::restart_level()
 {
   game_pause   = false;
   end_sequence = 0;
@@ -124,9 +127,8 @@ GameSession::restart_level(bool fromBeginning)
   level->stats.total_badguys = level->get_total_badguys();
   level->stats.total_secrets = level->get_total_count<SecretAreaTrigger>();
   level->stats.reset();
-  if (!fromBeginning && (reset_sector != "")) level->stats.declare_invalid();
+  if(reset_sector != "")level->stats.declare_invalid();
 
-  if (fromBeginning) reset_sector="";
   if(reset_sector != "") {
     currentsector = level->get_sector(reset_sector);
     if(!currentsector) {
@@ -380,18 +382,8 @@ GameSession::check_end_conditions()
   /* End of level? */
   if(end_sequence && end_sequence->is_done()) {
     finish(true);
-    return;
   } else if (!end_sequence && tux->is_dead()) {
-    if (player_status->coins < 0) {
-      // No more coins: restart level from beginning
-      player_status->coins = 0;
-      restart_level(true);
-    } else {
-      // Still has coins: restart level from last reset point
-      restart_level(false);
-    }
-
-    return;
+    restart_level();
   }
 }
 
@@ -579,50 +571,65 @@ GameSession::display_info_box(const std::string& text)
 void
 GameSession::start_sequence(const std::string& sequencename)
 {
-  if(sequencename == "endsequence" || sequencename == "fireworks") {
-    if(end_sequence)
-      return;
+  // handle special "stoptux" sequence
+  if (sequencename == "stoptux") {
+    if (!end_sequence) {
+      log_warning << "Final target reached without an active end sequence" << std::endl;
+      this->start_sequence("endsequence");
+    }
+    if (end_sequence) end_sequence->stop_tux();
+    return;
+  }
+
+  // abort if a sequence is already playing
+  if (end_sequence) return;
+
+  if (sequencename == "endsequence") {
 
     // Determine walking direction for Tux
-    float xst = 1.f, xend = 2.f;
+    /*float xst = 1.f, xend = 2.f;
     for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin(); i != currentsector->gameobjects.end(); i++) {
       SequenceTrigger* st = dynamic_cast<SequenceTrigger*>(*i);
       if(!st)
-        continue;
+       continue;
       if(st->get_sequence_name() == "stoptux")
-        xend = st->get_pos().x;
+       xend = st->get_pos().x;
       else if(st->get_sequence_name() == "endsequence")
-        xst = st->get_pos().y;
-    }
-    end_sequence = new EndSequence();
-    currentsector->add_object(end_sequence);
-    end_sequence->start((xst > xend) ? LEFT : RIGHT);
-
-    sound_manager->play_music("music/leveldone.ogg", false);
-    currentsector->player->invincible_timer.start(7.3f);
-
-    // Stop all clocks.
-    for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
-        i != currentsector->gameobjects.end(); ++i)
-    {
-      GameObject* obj = *i;
-
-      LevelTime* lt = dynamic_cast<LevelTime*> (obj);
-      if(lt)
-        lt->stop();
+       xst = st->get_pos().y;
     }
 
-    if(sequencename == "fireworks") {
-      currentsector->add_object(new Fireworks());
-    }
-  } else if(sequencename == "stoptux") {
-    if(!end_sequence) {
-      log_warning << "Final target reached without an active end sequence" << std::endl;
-      this->start_sequence("endsequence");
+    if (xst > xend) {
+      end_sequence = new EndSequenceWalkLeft();
+    } else {
+      end_sequence = new EndSequenceWalkRight();
+    }*/
+    if (currentsector->get_players()[0]->physic.get_velocity_x() < 0) {
+      end_sequence = new EndSequenceWalkLeft();
+    } else {
+      end_sequence = new EndSequenceWalkRight();
     }
-    if (end_sequence) end_sequence->stop_tux();
-  } else {
-    log_warning << "Unknown sequence '" << sequencename << "'" << std::endl;
+  }
+  else if (sequencename == "fireworks") end_sequence = new EndSequenceFireworks();
+  else {
+    log_warning << "Unknown sequence '" << sequencename << "'. Ignoring." << std::endl;
+    return;
+  }
+
+  currentsector->add_object(end_sequence);
+  end_sequence->start();
+
+  sound_manager->play_music("music/leveldone.ogg", false);
+  currentsector->player->invincible_timer.start(10000.0f);
+
+  // Stop all clocks.
+  for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
+                 i != currentsector->gameobjects.end(); ++i)
+  {
+    GameObject* obj = *i;
+
+    LevelTime* lt = dynamic_cast<LevelTime*> (obj);
+    if(lt)
+      lt->stop();
   }
 }