1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/*-----------------------------------------------------------------------------
Copyright 2007 Milan Babuskov
This file is part of Vodovod
Vodovod is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Vodovod is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vodovod in file COPYING; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-----------------------------------------------------------------------------*/
#include <string>
#include <sstream>
#include "keys.h"
#include "sutils.h"
#include "hiscore.h"
#include "config.h"
#include "game.h"
#include "resource.h"
extern SDL_Surface *Screen; // global objects
extern ResourceManager *Data;
//-----------------------------------------------------------------------------
Game::Game(int difficulty):
fontM(DATADIR "/font-white.bmp", 8, 14), mapM(13, 7, 10, 110)
{
difficultyM = difficulty;
SetupGame();
SetupLevel();
Play();
CheckHiscore();
}
//-----------------------------------------------------------------------------
void Game::ClearQueue()
{
while (!queueM.empty())
{
Shape *s = queueM.front();
delete s;
queueM.pop_front();
}
}
//-----------------------------------------------------------------------------
Game::~Game()
{
ClearQueue();
}
//-----------------------------------------------------------------------------
void Game::SetupGame()
{
currentLevelM = 0;
pointsM = 0;
}
//-----------------------------------------------------------------------------
void Game::AddShapeToQueue()
{
const char shapes[] = "LJF7-+|";
Shape *s = new RegularShape(shapes[NjamRandom(7)]);
queueM.push_back(s);
}
//-----------------------------------------------------------------------------
void Game::SetupLevel()
{
CheckMusic(Data->getMusic("walk"));
// rnd
Uint32 x = (SDL_GetTicks() % 1000);
while (x-- > 0)
NjamRandom(1);
floodM = false;
currentLevelM++;
pipesLeftM = 210 - (difficultyM+1)*currentLevelM*10;
if (pipesLeftM < 86)
pipesLeftM = 86;
// clean up field, flowDelay = -200
mapM.Setup(currentLevelM, -1000 + difficultyM*200);
ClearQueue();
for (int i=0; i<6; i++)
AddShapeToQueue();
char buffer[20];
sprintf(buffer, "LEVEL %d", currentLevelM);
TextEffect *t = new TextEffect(&fontM, 350, 260, std::string(buffer), 0);
specialEffects.addEffect(t);
}
//-----------------------------------------------------------------------------
bool Game::Flow()
{
int res = mapM.AdvanceFlow();
if (mapM.LevelComplete())
{
if (Mix_PlayingMusic())
Mix_HaltMusic();
PlaySound("levelend");
int pts = 100 + pipesLeftM*difficultyM;
pointsM += pts;
specialEffects.addEffect(new ScoreEffect(&fontM, 80, 560, pts));
TextEffect *t1 = new TextEffect(&fontM, 351, 240,
std::string("LEVEL COMPLETE"), -1);
TextEffect *t2 = new TextEffect(&fontM, 357, 255,
std::string("PRESS ANY KEY"), -1);
specialEffects.addEffect(t1);
specialEffects.addEffect(t2);
while (NjamGetch(false) != SDLK_LAST)
; // clean up event queue
while (true)
{
Uint32 startTime = SDL_GetTicks();
RenderScene();
if (NjamGetch(false) != SDLK_LAST)
break;
while (startTime + 34 > SDL_GetTicks())
SDL_Delay(1); // target about 30 fps
}
specialEffects.removeEffect(t1);
specialEffects.removeEffect(t2);
SetupLevel();
return true;
}
if (res == -1)
return false;
if (res > 0)
{
int x, y;
mapM.GetPos(x, y);
specialEffects.addEffect(new ScoreEffect(&fontM, x, y, res));
pointsM += res;
if (res == 30) // slow gives 30 pts
specialEffects.addEffect(new TextEffect(&fontM, x-25, y, "SLOW", 0));
}
return true;
}
//-----------------------------------------------------------------------------
void Game::Play()
{
while (true) // check for single player
{
Uint32 startTime = SDL_GetTicks();
int times = (floodM ? 10 : 1);
bool ok;
while (times--)
{
ok = (ProcessKeyboard() && Flow());
if (!ok)
break;
}
RenderScene();
CheckMusic(0);
if (!ok)
return;
while (startTime + 34 > SDL_GetTicks())
SDL_Delay(1); // target about 30 fps
}
}
//-----------------------------------------------------------------------------
void Game::RenderScene(bool flip)
{
mapM.Render();
if (queueM.begin() != queueM.end())
mapM.RenderNextShape(*(queueM.begin()));
SDL_Rect r;
SDL_Surface *next = Data->getGfx("next", DATADIR "/next.png");
NjamSetRect(r, 363, 533);
SDL_BlitSurface(next, 0, Screen, &r);
SDL_Surface *queue = Data->getGfx("queue");
NjamSetRect(r, 424, 532);
SDL_BlitSurface(queue, 0, Screen, &r);
int i = 0;
for (std::deque<Shape *>::iterator it = queueM.begin();
it != queueM.end(); ++it, ++i)
{
(*it)->Render(i*60+427, 534);
NjamSetRect(r, i*60+427, 534, 61, 60);
Box(Screen, r, 1, 75, 75, 75);
}
char buffer[30];
sprintf(buffer, "LEVEL: %8d", currentLevelM);
fontM.WriteTextXY(Screen, 185, 540, buffer);
sprintf(buffer, "POINTS: %8d", pointsM);
fontM.WriteTextXY(Screen, 185, 555, buffer);
sprintf(buffer, "PIPES LEFT: %4d", pipesLeftM);
fontM.WriteTextXY(Screen, 185, 570, buffer);
specialEffects.Render();
if (flip)
SDL_Flip(Screen);
}
//-----------------------------------------------------------------------------
bool Game::ProcessKeyboard()
{
SDLKey key;
bool repeatKeys = true;
bool pause = false;
if ((SDL_GetAppState() & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)) != (SDL_APPINPUTFOCUS | SDL_APPACTIVE))
pause = true;
// process single keys
while ((key = kbdM.update(repeatKeys)) != SDLK_LAST)
{
repeatKeys = false; // so we don't influence the repeat-delay
if (key == SDLK_ESCAPE)
return false;
if (key == SDLK_p)
pause = true;
if (key == getKey(taUp))
mapM.MoveCursor( 0, -1);
else if (key == getKey(taRight))
mapM.MoveCursor( 1, 0);
else if (key == getKey(taLeft))
mapM.MoveCursor(-1, 0);
else if (key == getKey(taDown))
mapM.MoveCursor( 0, 1);
else if (key == getKey(taDrop))
Drop();
else if (key == getKey(taFlood))
floodM = true;
}
// process sticky keys (those that can be held down)
if (kbdM.isKeyDown(getKey(taDown)))
mapM.MoveCursor( 0, 1);
if (kbdM.isKeyDown(getKey(taUp)))
mapM.MoveCursor( 0, -1);
if (kbdM.isKeyDown(getKey(taRight)))
mapM.MoveCursor( 1, 0);
if (kbdM.isKeyDown(getKey(taLeft)))
mapM.MoveCursor(-1, 0);
if (pause)
{
while (NjamGetch(false) != SDLK_LAST); // wait to release the key
TextEffect *t = new TextEffect(&fontM, 350, 240,
std::string("GAME PAUSED"), -1);
specialEffects.addEffect(t);
while (SDLK_p != NjamGetch(false)) // wait to press the key again
{
RenderScene();
SDL_Delay(5);
}
specialEffects.removeEffect(t);
}
return true;
}
//-----------------------------------------------------------------------------
void Game::Drop()
{
// drop shape from queue to map if possible, change score
if (queueM.empty()) // no more pipes
{
PlaySound("metal");
return;
}
Shape *s = queueM.front();
if (mapM.PutShape(s))
{
queueM.pop_front();
if (pipesLeftM > 0)
pipesLeftM--;
if (pipesLeftM > 5)
AddShapeToQueue();
}
}
//-----------------------------------------------------------------------------
void Game::CheckHiscore()
{
if (Mix_PlayingMusic())
Mix_HaltMusic();
HiScores &hs = hiScores();
if (!hs.canEnter(pointsM))
{
SDL_Delay(300);
PlaySound("bubbles");
SDL_Delay(300);
Message("GAME OVER * PRESS ANY KEY");
return;
}
else
PlaySound("levelend");
// ask for name
char name[30];
getName(name);
hs.addHiscore(name, currentLevelM, pointsM);
}
//-----------------------------------------------------------------------------
void Game::getName(char *name)
{
NjamFont *font = &fontM;
Uint32 white = SDL_MapRGB(Screen->format, 255, 255, 255);
Uint32 back = SDL_MapRGB(Screen->format, 100, 50, 0);
Uint32 t = SDL_GetTicks();
bool CursorOn = true;
const int namelen = 19;
const int yp = 220;
int fieldw = font->GetCharWidth() * (namelen+2);
int xp = (800 - fieldw)/2;
int textw = font->GetCharWidth() * namelen;
int fieldh = (int)((double)font->GetCharHeight() * 3.5);
int textx = xp+font->GetCharWidth();
int texty = yp+font->GetCharHeight()*2;
name[0] = '\0';
while (true)
{
SDL_Rect dest;
NjamSetRect(dest, xp, yp, fieldw, fieldh);
SDL_FillRect(Screen, &dest, 0);
NjamSetRect(dest, xp+1, yp+1, fieldw-2, fieldh-2);
SDL_FillRect(Screen, &dest, back);
font->WriteTextXY(Screen, textx, yp+font->GetCharHeight()/2, "*** NEW HISCORE ***");
NjamSetRect(dest, textx, texty, textw, font->GetCharHeight()+1);
SDL_FillRect(Screen, &dest, 0);
font->WriteTextXY(Screen, textx+1, texty+1, name);
int l = 0; // get len
while (name[l])
l++;
if (SDL_GetTicks() > t + 400) // make the cursor blink
{
t = SDL_GetTicks();
CursorOn = !CursorOn;
}
if (CursorOn)
{
NjamSetRect(dest, textx + font->GetCharWidth() * l + 2, texty+2, font->GetCharWidth()-2,
font->GetCharHeight()-2); // draw cursor
SDL_FillRect(Screen, &dest, white);
}
SDL_Flip(Screen);
SDLKey key = NjamGetch(false);
char allowed[] = "0123456789abcdefghijklmnopqrstuvwxyz. :)(!*";
bool ok = false;
for (int k=0; allowed[k]; k++)
if (allowed[k] == key)
ok = true;
if (ok && l < 20)
{
char c = key;
if (c >= 'a' && c <= 'z')
c -= ('a' - 'A');
name[l] = c;
name[l+1] = '\0';
}
if (key == SDLK_BACKSPACE && l > 0) // backspace
name[l-1] = '\0';
if ((key == SDLK_KP_ENTER || key == SDLK_RETURN) && name[0]) // entered
break;
}
}
//-----------------------------------------------------------------------------
void Game::Message(const std::string& text, bool wait)
{
if (wait)
while (NjamGetch(false) != SDLK_LAST); // clear the buffer
const int screenw = 800;
int l = text.length();
int w = fontM.GetCharWidth();
// render background
SDL_Rect dest;
NjamSetRect(dest, (screenw-l*w-50)/2, 245, l*w+50, 55);
SDL_FillRect(Screen, &dest, 0);
Uint32 FillColor = SDL_MapRGB(Screen->format, 255, 200, 0);
NjamSetRect(dest, (screenw-l*w-50)/2 + 1, 246, l*w+48, 53);
SDL_FillRect(Screen, &dest, FillColor);
NjamSetRect(dest, (screenw-l*w-30)/2, 255, l*w+30, 35);
SDL_FillRect(Screen, &dest, 0);
fontM.WriteTextXY(Screen, (screenw-l*w)/2, 265, text.c_str());
SDL_Flip(Screen);
if (wait)
NjamGetch(true);
}
//-----------------------------------------------------------------------------
|