Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gates_game.cpp

Go to the documentation of this file.
00001 #include <SDL/SDL_gfxPrimitives.h>
00002 
00003 #include "constants.h"
00004 #include "gates_game.h"
00005 #include "random.h"
00006 
00007 GatesGame :: GatesGame( SFCave *p, int w, int h, int diff )
00008         : Game( p, w, h, diff )
00009 {
00010         gameName = "Gates";
00011         difficulty = MENU_DIFFICULTY_EASY;
00012         blockUpdateRate = 200;
00013 
00014         terrain = new Terrain( w, h );
00015         player = new Player( w, h );
00016         highScore = 0;
00017 }
00018 
00019 GatesGame :: ~GatesGame()
00020 {
00021         // terrain and player get deleted by parent class
00022 }
00023 
00024 void GatesGame :: init()
00025 {
00026         blockHeight = 80;
00027         blockWidth = 20;
00028         lastGateBottomY = 0;
00029 
00030         gateDistance = 75;
00031         nextGate = nextInt( 50 ) + gateDistance;
00032         gapHeight = 75;
00033 
00034         switch( difficulty )
00035         {
00036                 case MENU_DIFFICULTY_EASY:
00037                         gapHeight = 75;
00038                         player->setMovementInfo( 0.4, 0.6, 4, 5 );
00039                         break;
00040                 case MENU_DIFFICULTY_NORMAL:
00041                         gapHeight = 50;
00042                         player->setMovementInfo( 0.4, 0.6, 4, 5 );
00043                         break;
00044                 case MENU_DIFFICULTY_HARD:
00045                         gapHeight = 25;
00046                         player->setMovementInfo( 0.6, 0.8, 6, 7 );
00047                         break;
00048         case MENU_DIFFICULTY_CUSTOM:
00049         {
00050             // Read custom difficulty settings for this game
00051             gapHeight = parent->loadIntSetting( "Gates_custom_gapHeight", 75 );
00052         
00053             double thrust = parent->loadDoubleSetting( "Gates_custom_player_thrust", 0.4 );
00054             double gravity = parent->loadDoubleSetting( "Gates_custom_player_gravity", 0.6 );
00055             double maxUp = parent->loadDoubleSetting( "Gates_custom_player_maxupspeed", 4.0 );
00056             double maxDown = parent->loadDoubleSetting( "Gates_custom_player_maxdownspeed", 5.0 );
00057                         player->setMovementInfo( thrust, gravity, maxUp, maxDown );
00058 
00059             break;
00060         }
00061         }
00062 
00063     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00064         blocks[i].y( -1 );
00065 
00066         Game :: init();
00067 }
00068 
00069 void GatesGame ::  update( int state )
00070 {
00071         Game::update( state );
00072 
00073         // Game logic goes here
00074         if ( state == STATE_PLAYING )
00075         {
00076                 if ( nrFrames % 3 == 0 )
00077                         score ++;
00078 
00079                 if ( nrFrames % 500 == 0 )
00080                 {
00081                         if ( gapHeight > 75 )
00082                                 gapHeight -= 5;
00083                 }
00084 
00085                 // Slightly random gap distance
00086                 if ( nrFrames >= nextGate )
00087                 {
00088                         nextGate = nrFrames + nextInt( 50 ) + gateDistance;
00089                         addGate();
00090                 }
00091 
00092                 if ( checkCollisions() )
00093                 {
00094                         parent->changeState( STATE_CRASHING );
00095                         return;
00096                 }
00097 
00098                 terrain->moveTerrain( 5 );
00099                 moveBlocks( 5 );
00100                 player->move( press );
00101         }
00102 }
00103 
00104 void GatesGame :: draw( SDL_Surface *screen )
00105 {
00106         Game::preDraw( screen );
00107 
00108         if ( parent->getState() == STATE_PLAYING )
00109         {
00110                 // Screen drawing goes here
00111                 terrain->drawTerrain( screen );
00112 
00113                 player->draw( screen );
00114 
00115                 drawBlocks( screen );
00116         }
00117         else
00118         {
00119                 // Screen drawing goes here
00120                 terrain->drawTerrain( screen );
00121 
00122                 drawBlocks( screen );
00123 
00124                 player->draw( screen );
00125         }
00126 
00127         Game::draw( screen );
00128 }
00129 
00130 
00131 void GatesGame :: addGate()
00132 {
00133         printf( "gapHeight = %d\n", gapHeight );
00134     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00135     {
00136         if ( blocks[i].y() == -1 )
00137         {
00138             int x1 = sWidth;
00139             int y1 = terrain->getMapTop(50);
00140             int b1Height = nextInt(terrain->getMapBottom( 50 ) - terrain->getMapTop(50) - gapHeight);
00141 
00142             // See if height between last gate and this one is too big
00143             if ( b1Height - 100 > lastGateBottomY )
00144                 b1Height -= 25;
00145             else if ( b1Height + 100 < lastGateBottomY )
00146                 b1Height += 25;
00147             lastGateBottomY = b1Height;
00148 
00149 
00150             int x2 = sWidth;
00151             int y2 = y1 + b1Height + gapHeight;
00152             int b2Height = terrain->getMapBottom( 50 ) - y2;
00153 
00154 
00155             blocks[i].setRect( x1, y1, blockWidth, b1Height );
00156             blocks[i+1].setRect( x2, y2, blockWidth, b2Height );
00157 
00158             break;
00159         }
00160     }
00161 }
00162 
00163 void GatesGame :: moveBlocks( int amountToMove )
00164 {
00165     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00166     {
00167         if ( blocks[i].y() != -1 )
00168         {
00169             blocks[i].moveBy( -amountToMove, 0 );
00170             if ( blocks[i].x() + blocks[i].y() < 0 )
00171                 blocks[i].y( -1 );
00172         }
00173     }
00174 }
00175 
00176 void GatesGame :: drawBlocks( SDL_Surface *screen )
00177 {
00178     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00179         {
00180         if ( blocks[i].y() != -1 )
00181         {
00182                 SDL_Rect r = blocks[i].getRect();
00183                         SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) );
00184         }
00185         }
00186 }
00187 
00188 bool GatesGame :: checkCollisions()
00189 {
00190         // Check collisions with blocks
00191         for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00192     {
00193         if ( blocks[i].y() != -1 )
00194         {
00195             if ( blocks[i].intersects( player->getPos() ) )
00196                 return true;
00197         }
00198     }
00199         // Check collision with landscape
00200         return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() );
00201 }

Generated on Sat Nov 5 16:17:24 2005 for OPIE by  doxygen 1.4.2