small fix to MrRocket
[supertux.git] / src / gameloop.cpp
index f6d92d9..642a436 100644 (file)
@@ -62,6 +62,7 @@
 #include "statistics.h"
 #include "timer.h"
 #include "object/fireworks.h"
+#include "textscroller.h"
 
 GameSession* GameSession::current_ = 0;
 
@@ -91,9 +92,6 @@ GameSession::GameSession(const std::string& levelfile_, int mode,
 
   context = new DrawingContext();
 
-  last_swap_point = Vector(-1, -1);
-  last_swap_stats.reset();
-
   restart_level();
 }
 
@@ -106,15 +104,6 @@ GameSession::restart_level()
 
   last_keys.clear();
 
-#if 0
-  Vector tux_pos = Vector(-1,-1);
-  if (currentsector)
-    { 
-      // Tux has lost a life, so we try to respawn him at the nearest reset point
-      tux_pos = currentsector->player->base;
-    }
-#endif
-  
   delete level;
   currentsector = 0;
 
@@ -126,40 +115,23 @@ GameSession::restart_level()
   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
 
-  currentsector = level->get_sector("main");
-  if(!currentsector)
-    Termination::abort("Level has no main sector.", "");
-  currentsector->activate("main");
-
-#if 0
-  // Set Tux to the nearest reset point
-  if(tux_pos.x != -1)
-    {
-    tux_pos = currentsector->get_best_spawn_point(tux_pos);
-
-    if(last_swap_point.x > tux_pos.x)
-      tux_pos = last_swap_point;
-    else  // new swap point
-      {
-      last_swap_point = tux_pos;
-
-      last_swap_stats += global_stats;
-      }
-
-    currentsector->player->base.x = tux_pos.x;
-    currentsector->player->base.y = tux_pos.y;
-
-    // has to reset camera on swapping
-    currentsector->camera->reset(Vector(currentsector->player->base.x,
-                                        currentsector->player->base.y));
-    }
-#endif
-
-  if (st_gl_mode != ST_GL_DEMO_GAME)
-    {
-      if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
-        levelintro();
+  if(reset_sector != "") {
+    currentsector = level->get_sector(reset_sector);
+    if(!currentsector) {
+      std::stringstream msg;
+      msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
+      throw std::runtime_error(msg.str());
     }
+    currentsector->activate(reset_pos);
+  } else {
+    currentsector = level->get_sector("main");
+    if(!currentsector)
+      throw std::runtime_error("Couldn't find main sector");
+    currentsector->activate("main");
+  }
+  
+  if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
+    levelintro();
 
   start_timers();
   currentsector->play_music(LEVEL_MUSIC);
@@ -172,7 +144,7 @@ GameSession::~GameSession()
 }
 
 void
-GameSession::levelintro(void)
+GameSession::levelintro()
 {
   SoundManager::get()->halt_music();
   
@@ -383,13 +355,13 @@ GameSession::process_events()
                       }
                   }
 
-                        /* Check if chacrater is ASCII */
-                        char ch[2];
-                        if((event.key.keysym.unicode & 0xFF80) == 0)
-                          {
-                          ch[0] = event.key.keysym.unicode & 0x7F;
-                          ch[1] = '\0';
-                          }
+                  /* Check if chacrater is ASCII */
+                  char ch[2];
+                  if((event.key.keysym.unicode & 0xFF80) == 0)
+                  {
+                      ch[0] = event.key.keysym.unicode & 0x7F;
+                      ch[1] = '\0';
+                  }
                         last_keys.append(ch);  // add to cheat keys
                         handle_cheats();
                   break;
@@ -486,17 +458,15 @@ GameSession::handle_cheats()
   // Cheating words (the goal of this is really for debugging,
   // but could be used for some cheating, nothing wrong with that)
   if(compare_last(last_keys, "grow")) {
-    tux.grow(false);
+    tux.set_bonus(GROWUP_BONUS, false);
     last_keys.clear();
   }
   if(compare_last(last_keys, "fire")) {
-    tux.grow(false);
-    tux.got_power = tux.FIRE_POWER;
+    tux.set_bonus(FIRE_BONUS, false);
     last_keys.clear();
   }
   if(compare_last(last_keys, "ice")) {
-    tux.grow(false);
-    tux.got_power = tux.ICE_POWER;
+    tux.set_bonus(ICE_BONUS, false);
     last_keys.clear();
   }
   if(compare_last(last_keys, "lifeup")) {
@@ -572,12 +542,9 @@ GameSession::check_end_conditions()
 
   /* End of level? */
   if(end_sequence && endsequence_timer.check()) {
-      exit_status = ES_LEVEL_FINISHED;
-      global_stats += last_swap_stats;  // add swap points stats
-      return;
+    exit_status = ES_LEVEL_FINISHED;
+    return;
   } else if (!end_sequence && tux->is_dead()) {
-    player_status.bonus = PlayerStatus::NO_BONUS;
-
     if (player_status.lives < 0) { // No more lives!?
       exit_status = ES_GAME_OVER;
     } else { // Still has lives, so reset Tux to the levelstart
@@ -592,19 +559,22 @@ void
 GameSession::action(float elapsed_time)
 {
   // advance timers
-  if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
-    {
-      // Update Tux and the World
-      currentsector->action(elapsed_time);
-    }
+  if(!currentsector->player->growing_timer.started()) {
+    // Update Tux and the World
+    currentsector->action(elapsed_time);
+  }
 
   // respawning in new sector?
   if(newsector != "" && newspawnpoint != "") {
     Sector* sector = level->get_sector(newsector);
+    if(sector == 0) {
+      std::cerr << "Sector '" << newsector << "' not found.\n";
+    }
+    sector->activate(newspawnpoint);
+    sector->play_music(LEVEL_MUSIC);
     currentsector = sector;
-    currentsector->activate(newspawnpoint);
-    currentsector->play_music(LEVEL_MUSIC);
-    newsector = newspawnpoint = "";
+    newsector = "";
+    newspawnpoint = "";
   }
 }
 
@@ -615,46 +585,46 @@ GameSession::draw()
   drawstatus(*context);
 
   if(game_pause)
-    {
-      int x = screen->h / 20;
-      for(int i = 0; i < x; ++i)
-        {
-          context->draw_filled_rect(
-              Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
-                -((pause_menu_frame * i)%screen->w)
-                ,(i*20+pause_menu_frame)%screen->h),
-              Vector(screen->w,10),
-              Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
-        }
-      context->draw_filled_rect(
-          Vector(0,0), Vector(screen->w, screen->h),
-          Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
-      context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
-          Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
-
-      char str1[60];
-      char str2[124];
-      sprintf(str1, _("Playing: "));
-      sprintf(str2, level->name.c_str());
-
-      context->draw_text(blue_text, str1,
-          Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
-          LEFT_ALLIGN, LAYER_FOREGROUND1+2);
-      context->draw_text(white_text, str2,
-          Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
-          LEFT_ALLIGN, LAYER_FOREGROUND1+2);
-    }
+    draw_pause();
 
-  if(Menu::current())
-    {
-      Menu::current()->draw(*context);
-      mouse_cursor->draw(*context);
-    }
+  if(Menu::current()) {
+    Menu::current()->draw(*context);
+    mouse_cursor->draw(*context);
+  }
 
   context->do_drawing();
 }
 
 void
+GameSession::draw_pause()
+{
+  int x = screen->h / 20;
+  for(int i = 0; i < x; ++i) {
+    context->draw_filled_rect(
+        Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
+          -((pause_menu_frame * i)%screen->w)
+          ,(i*20+pause_menu_frame)%screen->h),
+        Vector(screen->w,10),
+        Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
+  }
+  context->draw_filled_rect(
+      Vector(0,0), Vector(screen->w, screen->h),
+      Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
+  context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
+      Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
+
+  const char* str1 = _("Playing: ");
+  const char* str2 = level->get_name().c_str();
+
+  context->draw_text(blue_text, str1,
+      Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
+      LEFT_ALLIGN, LAYER_FOREGROUND1+2);
+  context->draw_text(white_text, str2,
+      Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
+      LEFT_ALLIGN, LAYER_FOREGROUND1+2);
+}
+  
+void
 GameSession::process_menu()
 {
   Menu* menu = Menu::current();
@@ -795,6 +765,36 @@ GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
 }
 
 void
+GameSession::set_reset_point(const std::string& sector, const Vector& pos)
+{
+  reset_sector = sector;
+  reset_pos = pos;
+}
+
+void
+GameSession::display_info_box(const std::string& text)
+{
+  InfoBox* box = new InfoBox(text);
+
+  bool running = true;
+  while(running)  {
+    SDL_Event event;
+    while (SDL_PollEvent(&event)) {
+      switch(event.type) {
+        case SDL_KEYDOWN:
+          running = false;
+          break;
+      }
+    }
+
+    box->draw(*context);
+    draw();
+  }
+
+  delete box;
+}
+
+void
 GameSession::start_sequence(const std::string& sequencename)
 {
   if(sequencename == "endsequence" || sequencename == "fireworks") {