Back to index

extremetuxracer  0.5beta
game_over.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "game_over.h"
00023 
00024 #include "ppgltk/ui_mgr.h"
00025 #include "ppgltk/font.h"
00026 #include "ppgltk/audio/audio.h"
00027 
00028 #include "game_config.h"
00029 #include "gl_util.h"
00030 #include "fps.h"
00031 #include "render_util.h"
00032 #include "phys_sim.h"
00033 #include "view.h"
00034 #include "course_render.h"
00035 #include "model_hndl.h"
00036 #include "tux_shadow.h"
00037 #include "loop.h"
00038 #include "fog.h"
00039 #include "snow.h"
00040 #include "viewfrustum.h"
00041 #include "hud.h"
00042 #include "joystick.h"
00043 #include "part_sys.h"
00044 
00045 #include "stuff.h"
00046 #include "game_mgr.h"
00047 #include "highscore.h"
00048 
00049 #include "bench.h"
00050 
00051 GameOver::GameOver()
00052 {
00053        halt_sound( "flying_sound" );
00054     halt_sound( "rock_sound" );
00055     halt_sound( "ice_sound" );
00056     halt_sound( "snow_sound" );
00057 
00058        if( Benchmark::getMode() != Benchmark::NONE ){
00059               Benchmark::displayState();
00060               winsys_exit( 0 );
00061        }
00062               
00063 if (gameMgr->gametype!=GameMgr::PRACTICING) {
00064 std::string mus = gameMgr->currentEvent->music;
00065 mus += "_game_over";
00066 play_music( (char *)mus.c_str() );
00067 }
00068 else
00069 {
00070 play_music("game_over");
00071 }
00072 
00073 
00074     m_aborted = gameMgr->wasRaceAborted();
00075 
00076        if ( !m_aborted ) {
00077               gameMgr->updatePlayersScores();
00078     }
00079               
00080     if ( gameMgr->gametype!=GameMgr::PRACTICING ) {
00081               m_bestScore = gameMgr->updateCurrentRaceData();         
00082               
00083               if(!gameMgr->wasRaceWon()){
00084                      players[0].decLives();
00085             //We have tried it and failed, and we have to play it next to advance
00086             if(players[0].getLives()>0) {
00087                 gameMgr->setRaceHasToBeRetried(true);
00088             } else {
00089                 //We cant play it any more, since we dont have any lives left...
00090                 gameMgr->setRaceHasToBeRetried(false);
00091             }
00092 
00093               }
00094     else{ //race was won
00095       gameMgr->setRaceHasToBeRetried(false);
00096     }
00097     }else{
00098               if ( !m_aborted ) {
00099                      m_bestScore = players[0].updateOpenCourseData(
00100                                                                gameMgr->getCurrentRace().name,
00101                                                                gameMgr->time,
00102                                                                players[0].herring,
00103                                                                players[0].score);
00104                      
00105                      if(m_bestScore) players[0].saveData();
00106               }
00107        }
00108        
00109        {  
00110        pp::Vec3d dir = players[0].vel;
00111        int speed = int(dir.normalize());
00112        //set max_speed
00113        if (speed > players[0].max_speed) players[0].max_speed=int(speed);
00114        }
00115        
00116        int width = getparam_x_resolution();
00117     int height = getparam_y_resolution();
00118        
00119        pp::Vec2d pos(width/2, height/2 +200);
00120        
00121        if ( gameMgr->wasRaceAborted() ) {
00122               mp_raceOverLbl = new pp::Label(pos,"race_over",_("Race aborted"));
00123               mp_raceOverLbl->alignment.center();
00124               mp_raceOverOutlineLbl = new pp::Label(pos,"race_over_outline",_("Race aborted"));
00125               mp_raceOverOutlineLbl->alignment.center();
00126     }else{    
00127               mp_raceOverLbl = new pp::Label(pos,"race_over",_("Race Over"));
00128               mp_raceOverLbl->alignment.center();
00129               mp_raceOverOutlineLbl = new pp::Label(pos,"race_over_outline",_("Race Over"));
00130               mp_raceOverOutlineLbl->alignment.center();
00131        
00132               char buff[BUFF_LEN];
00133               int minutes, seconds, hundredths;
00134 
00135               getTimeComponents( gameMgr->time, minutes, seconds, hundredths );
00136               sprintf( buff, _("Time: %02d:%02d.%02d"), minutes, seconds, hundredths );    
00137               pos.y-=100;
00138               mp_timeLbl = new pp::Label(pos,"race_stats",buff);
00139               mp_timeLbl->alignment.center();
00140               mp_timeOutlineLbl = new pp::Label(pos,"race_stats_outline",buff);
00141               mp_timeOutlineLbl->alignment.center();
00142        
00143               sprintf( buff, _("Herring: %3d"), players[0].herring );
00144               pos.y-=30;
00145               mp_herringLbl = new pp::Label(pos,"race_stats",buff);
00146               mp_herringLbl->alignment.center();
00147               mp_herringOutlineLbl = new pp::Label(pos,"race_stats_outline",buff);
00148               mp_herringOutlineLbl->alignment.center();
00149        
00150               sprintf( buff, _("Score: %6d"), players[0].score );
00151               pos.y-=30;
00152               mp_scoreLbl = new pp::Label(pos,"race_stats",buff);
00153               mp_scoreLbl->alignment.center();
00154               mp_scoreOutlineLbl = new pp::Label(pos,"race_stats_outline",buff);
00155               mp_scoreOutlineLbl->alignment.center();
00156        
00157               int speed = int((double)players[0].max_speed * M_PER_SEC_TO_KM_PER_H);
00158               sprintf( buff, _("Max speed: %3d km/h"), speed);
00159               pos.y-=30;
00160               mp_maxspeedLbl = new pp::Label(pos,"race_stats",buff);
00161               mp_maxspeedLbl->alignment.center();
00162               mp_maxspeedOutlineLbl = new pp::Label(pos,"race_stats_outline",buff);
00163               mp_maxspeedOutlineLbl->alignment.center();
00164        
00165               double percent = (gameMgr->airbornetime / gameMgr->time) * 100.0;
00166               sprintf( buff, _("Was flying: %.01f %% of time"), percent);
00167               pos.y-=30;
00168               mp_flyingLbl = new pp::Label(pos,"race_stats",buff);
00169               mp_flyingLbl->alignment.center();
00170               mp_flyingOutlineLbl = new pp::Label(pos,"race_stats_outline",buff);
00171               mp_flyingOutlineLbl->alignment.center();
00172               
00173               char buff2[50];
00174               snprintf(buff2, 50, "");
00175               if(highscore::useHighscore) {
00176                      int pos = Highscore->addScore(gameMgr->getCurrentRace().name,players[0].name,players[0].score);
00177                      if(pos != -1)
00178                             snprintf(buff2, 50, _("You made it to the %s place in the highscore!"),highscore::posToStr(pos).c_str());
00179               }
00180               pos.y-=30;
00181               mp_highscoreLbl = new pp::Label(pos,"race_stats",buff2);
00182               mp_highscoreLbl->alignment.center();
00183               mp_highscoreOutlineLbl = new pp::Label(pos,"race_stats_outline",buff2);
00184               mp_highscoreOutlineLbl->alignment.center();
00185        
00186               const char *string="";
00187        
00188               if ( gameMgr->gametype==GameMgr::PRACTICING){
00189                      if(m_bestScore){
00190                             string = _("You beat your best score!");
00191                      }
00192               } else if(gameMgr->wasEventWon()){
00193                      string = _("Congratulations! You won the campaign!");
00194               } else if(gameMgr->wasCupWon()){
00195                      string = _("Congratulations! You won the cup!");
00196               } else if(gameMgr->wasRaceWon()){
00197                      string = _("Requirements met! Advance to the next race.");
00198               } else {
00199                      string = _("Race requirements not met.");
00200               }      
00201        
00202               pos.y-=30;
00203               if (gameMgr->wasRaceWon()) {
00204                      mp_resultsLbl = new pp::Label(pos,"race_results",string);
00205                      mp_resultsLbl->alignment.center();}
00206               else{
00207                      mp_resultsLbl = new pp::Label(pos,"race_results_fail",string);
00208                      mp_resultsLbl->alignment.center();}
00209 
00210               mp_resultsOutlineLbl = new pp::Label(pos,"race_results_outline",string);
00211               mp_resultsOutlineLbl->alignment.center();
00212        }
00213 }
00214 
00215 GameOver::~GameOver()
00216 {
00217     if(gameMgr->getCurrentRace().snowing) reset_snow();
00218 //erase "game over" text from screen
00219        delete mp_raceOverLbl;
00220 //erase "game over" outline
00221        delete mp_raceOverOutlineLbl;
00222        
00223        if ( !gameMgr->wasRaceAborted() ) {
00224 //erase stats from the screen
00225               delete mp_timeLbl;
00226               delete mp_herringLbl;
00227               delete mp_scoreLbl;
00228               delete mp_maxspeedLbl;
00229               delete mp_flyingLbl;
00230               delete mp_resultsLbl;
00231               delete mp_highscoreLbl;
00232 
00233 //erase stat outlines from the screen
00234               delete mp_timeOutlineLbl;
00235               delete mp_herringOutlineLbl;
00236               delete mp_scoreOutlineLbl;
00237               delete mp_maxspeedOutlineLbl;
00238               delete mp_flyingOutlineLbl;
00239               delete mp_resultsOutlineLbl;
00240               delete mp_highscoreOutlineLbl;
00241        }
00242 }
00243 
00244 void
00245 GameOver::loop(float timeStep)
00246 {
00247     int width, height;
00248     width = getparam_x_resolution();
00249     height = getparam_y_resolution();
00250 
00251     /* Check joystick */
00252     if ( is_joystick_active() ) {
00253        update_joystick();
00254 
00255        if ( is_joystick_continue_button_down() )
00256        {
00257            if ( gameMgr->gametype != GameMgr::PRACTICING ) {
00258                      set_game_mode( EVENT_RACE_SELECT );
00259               }else{
00260                      set_game_mode( RACE_SELECT );
00261               }
00262            winsys_post_redisplay();
00263            return;
00264        }
00265     }
00266     fpsCounter.update();
00267 
00268     update_audio();
00269 
00270     clear_rendering_context();
00271 
00272     fogPlane.setup();
00273     update_player_pos( players[0], 0 );
00274     update_view( players[0], 0 );
00275 
00276     setup_view_frustum( players[0], NEAR_CLIP_DIST, 
00277                      getparam_forward_clip_distance() );
00278 
00279     draw_sky(players[0].view.pos);
00280     draw_fog_plane();
00281 
00282     set_course_clipping( true );
00283     set_course_eye_point( players[0].view.pos );
00284     setup_course_lighting();
00285     render_course();
00286     //Draw snow
00287     if(gameMgr->getCurrentRace().snowing) {
00288         draw_snow(players[0].view.pos);
00289     }
00290     draw_trees();
00291     if ( getparam_draw_particles() ) {
00292               draw_particles( players[0] );
00293     }
00294 
00295     ModelHndl->draw_tux();
00296     draw_tux_shadow();
00297     set_gl_options( GUI );
00298 
00299     UIMgr.setupDisplay();
00300        UIMgr.draw();
00301               
00302     HUD1.draw(players[0]);
00303     reshape( width, height );
00304 
00305     winsys_swap_buffers();
00306 }
00307 
00308 bool
00309 GameOver::mouseButtonEvent(int button, int x, int y, bool pressed)
00310 
00311 {
00312        if ( gameMgr->gametype!=GameMgr::PRACTICING ) {
00313               set_game_mode( EVENT_RACE_SELECT );
00314        }else{
00315               set_game_mode( RACE_SELECT );
00316        }
00317        winsys_post_redisplay();
00318        return true;
00319 }
00320 
00321 bool
00322 GameOver::keyPressEvent(SDLKey key)
00323 {
00324        if ( gameMgr->gametype!=GameMgr::PRACTICING ) {
00325               set_game_mode( EVENT_RACE_SELECT );
00326        }else{
00327               set_game_mode( RACE_SELECT );
00328        }
00329        winsys_post_redisplay();
00330        return true;
00331 }