author | andyq <andyq> | 2003-01-20 23:11:56 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-20 23:11:56 (UTC) |
commit | 92419b025aa5d82bb7592281af5505ff931c2e58 (patch) (unidiff) | |
tree | 374ee9ca86ef716ce388c8a28e38261f6a04ce7a /noncore/games/sfcave-sdl/sfcave_game.cpp | |
parent | 02090d2e63ad8398c0a8a9f1fb895a9c6e42514b (diff) | |
download | opie-92419b025aa5d82bb7592281af5505ff931c2e58.zip opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.gz opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.bz2 |
Initial Revision
Diffstat (limited to 'noncore/games/sfcave-sdl/sfcave_game.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/sfcave_game.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/sfcave_game.cpp b/noncore/games/sfcave-sdl/sfcave_game.cpp new file mode 100644 index 0000000..72c5ce3 --- a/dev/null +++ b/noncore/games/sfcave-sdl/sfcave_game.cpp | |||
@@ -0,0 +1,167 @@ | |||
1 | #include "SDL_gfxPrimitives.h" | ||
2 | |||
3 | #include "constants.h" | ||
4 | #include "sfcave_game.h" | ||
5 | #include "random.h" | ||
6 | |||
7 | SFCaveGame :: SFCaveGame( SFCave *p, int w, int h, int diff ) | ||
8 | : Game( p, w, h, diff ) | ||
9 | { | ||
10 | gameName = "SFCave"; | ||
11 | difficulty = MENU_DIFFICULTY_EASY; | ||
12 | blockUpdateRate = 200; | ||
13 | |||
14 | terrain = new Terrain( w, h ); | ||
15 | player = new Player( w, h ); | ||
16 | highScore = 0; | ||
17 | } | ||
18 | |||
19 | SFCaveGame :: ~SFCaveGame() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | void SFCaveGame :: init() | ||
24 | { | ||
25 | Game :: init(); | ||
26 | |||
27 | blockDistance = 50; | ||
28 | blockHeight = 80; | ||
29 | blockWidth = 20; | ||
30 | |||
31 | switch( difficulty ) | ||
32 | { | ||
33 | case MENU_DIFFICULTY_EASY: | ||
34 | blockDistance = 50; | ||
35 | break; | ||
36 | case MENU_DIFFICULTY_NORMAL: | ||
37 | blockDistance = 40; | ||
38 | break; | ||
39 | case MENU_DIFFICULTY_HARD: | ||
40 | blockDistance = 30; | ||
41 | break; | ||
42 | } | ||
43 | |||
44 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
45 | blocks[i].y( -1 ); | ||
46 | } | ||
47 | |||
48 | void SFCaveGame :: update( int state ) | ||
49 | { | ||
50 | Game::update( state ); | ||
51 | |||
52 | if ( state == STATE_PLAYING ) | ||
53 | { | ||
54 | if ( nrFrames % 3 == 0 ) | ||
55 | score ++; | ||
56 | |||
57 | if ( nrFrames % 200 == 0 ) | ||
58 | { | ||
59 | if ( terrain->getMaxHeight() < sHeight - 100 ) | ||
60 | { | ||
61 | terrain->increaseMaxHeight( 10 ); | ||
62 | |||
63 | // Reduce block height | ||
64 | if ( terrain->getMaxHeight() > sHeight - 150 ) | ||
65 | blockHeight -= 5; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | if ( checkCollisions() ) | ||
70 | { | ||
71 | // printf( "Crashed!\n" ); | ||
72 | parent->changeState( STATE_CRASHING ); | ||
73 | return; | ||
74 | } | ||
75 | |||
76 | if ( nrFrames % blockDistance == 0 ) | ||
77 | addBlock(); | ||
78 | |||
79 | // Game logic goes here | ||
80 | terrain->moveTerrain( 5 ); | ||
81 | moveBlocks( 5 ); | ||
82 | player->move( press ); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | void SFCaveGame :: draw( SDL_Surface *screen ) | ||
87 | { | ||
88 | Game::preDraw( screen ); | ||
89 | |||
90 | if ( parent->getState() == STATE_PLAYING ) | ||
91 | { | ||
92 | // Screen drawing goes here | ||
93 | terrain->drawTerrain( screen ); | ||
94 | |||
95 | player->draw( screen ); | ||
96 | |||
97 | drawBlocks( screen ); | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | // Screen drawing goes here | ||
102 | terrain->drawTerrain( screen ); | ||
103 | |||
104 | drawBlocks( screen ); | ||
105 | |||
106 | player->draw( screen ); | ||
107 | } | ||
108 | |||
109 | Game::draw( screen ); | ||
110 | } | ||
111 | |||
112 | void SFCaveGame :: addBlock() | ||
113 | { | ||
114 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
115 | { | ||
116 | if ( blocks[i].y() == -1 ) | ||
117 | { | ||
118 | int x = sWidth; | ||
119 | |||
120 | int y = terrain->getMapTop( MAPSIZE-1 ) + (int)(nextInt(terrain->getMapBottom( MAPSIZE-1 ) - terrain->getMapTop( MAPSIZE-1 ) - blockHeight)); | ||
121 | |||
122 | blocks[i].setRect( x, y, blockWidth, blockHeight ); | ||
123 | |||
124 | break; | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | void SFCaveGame :: moveBlocks( int amountToMove ) | ||
130 | { | ||
131 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
132 | { | ||
133 | if ( blocks[i].y() != -1 ) | ||
134 | { | ||
135 | blocks[i].moveBy( -amountToMove, 0 ); | ||
136 | if ( blocks[i].x() + blocks[i].y() < 0 ) | ||
137 | blocks[i].y( -1 ); | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | void SFCaveGame :: drawBlocks( SDL_Surface *screen ) | ||
143 | { | ||
144 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
145 | { | ||
146 | if ( blocks[i].y() != -1 ) | ||
147 | { | ||
148 | SDL_Rect r = blocks[i].getRect(); | ||
149 | SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) ); | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | |||
154 | bool SFCaveGame :: checkCollisions() | ||
155 | { | ||
156 | // Check collisions with blocks | ||
157 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
158 | { | ||
159 | if ( blocks[i].y() != -1 ) | ||
160 | { | ||
161 | if ( blocks[i].intersects( player->getPos() ) ) | ||
162 | return true; | ||
163 | } | ||
164 | } | ||
165 | // Check collision with landscape | ||
166 | return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() ); | ||
167 | } | ||