src/game/game.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Classe principale qui gêre le jeu : initialisation, dessin, gestion
00020  * des différents composants, et boucle de jeu.
00021  *****************************************************************************/
00022 
00023 #include "game.h"
00024 #include <iostream>
00025 #include <SDL.h>
00026 #include <sstream>
00027 #include "time.h"
00028 #include "game_loop.h"
00029 #include "game_mode.h"
00030 #include "../graphic/video.h"
00031 #include "../graphic/fps.h"
00032 #include "../include/app.h"
00033 #include "../interface/cursor.h"
00034 #include "../interface/keyboard.h"
00035 #include "../interface/game_msg.h"
00036 #include "../interface/mouse.h"
00037 #include "../map/camera.h"
00038 #include "../map/map.h"
00039 #include "../menu/results_menu.h"
00040 #include "../object/objects_list.h"
00041 #include "../sound/jukebox.h"
00042 #include "../team/macro.h"
00043 #include "../team/results.h"
00044 #include "../tool/debug.h"
00045 #include "../tool/i18n.h"
00046 #include "../tool/resource_manager.h"
00047 #include "../weapon/weapons_list.h"
00048 
00049 Game * Game::singleton = NULL;
00050 
00051 Game * Game::GetInstance()
00052 {
00053   if (singleton == NULL) {
00054     singleton = new Game();
00055   }
00056   return singleton;
00057 }
00058 
00059 
00060 Game::Game()
00061 {
00062   isGameLaunched = false;
00063   endOfGameStatus = false;
00064 }
00065 
00066 bool Game::IsGameFinished()
00067 {
00068   return (NbrRemainingTeams() <= 1);
00069 }
00070 
00071 int Game::NbrRemainingTeams()
00072 {
00073   uint nbr = 0;
00074 
00075   FOR_EACH_TEAM(team){
00076     if( (**team).NbAliveCharacter() > 0 )
00077       nbr++;
00078   }
00079 
00080   return nbr;
00081 }
00082 
00083 void Game::MessageLoading()
00084 {
00085   std::cout << std::endl;
00086   std::cout << "[ " << _("Starting a new game") << " ]" << std::endl;
00087 }
00088 
00089 void Game::MessageEndOfGame()
00090 {
00091   std::vector<TeamResults*>* results_list = TeamResults::createAllResults();
00092   const char *winner_name = NULL;
00093 
00094   FOR_EACH_TEAM(team)
00095   {
00096     // Determine winner
00097     if (0 < (**team).NbAliveCharacter())
00098     {
00099       winner_name = (**team).GetName().c_str();
00100       break;
00101     }
00102   }
00103 
00104   // Print out results
00105   if (winner_name)
00106     jukebox.Play("share","victory");
00107 
00108   //question.Set (txt, true, 0);
00109   //AskQuestion();
00110   Mouse::GetInstance()->SetPointer(Mouse::POINTER_STANDARD);
00111   ResultsMenu menu(results_list, winner_name);
00112   menu.Run();
00113 
00114   TeamResults::deleteAllResults(results_list);
00115 }
00116 
00117 int Game::AskQuestion (Question &question, bool draw)
00118 {
00119   Time * global_time = Time::GetInstance();
00120   global_time->Pause();
00121 
00122   if (draw)
00123     GameLoop::GetInstance()->Draw ();
00124 
00125   int answer = question.Ask ();
00126 
00127   global_time->Continue();
00128   return answer;
00129 }
00130 
00131 void Game::Start()
00132 {
00133   bool err=true;
00134   bool end;
00135   std::string err_msg;
00136 
00137   try
00138   {
00139     GameLoop::GetInstance()->Init ();
00140 
00141     do
00142     {
00143       isGameLaunched = true;
00144       GameLoop::GetInstance()->fps.Reset();
00145 
00146       GameLoop::GetInstance()->Run();
00147 
00148       MSG_DEBUG( "game", "End of game_loop.Run()" );
00149       isGameLaunched = false;
00150 
00151       if (!IsGameFinished())
00152       {
00153         Question question;
00154         const char *msg = _("Do you really want to quit? (Y/N)");
00155         question.Set (msg, true, 0, "interface/quit_screen");
00156 
00157         {
00158           /* Tiny fix by Zygmunt Krynicki <zyga@zyga.dyndns.org> */
00159           /* Let's find out what the user would like to press ... */
00160           char *key_x_ptr = strchr (msg, '/');
00161           char key_x;
00162           if (key_x_ptr && key_x_ptr > msg) /* it's there and it's not the first char */
00163             key_x = tolower(key_x_ptr[-1]);
00164           else
00165             abort();
00166           if (!isalpha(key_x)) /* sanity check */
00167             abort();
00168 
00169           question.add_choice(SDLK_a + (int)key_x - 'a', 1);
00170         }
00171 
00172         jukebox.Pause();
00173         end = (AskQuestion(question) == 1);
00174         jukebox.Resume();
00175         if(!end)
00176           world.ToRedrawOnScreen(Rectanglei(Point2i(0,0),AppWormux::GetInstance()->video.window.GetSize()));
00177       } else {
00178         end = true;
00179       }
00180     } while (!end);
00181     err = false;
00182   }
00183   catch (const std::exception &e)
00184   {
00185     err_msg = e.what();
00186   }
00187 
00188   if (!err)
00189     if (IsGameFinished())
00190       MessageEndOfGame();
00191 
00192   UnloadDatas();
00193   Mouse::GetInstance()->SetPointer(Mouse::POINTER_STANDARD);
00194 
00195   if (err)
00196   {
00197     Question question;
00198     std::string txt = Format(_("Error:\n%s"), err_msg.c_str());
00199     std::cout << std::endl << txt << std::endl;
00200     question.Set (txt, true, 0);
00201     AskQuestion (question, false);
00202   }
00203 }
00204 
00205 void Game::UnloadDatas()
00206 {
00207   world.FreeMem();
00208   lst_objects.FreeMem();
00209   ParticleEngine::Stop();
00210   teams_list.UnloadGamingData();
00211   jukebox.StopAll();
00212 }
00213 
00214 void Game::Pause()
00215 {
00216   Question question;
00217   if(!network.IsLocal())
00218     return;
00219 
00220   jukebox.Pause();
00221 
00222   //Pause screen
00223   question.Set ("", false, 0, "interface/pause_screen");
00224   question.add_choice(Config::GetInstance()->GetKeyboard()->GetKeyAssociatedToAction(Action::ACTION_PAUSE),
00225                       1
00226                       );
00227   AskQuestion(question, false);
00228   jukebox.Resume();
00229 }
00230 
00231 bool Game::IsGameLaunched() const{
00232   return isGameLaunched;
00233 }
00234 
00235 bool Game::GetEndOfGameStatus(){
00236   return endOfGameStatus;
00237 }
00238 
00239 void Game::SetEndOfGameStatus(bool status){
00240   endOfGameStatus = status;
00241 }

Generated on Mon Jan 1 13:10:56 2007 for Wormux by  doxygen 1.4.7