src/StateBattle/StateDone.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Naval Battle Done Code
00003  */
00004 
00005 #include <string>
00006 #include <sstream>
00007 #include <guichan.hpp>
00008 #include <guichan/sdl.hpp>
00009 #include <guichan/opengl.hpp>
00010 #include "SDL.h"
00011 #include "../GLee.h"
00012 #include "../Menu/CCGui.h"
00013 #include "../Menu/CCTable.h"
00014 #include "../Menu/CCButton.h"
00015 #include "../Menu/CCFont.h"
00016 #include "../Menu/CCLabel.h"
00017 #include "../Config.h"
00018 #include "../Log.h"
00019 #include "../Texture.h"
00020 #include "../font.h"
00021 #include "../IGameState.h"
00022 #include "../ccmath.h"
00023 #include "../Player.h"
00024 #include "../Ship.h"
00025 #include "../Economy.h"
00026 #include "../Product.h"
00027 #include "../Cargo.h"
00028 #include "StateDone.h"
00029 
00030 using namespace std;
00031 
00032 const string StateDone::s_mainFontName = "smallFont.png";
00033 const string StateDone::s_highlightFontName = "smallHighlight.png";
00034 const string StateDone::s_disabledFontName = "smallDisabledFont.png";
00035 
00036 StateDone::StateDone(Ship *enemy, BattleResult result): IGameState(NULL) {
00037   string backgroundName;
00038 
00039   m_enemyGold = (int) (randBellCurve() * 100.0);
00040 
00041   m_enemyCargo = enemy->GetCargo();
00042   m_enemyCargo->FillRandom();
00043   m_enemyCargo->Dump();
00044 
00045   m_playerCargo = Player::player->GetCargo();
00046 
00047   // Make sure this is large enough just so the player could leave everything if they wanted to
00048   m_enemyCargo->SetSize(m_enemyCargo->GetSize() + m_playerCargo->GetSize());
00049 
00050   m_exitCallback.SetCallback(this, &StateDone::Exit);
00051   m_buyCallback.SetCallback(this, &StateDone::Buy);
00052   m_sellCallback.SetCallback(this, &StateDone::Sell);
00053   m_buyAllCallback.SetCallback(this, &StateDone::TakeAll);
00054   m_sellAllCallback.SetCallback(this, &StateDone::LeaveAll);
00055 
00056   switch (result) {
00057     case sbVictory:
00058       backgroundName = "battlesuccess.png";
00059       break;
00060     case sbDefeat:
00061       backgroundName = "battledefeat.png";
00062       break;
00063     case sbNone:
00064       backgroundName = "battletie.png";
00065       break;
00066   }
00067 
00068   Log::s_log->Message("Creating background image");
00069   m_background = new Texture(backgroundName);
00070 
00071   /* Setting these to null keeps us from calling
00072    * delete on objects that don't exist. They don't
00073    * get created if we don't win the battle.
00074    */
00075   m_productTable = NULL;
00076 
00077   Log::s_log->Message("Setting up captions");
00078   switch (result) {
00079     case sbVictory:
00080       m_firstTitle = new CCLabel("Victory!", s_mainFontName);
00081       m_secondTitle = new CCLabel("You get the enemy's gold.", s_mainFontName);
00082       Log::s_log->Message("Setting up menu");
00083       SetupMenu();
00084       Player::player->gold += m_enemyGold;
00085       break;
00086     case sbDefeat:
00087       m_firstTitle = new CCLabel("You were defeated!", s_mainFontName);
00088       m_secondTitle = new CCLabel("The enemy sacks your ship but luckily you escape.", s_mainFontName);
00089 
00090       SackPlayer();
00091       break;
00092     case sbNone:
00093       m_firstTitle = new CCLabel("The battle draws to a close as the ships slip apart.", s_mainFontName);
00094       m_secondTitle = new CCLabel("", s_mainFontName);
00095       m_productTable = NULL;
00096       break;
00097   }
00098 
00099   Log::s_log->Message("Creating done button");
00100   m_doneButton = new CCButton("Done", "done_button", s_mainFontName, s_highlightFontName, s_disabledFontName, &m_exitCallback);
00101 
00102   m_top->add(m_doneButton, 15, 150);
00103 
00104   m_top->add(m_firstTitle, 15, 50);
00105   m_top->add(m_secondTitle, 15, 80);
00106 
00107   Log::s_log->Message("Setting up menu");
00108 }
00109 
00110 StateDone::~StateDone() {
00111   // Don't delete either cargo, since the ships we got them from will do that
00112   if (m_productTable != NULL)
00113     delete m_productTable;
00114   delete m_firstTitle;
00115   delete m_secondTitle;
00116   delete m_doneButton;
00117 
00118   delete m_background;
00119 
00120   Log::s_log->Message("State Done deleted");
00121 }
00122 
00123 void StateDone::SetupMenu() {
00124   stringstream tempStr;
00125   CCButton* tempButton;
00126   CCLabel* tempLabel;
00127 
00128   Log::s_log->Message("Creating new Table");
00129   m_productTable = new CCTable(5, Economy::s_economy->ProductCount() + 2);
00130   Log::s_log->Message("New Table Created");
00131 
00132   m_top->add(m_productTable, 300, 300);
00133 
00134   Log::s_log->Message("Created and set fonts");
00135 
00136   /*****************************************************
00137    * This section sets up the headers for the columns. *
00138    *****************************************************/
00139   tempLabel = new CCLabel("Products", s_mainFontName);
00140   m_productTable->add(0, 0, tempLabel);
00141   //widgetList.push_back(tempLabel);
00142 
00143   tempLabel = new CCLabel("Take", s_mainFontName);
00144   m_productTable->add(1, 0, tempLabel);
00145   m_productTable->resizeColumn(1, m_productTable->getColumnSize(0));
00146 
00147   tempLabel = new CCLabel("Leave", s_mainFontName);
00148   m_productTable->add(4, 0, tempLabel);
00149   m_productTable->resizeColumn(4, m_productTable->getColumnSize(0));
00150   /* End Headers */
00151 
00152   m_productTable->setColumnBuffer(10);
00153 
00154   Log::s_log->Message("Set table headers");
00155   /* Need to fill in the table with each product... */
00156   for (int i = 0; i < Economy::s_economy->ProductCount(); i++) {
00157     tempLabel = new CCLabel(Economy::s_economy->ProdList[i], s_mainFontName);
00158     m_productTable->add(0, i + 1, tempLabel);
00159 
00160     tempStr.str("");
00161     tempStr << m_playerCargo->GetQty(i);
00162     tempLabel = new CCLabel(tempStr.str(), s_mainFontName);
00163     m_productTable->add(1, i + 1, tempLabel);
00164 
00165     /* Each Take and Leave button gets the ID of
00166      * the product that it corresponds to. This is
00167      * represented in this loop by i. So if Cotton
00168      * is at Economy::s_economy->ProdList[0], then id
00169      * here would be zero. That way in our callbacks
00170      * it makes it real easy to figure which widgets
00171      * need to be changed.
00172      */
00173     tempButton = new CCButton("Take", "buy" + Economy::s_economy->ProdList[i], s_mainFontName, s_highlightFontName, s_disabledFontName, &m_buyCallback);
00174     tempButton->SetID(i);
00175     m_productTable->add(2, i + 1, tempButton);
00176 
00177     tempButton = new CCButton("Leave", "sell" + Economy::s_economy->ProdList[i], s_mainFontName, s_highlightFontName, s_disabledFontName, &m_sellCallback);
00178     tempButton->SetID(i);
00179     m_productTable->add(3, i + 1, tempButton);
00180 
00181     tempStr.str("");
00182     tempStr << m_enemyCargo->GetQty(i);
00183     tempLabel = new CCLabel(tempStr.str(), s_mainFontName);
00184     m_productTable->add(4, i + 1, tempLabel);
00185   }
00186 
00187   Log::s_log->Message("Setting up Take and Leave all buttons");
00188   tempButton = new CCButton("All", "sellAll", s_mainFontName, s_highlightFontName, s_disabledFontName, &m_sellAllCallback);
00189   m_productTable->add(3, m_productTable->getNumRows() - 1, tempButton);
00190   tempButton = new CCButton("All", "BuyAll", s_mainFontName, s_highlightFontName, s_disabledFontName, &m_buyAllCallback);
00191   m_productTable->add(2, m_productTable->getNumRows() - 1, tempButton);
00192 }
00193 
00194 void StateDone::Update(const unsigned int ticks) {
00195   CheckInput();
00196 }
00197 
00198 void StateDone::Display() {
00199   glLoadIdentity();
00200   DisplayBackground(m_background->GetTexture(), 1);
00201 }
00202 
00203 void StateDone::CheckInput() {
00204  SDL_Event event;
00205   /*
00206    * Poll SDL events
00207    */
00208   while(SDL_PollEvent(&event))
00209   {
00210     if (event.type == SDL_KEYDOWN)
00211     {
00212       /*
00213       if (event.key.keysym.sym == SDLK_ESCAPE)
00214       {
00215         done = false;
00216       }
00217       if (event.key.keysym.sym == SDLK_f)
00218       {
00219         if (event.key.keysym.mod & KMOD_CTRL)
00220         {
00221           // Works with X11 only
00222           //SDL_WM_ToggleFullScreen(screen);
00223         }
00224       }
00225       */
00226       if (event.key.keysym.sym == SDLK_z) {
00227         TakeScreenshot();
00228       }
00229     }
00230 
00231     /*
00232      * Now that we are done polling and using SDL events we pass
00233      * the leftovers to the SDLInput object to later be handled by
00234      * the Gui. (This example doesn't require us to do this 'cause a
00235      * label doesn't use input. But will do it anyway to show how to
00236      * set up an SDL application with Guichan.)
00237      */
00238     CCGui::s_CCGui->m_input->pushInput(event);
00239   }
00240 }
00241 void StateDone::Exit(int ID) {
00242   PrepareStateSwitch(m_previous, this);
00243 }
00244 
00245 void StateDone::Buy(int ID) {
00246   int productId = -1;
00247   stringstream tempStr;
00248   CCLabel* tempLabel;
00249 
00250   productId = ID;
00251 
00252   if ((productId > 0) && (productId >= Economy::s_economy->ProductCount())) {
00253     Log::s_log->Message("StateDone.sell: Unknown id (%d)", ID);
00254     return;
00255   }
00256 
00257   if ((m_enemyCargo->GetQty(productId) > 0) && (m_playerCargo->GetAvailQty() > 0)) {
00258     m_playerCargo->AddQty(productId, 1);
00259     m_enemyCargo->AddQty(productId, -1);
00260     tempStr.str("");
00261     tempStr << m_playerCargo->GetQty(productId);
00262     tempLabel = (CCLabel*)m_productTable->getWidget(1, productId + 1);
00263     tempLabel->setCaption(tempStr.str());
00264     tempLabel->adjustSize();
00265     tempStr.str("");
00266     tempStr << m_enemyCargo->GetQty(productId);
00267     tempLabel = (CCLabel*)m_productTable->getWidget(4, productId + 1);
00268     tempLabel->setCaption(tempStr.str());
00269     tempLabel->adjustSize();
00270   }
00271 
00272 }
00273 
00274 void StateDone::Sell(int ID) {
00275   int productId = -1;
00276   stringstream tempStr;
00277   CCLabel* tempLabel;
00278 
00279   productId = ID;
00280 
00281   if ((productId > 0) && (productId >= Economy::s_economy->ProductCount())) {
00282     Log::s_log->Message("StateDone.sell: Unknown id (%d)", ID);
00283     return;
00284   }
00285 
00286   if ((m_playerCargo->GetQty(productId) > 0) && (m_enemyCargo->GetAvailQty() > 0)) {
00287     m_playerCargo->AddQty(productId, -1);
00288     m_enemyCargo->AddQty(productId, 1);
00289     tempStr.str("");
00290     tempStr << m_playerCargo->GetQty(productId);
00291     tempLabel = (CCLabel*)m_productTable->getWidget(1, productId + 1);
00292     tempLabel->adjustSize();
00293     tempLabel->setCaption(tempStr.str());
00294     tempStr.str("");
00295     tempStr << m_enemyCargo->GetQty(productId);
00296     tempLabel = (CCLabel*)m_productTable->getWidget(4, productId + 1);
00297     tempLabel->setCaption(tempStr.str());
00298     tempLabel->adjustSize();
00299   }
00300 
00301 }
00302 
00303 void StateDone::TakeAll(int ID) {
00304   m_playerCargo->AddCargo(m_enemyCargo);
00305 
00306   SetMenuQtys();
00307 }
00308 
00309 void StateDone::LeaveAll(int ID) {
00310   m_enemyCargo->AddCargo(m_playerCargo);
00311 
00312   SetMenuQtys();
00313 }
00314 
00315 void StateDone::SackPlayer() {
00316   unsigned int i;
00317 
00318   Player::player->ship->m_damage = 1;
00319   Player::player->gold = (int) (Player::player->gold * randBellCurve());
00320 
00321 
00322   for (i = 0; i < (unsigned int) Economy::s_economy->ProductCount(); i++) {
00323     try {
00324       m_playerCargo->SetQty(i, (unsigned int) (m_playerCargo->GetQty(i) * randBellCurve()));
00325     } catch (const char *e) {
00326       Log::s_log->Message("Warning: Could not set qty in sackPlayer (%s)", e);
00327     }
00328   }
00329 }
00330 
00331 void StateDone::SetMenuQtys() {
00332   int i;
00333   stringstream tempStr;
00334   Label* tempLabel;
00335 
00336   Log::s_log->Message("setMenuQtys");
00337 
00338   for (i = 0; i < Economy::s_economy->ProductCount(); i++) {
00339     tempStr.str("");
00340     tempStr << m_playerCargo->GetQty(i);
00341     tempLabel = (Label*)m_productTable->getWidget(1, i + 1);
00342     tempLabel->setCaption(tempStr.str());
00343     tempLabel->adjustSize();
00344     Log::s_log->Message("Player %d: %s", i, tempStr.str().c_str());
00345     tempStr.str("");
00346     tempStr << m_enemyCargo->GetQty(i);
00347     Log::s_log->Message("Enemy %d: %s", i, tempStr.str().c_str());
00348     tempLabel = (Label*)m_productTable->getWidget(4, i + 1);
00349     tempLabel->setCaption(tempStr.str());
00350     tempLabel->adjustSize();
00351   }
00352 
00353 }
00354 
00355 void StateDone::SwitchTo(IGameState *oldState) {
00356   EnterTextMode(Config::s_config->GetWinWidth(), Config::s_config->GetWinHeight());
00357 }
00358 
00359 void StateDone::SwitchFrom() {
00360 }

Generated on Mon Jan 8 22:34:13 2007 for CrownandCutlass by  doxygen 1.4.7