Diffstat (limited to 'noncore/games/sfcave-sdl/gates_game.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/gates_game.cpp | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/gates_game.cpp b/noncore/games/sfcave-sdl/gates_game.cpp new file mode 100644 index 0000000..1a9bc89 --- a/dev/null +++ b/noncore/games/sfcave-sdl/gates_game.cpp | |||
@@ -0,0 +1,188 @@ | |||
1 | #include "SDL_gfxPrimitives.h" | ||
2 | |||
3 | #include "constants.h" | ||
4 | #include "gates_game.h" | ||
5 | #include "random.h" | ||
6 | |||
7 | GatesGame :: GatesGame( SFCave *p, int w, int h, int diff ) | ||
8 | : Game( p, w, h, diff ) | ||
9 | { | ||
10 | gameName = "Gates"; | ||
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 | GatesGame :: ~GatesGame() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | void GatesGame :: init() | ||
24 | { | ||
25 | Game :: init(); | ||
26 | |||
27 | blockHeight = 80; | ||
28 | blockWidth = 20; | ||
29 | lastGateBottomY = 0; | ||
30 | |||
31 | gateDistance = 75; | ||
32 | nextGate = nextInt( 50 ) + gateDistance; | ||
33 | gapHeight = 75; | ||
34 | |||
35 | switch( difficulty ) | ||
36 | { | ||
37 | case MENU_DIFFICULTY_EASY: | ||
38 | gapHeight = 75; | ||
39 | player->setMovementInfo( 0.4, 0.6, 4, 5 ); | ||
40 | break; | ||
41 | case MENU_DIFFICULTY_NORMAL: | ||
42 | gapHeight = 50; | ||
43 | player->setMovementInfo( 0.4, 0.6, 4, 5 ); | ||
44 | break; | ||
45 | case MENU_DIFFICULTY_HARD: | ||
46 | gapHeight = 25; | ||
47 | player->setMovementInfo( 0.6, 0.8, 6, 7 ); | ||
48 | break; | ||
49 | } | ||
50 | |||
51 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
52 | blocks[i].y( -1 ); | ||
53 | } | ||
54 | |||
55 | void GatesGame :: update( int state ) | ||
56 | { | ||
57 | Game::update( state ); | ||
58 | |||
59 | // Game logic goes here | ||
60 | if ( state == STATE_PLAYING ) | ||
61 | { | ||
62 | if ( nrFrames % 3 == 0 ) | ||
63 | score ++; | ||
64 | |||
65 | if ( nrFrames % 500 == 0 ) | ||
66 | { | ||
67 | if ( gapHeight > 75 ) | ||
68 | gapHeight -= 5; | ||
69 | } | ||
70 | |||
71 | // Slightly random gap distance | ||
72 | if ( nrFrames >= nextGate ) | ||
73 | { | ||
74 | nextGate = nrFrames + nextInt( 50 ) + gateDistance; | ||
75 | addGate(); | ||
76 | } | ||
77 | |||
78 | if ( checkCollisions() ) | ||
79 | { | ||
80 | // printf( "Crashed!\n" ); | ||
81 | parent->changeState( STATE_CRASHING ); | ||
82 | return; | ||
83 | } | ||
84 | |||
85 | terrain->moveTerrain( 5 ); | ||
86 | moveBlocks( 5 ); | ||
87 | player->move( press ); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | void GatesGame :: draw( SDL_Surface *screen ) | ||
92 | { | ||
93 | Game::preDraw( screen ); | ||
94 | |||
95 | if ( parent->getState() == STATE_PLAYING ) | ||
96 | { | ||
97 | // Screen drawing goes here | ||
98 | terrain->drawTerrain( screen ); | ||
99 | |||
100 | player->draw( screen ); | ||
101 | |||
102 | drawBlocks( screen ); | ||
103 | } | ||
104 | else | ||
105 | { | ||
106 | // Screen drawing goes here | ||
107 | terrain->drawTerrain( screen ); | ||
108 | |||
109 | drawBlocks( screen ); | ||
110 | |||
111 | player->draw( screen ); | ||
112 | } | ||
113 | |||
114 | Game::draw( screen ); | ||
115 | } | ||
116 | |||
117 | |||
118 | void GatesGame :: addGate() | ||
119 | { | ||
120 | printf( "gapHeight = %d\n", gapHeight ); | ||
121 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
122 | { | ||
123 | if ( blocks[i].y() == -1 ) | ||
124 | { | ||
125 | int x1 = sWidth; | ||
126 | int y1 = terrain->getMapTop(50); | ||
127 | int b1Height = nextInt(terrain->getMapBottom( 50 ) - terrain->getMapTop(50) - gapHeight); | ||
128 | |||
129 | // See if height between last gate and this one is too big | ||
130 | if ( b1Height - 100 > lastGateBottomY ) | ||
131 | b1Height -= 25; | ||
132 | else if ( b1Height + 100 < lastGateBottomY ) | ||
133 | b1Height += 25; | ||
134 | lastGateBottomY = b1Height; | ||
135 | |||
136 | |||
137 | int x2 = sWidth; | ||
138 | int y2 = y1 + b1Height + gapHeight; | ||
139 | int b2Height = terrain->getMapBottom( 50 ) - y2; | ||
140 | |||
141 | |||
142 | blocks[i].setRect( x1, y1, blockWidth, b1Height ); | ||
143 | blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); | ||
144 | |||
145 | break; | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | |||
150 | void GatesGame :: moveBlocks( int amountToMove ) | ||
151 | { | ||
152 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
153 | { | ||
154 | if ( blocks[i].y() != -1 ) | ||
155 | { | ||
156 | blocks[i].moveBy( -amountToMove, 0 ); | ||
157 | if ( blocks[i].x() + blocks[i].y() < 0 ) | ||
158 | blocks[i].y( -1 ); | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | void GatesGame :: drawBlocks( SDL_Surface *screen ) | ||
164 | { | ||
165 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
166 | { | ||
167 | if ( blocks[i].y() != -1 ) | ||
168 | { | ||
169 | SDL_Rect r = blocks[i].getRect(); | ||
170 | SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) ); | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | |||
175 | bool GatesGame :: checkCollisions() | ||
176 | { | ||
177 | // Check collisions with blocks | ||
178 | for ( int i = 0 ; i < BLOCKSIZE ; ++i ) | ||
179 | { | ||
180 | if ( blocks[i].y() != -1 ) | ||
181 | { | ||
182 | if ( blocks[i].intersects( player->getPos() ) ) | ||
183 | return true; | ||
184 | } | ||
185 | } | ||
186 | // Check collision with landscape | ||
187 | return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() ); | ||
188 | } | ||