replaced distros with coins
[supertux.git] / src / gameloop.cpp
index 229e8fd..5c33406 100644 (file)
 #include "tile.h"
 #include "particlesystem.h"
 #include "resources.h"
+#include "music_manager.h"
 
 GameSession* GameSession::current_ = 0;
 
 GameSession::GameSession(const std::string& subset_, int levelnb_, int mode)
-  : world(0), st_gl_mode(mode), levelnb(levelnb_), subset(subset_)
+  : world(0), st_gl_mode(mode), levelnb(levelnb_), end_sequenze(false),
+    subset(subset_)
 {
   current_ = this;
-  end_sequenze = false;
+  
+  global_frame_counter = 0;
+  game_pause = false;
+
+  fps_timer.init(true);            
+  frame_timer.init(true);
+
   restart_level();
 }
 
@@ -122,7 +130,8 @@ GameSession::restart_level()
     }
 
   time_left.init(true);
-  start_timers(); 
+  start_timers();
+  world->play_music(LEVEL_MUSIC);
 }
 
 GameSession::~GameSession()
@@ -133,6 +142,8 @@ GameSession::~GameSession()
 void
 GameSession::levelintro(void)
 {
+  music_manager->halt_music();
+  
   char str[60];
   /* Level Intro: */
   clearscreen(0, 0, 0);
@@ -165,6 +176,9 @@ GameSession::start_timers()
 void
 GameSession::on_escape_press()
 {
+  if(game_pause)
+    return;
+
   if(st_gl_mode == ST_GL_TEST)
     {
       exit_status = LEVEL_ABORT;
@@ -195,6 +209,9 @@ GameSession::process_events()
     }
   else
     {
+      if(!Menu::current() && !game_pause)
+        st_pause_ticks_stop();
+
       SDL_Event event;
       while (SDL_PollEvent(&event))
         {
@@ -208,8 +225,6 @@ GameSession::process_events()
             {
               Player& tux = *world->get_tux();
   
-              st_pause_ticks_stop();
-
               switch(event.type)
                 {
                 case SDL_QUIT:        /* Quit event - quit: */
@@ -368,7 +383,7 @@ GameSession::check_end_conditions()
     {
       end_sequenze = true;
       last_x_pos = -1;
-      halt_music();
+      music_manager->halt_music();
     }
   else
     {
@@ -442,10 +457,10 @@ GameSession::process_menu()
         {
           switch (game_menu->check())
             {
-            case 2:
+            case MNID_CONTINUE:
               st_pause_ticks_stop();
               break;
-            case 5:
+            case MNID_ABORTLEVEL:
               st_pause_ticks_stop();
               exit_status = LEVEL_ABORT;
               break;
@@ -466,27 +481,16 @@ GameSession::ExitStatus
 GameSession::run()
 {
   Menu::set_current(0);
-  Player* tux = world->get_tux();
   current_ = this;
   
-  int  fps_cnt;
+  int fps_cnt = 0;
 
-  global_frame_counter = 0;
-  game_pause = false;
-
-  fps_timer.init(true);
-  frame_timer.init(true);
-
-  last_update_time = st_get_ticks();
-  fps_cnt = 0;
+  update_time = last_update_time = st_get_ticks();
 
   /* Clear screen: */
   clearscreen(0, 0, 0);
   updatescreen();
 
-  /* Play music: */
-  play_current_music();
-
   // Eat unneeded events
   SDL_Event event;
   while (SDL_PollEvent(&event)) {}
@@ -506,7 +510,7 @@ GameSession::run()
         }
 
       /* Handle events: */
-      tux->input.old_fire = tux->input.fire;
+      world->get_tux()->input.old_fire = world->get_tux()->input.fire;
 
       process_events();
       process_menu();
@@ -557,19 +561,25 @@ GameSession::run()
         }
 
       /* Handle time: */
-      if (time_left.check())
+      if (!time_left.check() && world->get_tux()->dying == DYING_NOT)
+        world->get_tux()->kill(Player::KILL);
+
+      /* Handle music: */
+      if(world->get_tux()->invincible_timer.check())
         {
-          /* are we low on time ? */
-          if (time_left.get_left() < TIME_WARNING
-              && (get_current_music() != HURRYUP_MUSIC)) /* play the fast music */
-            {
-              set_current_music(HURRYUP_MUSIC);
-              play_current_music();
-            }
+          if(world->get_music_type() != HERRING_MUSIC)
+            world->play_music(HERRING_MUSIC);
+        }
+      /* are we low on time ? */
+      else if (time_left.get_left() < TIME_WARNING
+         && (world->get_music_type() == LEVEL_MUSIC))
+        {
+          world->play_music(HURRYUP_MUSIC);
         }
-      else if(tux->dying == DYING_NOT)
+      /* or just normal music? */
+      else if(world->get_music_type() != LEVEL_MUSIC)
         {
-          tux->kill(KILL);
+          world->play_music(LEVEL_MUSIC);
         }
 
       /* Calculate frames per second */
@@ -586,11 +596,8 @@ GameSession::run()
         }
     }
   
-  halt_music();
-
-  world->get_level()->free_gfx();
-  world->get_level()->cleanup();
-  world->get_level()->free_song();
+  delete world;
+  world = 0;
 
   return exit_status;
 }
@@ -627,7 +634,7 @@ GameSession::drawstatus()
     }
 
   sprintf(str, "%d", player_status.distros);
-  white_text->draw("DISTROS", screen->h, 0, 1);
+  white_text->draw("COINS", screen->h, 0, 1);
   gold_text->draw(str, 608, 0, 1);
 
   white_text->draw("LIVES", screen->h, 20, 1);
@@ -678,7 +685,7 @@ GameSession::drawresultscreen(void)
   sprintf(str, "SCORE: %d", player_status.score);
   gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1);
 
-  sprintf(str, "DISTROS: %d", player_status.distros);
+  sprintf(str, "COINS: %d", player_status.distros);
   gold_text->drawf(str, 0, 256, A_HMIDDLE, A_TOP, 1);
 
   flipscreen();