- fix a bunch of msvc warnings (mostly assigning double constants to float variables)
[supertux.git] / src / game_session.cpp
index c5729eb..c1ae83c 100644 (file)
@@ -83,7 +83,7 @@ GameSession* GameSession::current_ = NULL;
 
 GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
   : level(0), currentsector(0),
-    end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
+    end_sequence(0),
     levelfile(levelfile_), best_level_statistics(statistics),
     capture_demo_stream(0), playback_demo_stream(0), demo_controller(0),
     play_time(0)
@@ -110,7 +110,7 @@ void
 GameSession::restart_level(bool fromBeginning)
 {
   game_pause   = false;
-  end_sequence = NO_ENDSEQUENCE;
+  end_sequence = 0;
 
   main_controller->reset();
 
@@ -122,7 +122,7 @@ 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) level->stats.declare_invalid();
+  if (!fromBeginning && (reset_sector != "")) level->stats.declare_invalid();
 
   if (fromBeginning) reset_sector="";
   if(reset_sector != "") {
@@ -160,8 +160,6 @@ GameSession::~GameSession()
   delete playback_demo_stream;
   delete demo_controller;
 
-  delete end_sequence_controller;
-
   current_ = NULL;
 }
 
@@ -271,8 +269,13 @@ GameSession::levelintro()
 void
 GameSession::on_escape_press()
 {
-  if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
+  if(currentsector->player->is_dying() || end_sequence)
+  {
+    // Let the timers run out, we fast-forward them to force past a sequence
+    if (end_sequence) end_sequence->stop();
+    currentsector->player->dying_timer.start(FLT_EPSILON);
     return;   // don't let the player open the menu, when he is dying
+  }
 
   if(level->on_menukey_script != "") {
     std::istringstream in(level->on_menukey_script);
@@ -328,26 +331,11 @@ GameSession::run_script(std::istream& in, const std::string& sourcename)
 void
 GameSession::process_events()
 {
-  Player& tux = *currentsector->player;
-
   // end of pause mode?
   if(!Menu::current() && game_pause) {
     game_pause = false;
   }
 
-  if (end_sequence != NO_ENDSEQUENCE) {
-    if(end_sequence_controller == 0) {
-      end_sequence_controller = new CodeController();
-      tux.set_controller(end_sequence_controller);
-    }
-
-    end_sequence_controller->press(Controller::RIGHT);
-
-    if (int(last_x_pos) == int(tux.get_pos().x))
-      end_sequence_controller->press(Controller::JUMP);
-    last_x_pos = tux.get_pos().x;
-  }
-
   // playback a demo?
   if(playback_demo_stream != 0) {
     demo_controller->update();
@@ -388,7 +376,7 @@ GameSession::check_end_conditions()
   Player* tux = currentsector->player;
 
   /* End of level? */
-  if(end_sequence && endsequence_timer.check()) {
+  if(end_sequence && end_sequence->is_done()) {
     finish(true);
     return;
   } else if (!end_sequence && tux->is_dead()) {
@@ -420,7 +408,7 @@ GameSession::draw_pause(DrawingContext& context)
 {
   context.draw_filled_rect(
       Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
-      Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
+      Color(.2f, .2f, .2f, .5f), LAYER_FOREGROUND1);
 }
 
 void
@@ -484,12 +472,16 @@ GameSession::update(float elapsed_time)
   // Update the world state and all objects in the world
   if(!game_pause) {
     // Update the world
-    if (end_sequence == ENDSEQUENCE_RUNNING) {
-      currentsector->update(elapsed_time/2);
-    } else if(end_sequence == NO_ENDSEQUENCE) {
+    if (!end_sequence) {
       play_time += elapsed_time; //TODO: make sure we don't count cutscene time
       level->stats.time = play_time;
       currentsector->update(elapsed_time);
+    } else {
+      if (!end_sequence->is_tux_stopped()) {
+        currentsector->update(elapsed_time/2);
+      } else {
+        end_sequence->update(elapsed_time/2);
+      }
     }
   }
 
@@ -589,11 +581,12 @@ GameSession::start_sequence(const std::string& sequencename)
     if(end_sequence)
       return;
 
-    end_sequence = ENDSEQUENCE_RUNNING;
-    endsequence_timer.start(7.3);
-    last_x_pos = -1;
+    end_sequence = new EndSequence();
+    currentsector->add_object(end_sequence);
+    end_sequence->start();
+
     sound_manager->play_music("music/leveldone.ogg", false);
-    currentsector->player->invincible_timer.start(7.3);
+    currentsector->player->invincible_timer.start(7.3f);
 
     // Stop all clocks.
     for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
@@ -614,7 +607,7 @@ GameSession::start_sequence(const std::string& sequencename)
       log_warning << "Final target reached without an active end sequence" << std::endl;
       this->start_sequence("endsequence");
     }
-    end_sequence =  ENDSEQUENCE_WAITING;
+    if (end_sequence) end_sequence->stop_tux();
   } else {
     log_warning << "Unknown sequence '" << sequencename << "'" << std::endl;
   }