StateSailing Class Reference

#include <StateSailing.h>

Inheritance diagram for StateSailing:

IGameState ISaveObject List of all members.

Public Member Functions

 StateSailing (const std::string playerName, const std::string shipName)
 ~StateSailing ()
void NewGame (const std::string playerName, const std::string shipName)
void Save (TiXmlElement *parent)
void Load (TiXmlElement *parent)
void Update (const unsigned int ticks)
void Display ()
void SwitchTo (IGameState *oldState)
void SwitchFrom ()
void Dump ()

Public Attributes

CityManagerm_cities

Private Member Functions

void Initialize (const std::string playerName, const std::string shipName)
void Destroy ()
void CheckInput ()
void HandleKeyUp (SDL_keysym *keysym)
void HandleKeyDown (SDL_keysym *keysym)
void Timer (const unsigned int ticks)
void SetTicksToNextBattle ()

Private Attributes

int m_ticksToNextBattle
GLfloat m_rotx
Terrainm_land
Mapm_map
IGameStatem_childState
Cameram_camera
GLfloat m_cameraX
GLfloat m_cameraY
GLfloat m_cameraZ
GLfloat m_ambientLight [4]
GLfloat m_diffuseLight [4]
GLfloat m_lightPosition [4]
EconomyScreenm_es

Static Private Attributes

static const std::string s_mainFontName = "papyrus.png"

Detailed Description

Definition at line 28 of file StateSailing.h.


Constructor & Destructor Documentation

StateSailing::StateSailing ( const std::string  playerName,
const std::string  shipName 
)

Definition at line 43 of file StateSailing.cpp.

References IGameState::DisplaySplashScreen(), Initialize(), m_ambientLight, m_childState, m_diffuseLight, m_land, m_lightPosition, Log::Message(), Log::s_log, s_mainFontName, and SetTicksToNextBattle().

00043                                                             : IGameState(NULL), ISaveObject("StateSailing") {
00044   DisplaySplashScreen("loading.png", "Loading...", s_mainFontName, .4);
00045 
00046   Log::s_log->Message("Loading screen displayed");
00047 
00048   // Set the m_childState pointer to NULL
00049   m_childState = NULL;
00050 
00051   SetTicksToNextBattle();
00052 
00053   // Create the terrain
00054   m_land = new Terrain("data/heightmap.png", .04, 25);
00055 
00056   Log::s_log->Message("Terrain created");
00057 
00058   // Set the light color and position
00059   m_ambientLight[0] = 0.17;
00060   m_ambientLight[1] = 0.17;
00061   m_ambientLight[2] = 0.17;
00062   m_ambientLight[3] = 1.0;
00063 
00064   m_diffuseLight[0] = 1.0;
00065   m_diffuseLight[1] = 1.0;
00066   m_diffuseLight[2] = 1.0;
00067   m_diffuseLight[3] = 1.0;
00068 
00069   m_lightPosition[0] = 7.0;
00070   m_lightPosition[1] = 15.0;
00071   m_lightPosition[2] = 2.0;
00072   m_lightPosition[3] = 0.0;
00073 
00074   // Begin setting up the rendering
00075 
00076   glClearColor(0, 0, 0, 0);
00077 
00078   glEnable(GL_TEXTURE_2D);
00079   glEnable(GL_DEPTH_TEST);
00080   glEnable(GL_CULL_FACE);
00081   //glEnable(GL_NORMALIZE);
00082   glShadeModel(GL_SMOOTH);
00083   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00084   glDisable(GL_BLEND);
00085 
00086   // Set up lighting
00087   glEnable(GL_LIGHTING);
00088   glLightfv(GL_LIGHT0, GL_AMBIENT, m_ambientLight);
00089   glLightfv(GL_LIGHT0, GL_DIFFUSE, m_diffuseLight);
00090   glLightfv(GL_LIGHT0, GL_POSITION, m_lightPosition);
00091   glEnable(GL_LIGHT0);
00092 
00093   Initialize(playerName, shipName);
00094 
00095   Log::s_log->Message("StateSailing initialized\n");
00096 }

StateSailing::~StateSailing (  ) 

Definition at line 98 of file StateSailing.cpp.

References Destroy(), m_land, Log::Message(), and Log::s_log.

00098                             {
00099   Destroy();
00100 
00101   // Delete all the objects that don't get cleaned up by destroy
00102   delete m_land;
00103 
00104   Log::s_log->Message("StateSailing deleted");
00105 }


Member Function Documentation

void StateSailing::CheckInput (  )  [private, virtual]

Implements IGameState.

Definition at line 217 of file StateSailing.cpp.

References IGameState::DoExit(), HandleKeyDown(), HandleKeyUp(), CCGui::m_input, and CCGui::s_CCGui.

Referenced by Update().

00217                               {
00218   /* Our SDL event placeholder. */
00219   SDL_Event event;
00220 
00221   /* Grab all the events off the queue. */
00222   while( SDL_PollEvent( &event ) ) {
00223     switch( event.type ) {
00224     case SDL_KEYDOWN:
00225       /* Handle key presses. */
00226       HandleKeyDown( &event.key.keysym );
00227       break;
00228     case SDL_KEYUP:
00229       HandleKeyUp(&event.key.keysym);
00230       break;
00231     case SDL_QUIT:
00232       /* Handle quit requests (like Ctrl-c). */
00233       DoExit();
00234       break;
00235     }
00236     CCGui::s_CCGui->m_input->pushInput(event);
00237   }
00238 }

void StateSailing::Destroy (  )  [private]

Definition at line 148 of file StateSailing.cpp.

References Player::DeletePlayer(), Economy::DestroyEconomy(), m_camera, m_childState, m_cities, m_es, and m_map.

Referenced by NewGame(), and ~StateSailing().

00148                            {
00149   // Delete the m_childState if it's active
00150   if (m_childState != NULL) {
00151     delete m_childState;
00152     m_childState = NULL;
00153   }
00154 
00155   Economy::DestroyEconomy();
00156 
00157   delete m_map;
00158   delete m_es;
00159   delete m_cities;
00160   delete m_camera;
00161 
00162   Player::DeletePlayer();
00163 }

void StateSailing::Display (  )  [virtual]

Implements IGameState.

Definition at line 202 of file StateSailing.cpp.

References CityManager::DisplayCities(), Terrain::Draw(), Ship::Draw(), m_camera, m_cities, m_land, Ship::m_z, Player::player, Camera::SetCamera(), and Player::ship.

00202                            {
00203   glLoadIdentity();
00204   m_camera->SetCamera();
00205 
00206   // Draw the ship
00207   Player::player->ship->Draw(false, 0, 0);
00208 
00209   // Draw the terrain
00210   glPushMatrix();
00211   glTranslatef(-1*Player::player->ship->m_x, 0, -1*Player::player->ship->m_z);
00212   m_land->Draw();
00213   m_cities->DisplayCities();
00214   glPopMatrix();
00215 }

void StateSailing::Dump (  ) 

Definition at line 419 of file StateSailing.cpp.

References Player::Dump(), and Player::player.

Referenced by HandleKeyDown(), and MainMenu::LoadGame().

00419                         {
00420   Player::player->Dump();
00421 }

void StateSailing::HandleKeyDown ( SDL_keysym *  keysym  )  [private]

Definition at line 259 of file StateSailing.cpp.

References Dump(), LEFT, Ship::LowerSails(), m_camera, m_cameraX, m_cameraY, m_cameraZ, m_childState, m_cities, m_es, m_land, M_PI, Ship::m_rot, Player::player, IGameState::PrepareStateSwitch(), Ship::RaiseSails(), RIGHT, Camera::SetPosition(), Ship::SetRudder(), SetTicksToNextBattle(), Player::ship, IGameState::TakeScreenshot(), and EconomyScreen::ToggleShowScreen().

Referenced by CheckInput().

00259                                                    {
00260   switch( keysym->sym ) {
00261   case SDLK_b:
00262     SetTicksToNextBattle();
00263     if (m_childState != NULL) {
00264       delete m_childState;
00265     }
00266     m_childState = new StateBattle();
00267     PrepareStateSwitch(m_childState, this);
00268     break;
00269   case SDLK_c:
00270     Player::player->ship->m_rot += M_PI;
00271     break;
00272   case SDLK_d:
00273     Dump();
00274     break;
00275   case SDLK_e:
00276     m_es->ToggleShowScreen();
00277     break;
00278   case SDLK_z:
00279     TakeScreenshot();
00280     break;
00281   case SDLK_u:
00282     delete m_cities;
00283     m_cities = new CityManager(m_land);
00284     break;
00285   case SDLK_1:
00286     m_cameraX = 0;
00287     m_cameraY = 13;
00288     m_cameraZ = 9;
00289     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00290     break;
00291   case SDLK_2:
00292     m_cameraX = 0;
00293     m_cameraY = 150;
00294     m_cameraZ = 150;
00295     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00296     break;
00297   case SDLK_3:
00298     m_cameraX = 0;
00299     m_cameraY = 5;
00300     m_cameraZ = 9;
00301     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00302     break;
00303   case SDLK_4:
00304     m_cameraX = 0;
00305     m_cameraY = 26;
00306     m_cameraZ = 18;
00307     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00308     break;
00309   case SDLK_5:
00310     m_cameraX = 0;
00311     m_cameraY = 0;
00312     m_cameraZ = 10;
00313     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00314     break;
00315   case SDLK_6:
00316     m_cameraX = 0;
00317     m_cameraY = 10;
00318     m_cameraZ = .00001;
00319     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00320     break;
00321   case SDLK_7:
00322     m_cameraX = 0;
00323     m_cameraY = 2.6;
00324     m_cameraZ = 1.8;
00325     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00326     break;
00327   case SDLK_8:
00328     m_cameraX = 0;
00329     m_cameraY = 0;
00330     m_cameraZ = 3;
00331     m_camera->SetPosition(m_cameraX, m_cameraY, m_cameraZ);
00332     break;
00333   case SDLK_LEFT:
00334     Player::player->ship->SetRudder(LEFT);
00335     break;
00336   case SDLK_RIGHT:
00337     Player::player->ship->SetRudder(RIGHT);
00338     break;
00339   case SDLK_UP:
00340     Player::player->ship->RaiseSails();
00341     break;
00342   case SDLK_DOWN:
00343     Player::player->ship->LowerSails();
00344   default:
00345     break;
00346   }
00347 }

void StateSailing::HandleKeyUp ( SDL_keysym *  keysym  )  [private]

Definition at line 240 of file StateSailing.cpp.

References CENTER, m_map, Player::player, IGameState::PrepareStateSwitch(), IGameState::s_mainMenu, Ship::SetRudder(), and Player::ship.

Referenced by CheckInput().

00240                                                  {
00241   switch( keysym->sym ) {
00242   case SDLK_LEFT:
00243     Player::player->ship->SetRudder(CENTER);
00244     break;
00245   case SDLK_RIGHT:
00246     Player::player->ship->SetRudder(CENTER);
00247     break;
00248   case SDLK_ESCAPE:
00249     PrepareStateSwitch(s_mainMenu, this);
00250     break;
00251   case SDLK_m:
00252     PrepareStateSwitch(m_map, this);
00253     break;
00254   default:
00255     break;
00256   }
00257 }

void StateSailing::Initialize ( const std::string  playerName,
const std::string  shipName 
) [private]

Definition at line 107 of file StateSailing.cpp.

References Economy::CreateEconomy(), Player::CreateNewPlayer(), EconomyScreen::GetWidget(), Config::GetWinHeight(), Config::GetWinWidth(), m_camera, m_cameraX, m_cameraY, m_cameraZ, m_cities, m_es, m_land, m_map, m_rotx, IGameState::m_top, Log::Message(), Player::player, Timer::ResetTimer(), Config::s_config, Log::s_log, and Timer::timer.

Referenced by NewGame(), and StateSailing().

00107                                                                             {
00108   bool EconCreated;
00109 
00110   // Temp storage for window width & height
00111   int width = Config::s_config->GetWinWidth();
00112   int height = Config::s_config->GetWinHeight();
00113 
00114   // Initialize sway of the ship
00115   m_rotx = 0.0;
00116 
00117   // Set up the camera
00118   m_cameraX = 0;
00119   m_cameraY = 13;
00120   m_cameraZ = 9;
00121   m_camera = new Camera((float) width/(float) height, m_cameraX, m_cameraY, m_cameraZ);
00122 
00123   Log::s_log->Message("Camera created");
00124 
00125   EconCreated = Economy::CreateEconomy();
00126 
00127   if (EconCreated) Log::s_log->Message("Economy loaded");
00128   else Log::s_log->Message("***Economy loading failed***");
00129 
00130   // Create Cities
00131   m_cities = new CityManager(m_land);
00132 
00133   // Create the economy screen
00134   m_es = new EconomyScreen(m_cities, 600, 600);
00135   m_top->add(m_es->GetWidget(), 10, 10);
00136   m_top->moveToTop(m_es->GetWidget());
00137 
00138   Player::CreateNewPlayer(playerName, shipName);
00139 
00140   // Create the map
00141   m_map = new Map(this, Player::player->ship, m_land);
00142 
00143   Log::s_log->Message("Map created");
00144 
00145   Timer::timer->ResetTimer();
00146 }

void StateSailing::Load ( TiXmlElement *  parent  )  [virtual]

Implements ISaveObject.

Definition at line 177 of file StateSailing.cpp.

References Player::Load(), ISaveObject::m_XMLName, and Player::player.

Referenced by MainMenu::LoadGame().

00177                                             {
00178   TiXmlElement *selfNode;
00179 
00180   selfNode = parent->FirstChildElement(m_XMLName);
00181   Player::player->Load(selfNode);
00182 }

void StateSailing::NewGame ( const std::string  playerName,
const std::string  shipName 
)

Definition at line 165 of file StateSailing.cpp.

References Destroy(), and Initialize().

Referenced by StateNewGame::Accept(), and MainMenu::Accept().

00165                                                                          {
00166   Destroy();
00167   Initialize(playerName, shipName);
00168 }

void StateSailing::Save ( TiXmlElement *  parent  )  [virtual]

Implements ISaveObject.

Definition at line 170 of file StateSailing.cpp.

References ISaveObject::m_XMLName, Player::player, and Player::Save().

Referenced by MainMenu::SaveGame().

00170                                             {
00171   TiXmlElement selfNode(m_XMLName);
00172 
00173   Player::player->Save(&selfNode);
00174   parent->InsertEndChild(selfNode);
00175 }

void StateSailing::SetTicksToNextBattle (  )  [private]

Definition at line 423 of file StateSailing.cpp.

References m_ticksToNextBattle, randBellCurve(), and Config::s_config.

Referenced by HandleKeyDown(), StateSailing(), and Timer().

00423                                         {
00424   m_ticksToNextBattle = (int) (Config::s_config->GetTicksBetweenBattles() * (randBellCurve() + .5));
00425 }

void StateSailing::SwitchFrom (  )  [virtual]

Implements IGameState.

Definition at line 444 of file StateSailing.cpp.

00444                               {
00445   glDisable(GL_CULL_FACE);
00446   glDisable(GL_LIGHTING);
00447   glDisable(GL_DEPTH_TEST);
00448 }

void StateSailing::SwitchTo ( IGameState oldState  )  [virtual]

Implements IGameState.

Definition at line 427 of file StateSailing.cpp.

References m_ambientLight, m_camera, m_diffuseLight, m_lightPosition, and Camera::SetProjection().

00427                                                 {
00428   glMatrixMode(GL_MODELVIEW);
00429   glLoadIdentity();
00430 
00431   m_camera->SetProjection();
00432 
00433   glEnable(GL_DEPTH_TEST);
00434   glEnable(GL_CULL_FACE);
00435   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00436   glDisable(GL_BLEND);
00437   glLightfv(GL_LIGHT0, GL_AMBIENT, m_ambientLight);
00438   glLightfv(GL_LIGHT0, GL_DIFFUSE, m_diffuseLight);
00439   glLightfv(GL_LIGHT0, GL_POSITION, m_lightPosition);
00440   glEnable(GL_LIGHT0);
00441   glEnable(GL_LIGHTING);
00442 }

void StateSailing::Timer ( const unsigned int  ticks  )  [private]

Definition at line 349 of file StateSailing.cpp.

References CityManager::CheckCollision(), Terrain::CheckLineCollision(), Ship::GetDirectionVector(), Terrain::GetHeight(), Terrain::GetWidth(), m_childState, m_cities, m_land, M_PI, Ship::m_rot, m_rotx, Ship::m_speed, m_ticksToNextBattle, Ship::m_x, Ship::m_z, Log::Message(), NOSAILS, Player::player, IGameState::PrepareStateSwitch(), Economy::s_economy, Log::s_log, Ship::SetSails(), SetTicksToNextBattle(), Player::ship, TWO_PI, Terrain::Update(), Ship::Update(), and Economy::UpdateEconomy().

Referenced by Update().

00349                                                  {
00350   double shipLine[3];
00351   int maxX, maxZ;
00352 
00353   maxX = m_land->GetWidth() / 2 - 5;
00354   maxZ = m_land->GetHeight() / 2 - 5;
00355 
00356   // Used to check for a collision with a city
00357   City *town;
00358 
00359   // Check for hitting a city
00360   town = m_cities->CheckCollision(Player::player->ship);
00361   if (town != NULL) {
00362     Player::player->ship->m_speed = 0;
00363     Player::player->ship->SetSails(NOSAILS);
00364     Player::player->ship->m_rot += M_PI;
00365     if (Player::player->ship->m_rot > TWO_PI) Player::player->ship->m_rot -= TWO_PI;
00366 
00367     if (m_childState != NULL) {
00368       Log::s_log->Message("Warning: m_childState not null when entering town");
00369       delete m_childState;
00370       m_childState = NULL;
00371     }
00372 
00373     // Before we switch, update the economy
00374     Economy::s_economy->UpdateEconomy();
00375 
00376     m_childState = new StateCity(town);
00377 
00378     PrepareStateSwitch(m_childState, this);
00379   }
00380 
00381   Player::player->ship->Update(ticks);
00382   Player::player->ship->GetDirectionVector(shipLine);
00383 
00384   if (Player::player->ship->m_x > maxX) {
00385     Player::player->ship->m_x = maxX;
00386   } else if (Player::player->ship->m_x < -maxX) {
00387     Player::player->ship->m_x = -maxX;
00388   }
00389   if (Player::player->ship->m_z > maxZ) {
00390     Player::player->ship->m_z = maxZ;
00391   } else if (Player::player->ship->m_z < -maxZ) {
00392     Player::player->ship->m_z = -maxZ;
00393   }
00394 
00395   if((Player::player->ship->m_speed > 0) && (m_land->CheckLineCollision(Player::player->ship->m_x, Player::player->ship->m_z, 6, shipLine, 1.25))) {
00396     Player::player->ship->m_speed = 0;
00397     Player::player->ship->SetSails(NOSAILS);
00398   }
00399 
00400   m_land->Update(ticks);
00401 
00402   m_rotx += .001 * ticks;
00403   if (m_rotx > TWO_PI) m_rotx -= TWO_PI;
00404 
00405   m_ticksToNextBattle -= ticks;
00406   if (m_ticksToNextBattle <= 0) {
00407     SetTicksToNextBattle();
00408 
00409     if (m_childState != NULL) {
00410       Log::s_log->Message("Warning: m_childState not null when entering naval battle");
00411       delete m_childState;
00412       m_childState = NULL;
00413     }
00414     m_childState = new StateBattle();
00415     PrepareStateSwitch(m_childState, this);
00416   }
00417 }

void StateSailing::Update ( const unsigned int  ticks  )  [virtual]

Implements IGameState.

Definition at line 184 of file StateSailing.cpp.

References CheckInput(), m_childState, and Timer().

00184                                             {
00185   // Check for child class.  If it is there it means we switched to it and
00186   //   are now back, so it can be deleted.
00187   if (m_childState != NULL) {
00188     delete m_childState;
00189     m_childState = NULL;
00190   }
00191 
00192   // Process incoming events
00193   CheckInput();
00194 
00195   // Change the economy
00196   //Economy::s_economy->Step();
00197 
00198   // Update the scene
00199   Timer(ticks);
00200 }


Member Data Documentation

GLfloat StateSailing::m_ambientLight[4] [private]

Definition at line 99 of file StateSailing.h.

Referenced by StateSailing(), and SwitchTo().

Camera* StateSailing::m_camera [private]

Definition at line 95 of file StateSailing.h.

Referenced by Destroy(), Display(), HandleKeyDown(), Initialize(), and SwitchTo().

GLfloat StateSailing::m_cameraX [private]

Definition at line 96 of file StateSailing.h.

Referenced by HandleKeyDown(), and Initialize().

GLfloat StateSailing::m_cameraY [private]

Definition at line 96 of file StateSailing.h.

Referenced by HandleKeyDown(), and Initialize().

GLfloat StateSailing::m_cameraZ [private]

Definition at line 96 of file StateSailing.h.

Referenced by HandleKeyDown(), and Initialize().

IGameState* StateSailing::m_childState [private]

Definition at line 92 of file StateSailing.h.

Referenced by Destroy(), HandleKeyDown(), StateSailing(), Timer(), and Update().

CityManager* StateSailing::m_cities

Definition at line 57 of file StateSailing.h.

Referenced by Destroy(), Display(), HandleKeyDown(), Initialize(), Map::InitMenu(), and Timer().

GLfloat StateSailing::m_diffuseLight[4] [private]

Definition at line 100 of file StateSailing.h.

Referenced by StateSailing(), and SwitchTo().

EconomyScreen* StateSailing::m_es [private]

Definition at line 104 of file StateSailing.h.

Referenced by Destroy(), HandleKeyDown(), and Initialize().

Terrain* StateSailing::m_land [private]

Definition at line 83 of file StateSailing.h.

Referenced by Display(), HandleKeyDown(), Initialize(), StateSailing(), Timer(), and ~StateSailing().

GLfloat StateSailing::m_lightPosition[4] [private]

Definition at line 101 of file StateSailing.h.

Referenced by StateSailing(), and SwitchTo().

Map* StateSailing::m_map [private]

Definition at line 86 of file StateSailing.h.

Referenced by Destroy(), HandleKeyUp(), and Initialize().

GLfloat StateSailing::m_rotx [private]

Definition at line 80 of file StateSailing.h.

Referenced by Initialize(), and Timer().

int StateSailing::m_ticksToNextBattle [private]

Definition at line 77 of file StateSailing.h.

Referenced by SetTicksToNextBattle(), and Timer().

const string StateSailing::s_mainFontName = "papyrus.png" [static, private]

Definition at line 107 of file StateSailing.h.

Referenced by StateSailing().


The documentation for this class was generated from the following files:
Generated on Mon Jan 8 22:34:15 2007 for CrownandCutlass by  doxygen 1.4.7