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

sfcave_game.cpp

Go to the documentation of this file.
00001 #include <SDL/SDL_gfxPrimitives.h>
00002 
00003 #include "constants.h"
00004 #include "sfcave_game.h"
00005 #include "random.h"
00006 
00007 SFCaveGame :: SFCaveGame( SFCave *p, int w, int h, int diff )
00008         : Game( p, w, h, diff )
00009 {
00010         gameName = "SFCave";
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 SFCaveGame :: ~SFCaveGame()
00020 {
00021 }
00022 
00023 void SFCaveGame :: init()
00024 {
00025         blockDistance = 50;
00026         blockHeight = 80;
00027         blockWidth = 20;
00028 
00029         switch( difficulty )
00030         {
00031                 case MENU_DIFFICULTY_EASY:
00032                         blockDistance = 50;
00033                         break;
00034                 case MENU_DIFFICULTY_NORMAL:
00035                         blockDistance = 40;
00036                         break;
00037                 case MENU_DIFFICULTY_HARD:
00038                         blockDistance = 30;
00039                         break;
00040         case MENU_DIFFICULTY_CUSTOM:
00041         {
00042             // Read custom difficulty settings for this game
00043             blockDistance = parent->loadIntSetting( "SFCave_custom_blockdistance", 50 );
00044 
00045             double thrust = parent->loadDoubleSetting( "SFCave_custom_player_thrust", 0.4 );
00046             double gravity = parent->loadDoubleSetting( "SFCave_custom_player_gravity", 0.6 );
00047             double maxUp = parent->loadDoubleSetting( "SFCave_custom_player_maxupspeed", 4.0 );
00048             double maxDown = parent->loadDoubleSetting( "SFCave_custom_player_maxdownspeed", 5.0 );
00049                         player->setMovementInfo( thrust, gravity, maxUp, maxDown );
00050 
00051             break;
00052         }
00053         }
00054 
00055     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00056         blocks[i].y( -1 );
00057 
00058         Game :: init();
00059 }
00060 
00061 void SFCaveGame ::  update( int state )
00062 {
00063         Game::update( state );
00064 
00065         if ( state == STATE_PLAYING )
00066         {
00067                 if ( nrFrames % 3 == 0 )
00068                         score ++;
00069 
00070                 if ( nrFrames % 200 == 0 )
00071                 {
00072                         if ( terrain->getMaxHeight() < sHeight - 100 )
00073                         {
00074                                 terrain->increaseMaxHeight( 10 );
00075 
00076                                 // Reduce block height
00077                                 if ( terrain->getMaxHeight() > sHeight - 150 )
00078                                         blockHeight -= 5;
00079                         }
00080                 }
00081 
00082                 if ( checkCollisions() )
00083                 {
00084                         parent->changeState( STATE_CRASHING );
00085                         return;
00086                 }
00087 
00088                 if ( nrFrames % blockDistance == 0 )
00089                         addBlock();
00090 
00091                 // Game logic goes here
00092                 terrain->moveTerrain( 5 );
00093                 moveBlocks( 5 );
00094                 player->move( press );
00095         }
00096 }
00097 
00098 void SFCaveGame :: draw( SDL_Surface *screen )
00099 {
00100         Game::preDraw( screen );
00101 
00102         if ( parent->getState() == STATE_PLAYING )
00103         {
00104                 // Screen drawing goes here
00105                 terrain->drawTerrain( screen );
00106 
00107                 player->draw( screen );
00108 
00109                 drawBlocks( screen );
00110         }
00111         else
00112         {
00113                 // Screen drawing goes here
00114                 terrain->drawTerrain( screen );
00115 
00116                 drawBlocks( screen );
00117 
00118                 player->draw( screen );
00119         }
00120 
00121         Game::draw( screen );
00122 }
00123 
00124 void SFCaveGame :: addBlock()
00125 {
00126     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00127     {
00128         if ( blocks[i].y() == -1 )
00129         {
00130             int x = sWidth;
00131 
00132             int y = terrain->getMapTop( MAPSIZE-1 ) + (int)(nextInt(terrain->getMapBottom( MAPSIZE-1 ) - terrain->getMapTop( MAPSIZE-1 ) - blockHeight));
00133 
00134             blocks[i].setRect( x, y, blockWidth, blockHeight );
00135 
00136             break;
00137         }
00138     }
00139 }
00140 
00141 void SFCaveGame :: moveBlocks( int amountToMove )
00142 {
00143     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00144     {
00145         if ( blocks[i].y() != -1 )
00146         {
00147             blocks[i].moveBy( -amountToMove, 0 );
00148             if ( blocks[i].x() + blocks[i].y() < 0 )
00149                 blocks[i].y( -1 );
00150         }
00151     }
00152 }
00153 
00154 void SFCaveGame :: drawBlocks( SDL_Surface *screen )
00155 {
00156     for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00157         {
00158         if ( blocks[i].y() != -1 )
00159         {
00160                 SDL_Rect r = blocks[i].getRect();
00161                         SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) );
00162         }
00163         }
00164 }
00165 
00166 bool SFCaveGame :: checkCollisions()
00167 {
00168         // Check collisions with blocks
00169         for ( int i = 0 ; i < BLOCKSIZE ; ++i )
00170     {
00171         if ( blocks[i].y() != -1 )
00172         {
00173             if ( blocks[i].intersects( player->getPos() ) )
00174                 return true;
00175         }
00176     }
00177         // Check collision with landscape
00178         return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() );
00179 }

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