git repos / blockattack-game

commit dfdb7d66

sago007 · 2015-11-29 19:21
dfdb7d6655b56730ed3bb9b64a69d587b18b931f patch · browse files
parent 7d604e028a78a16e9d7ab7d94bb2d5763e51d67a

Moved some of the highscore logic out of the blockGame object

Changed files

M source/code/BlockGame.cpp before
M source/code/highscore.cpp before
M source/code/highscore.h before
M source/code/main.cpp before
diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index 0f366ab..4a9c3a0 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp
@@ -1215,13 +1215,6 @@ void BlockGame::PushLineInternal() {
}
if ((TowerHeight>12) && (!puzzleMode)&&(!bGameOver)&&(chain==0)) {
- if ((!vsMode)&&(theTopScoresEndless.isHighScore(score))&&(!AI_Enabled)) {
- EndlessHighscoreEvent();
- theTopScoresEndless.addScore(name, score);
- if (verboseLevel) {
- cout << "New high score!" << endl;
- }
- }
SetGameOver();
}
@@ -1840,18 +1833,6 @@ void BlockGame::Update() {
if ((timetrial) && (!bGameOver) && (nowTime>gameStartedAt+2*60*1000)) {
SetGameOver();
TimeTrialEndEvent();
- if ((theTopScoresTimeTrial.isHighScore(score))&&(!AI_Enabled)) {
- theTopScoresTimeTrial.addScore(name, score);
- //new highscore
- //Also check if it is better than the best result so far.
- string checkFilename = getPathToSaveFiles() + "/bestTTresult";
- ifstream inFile(checkFilename.c_str());
- Uint32 bestResult = 3000; //Never accept a best result under 3000
- if (inFile) {
- inFile >> bestResult;
- inFile.close();
- }
- }
}
}
}
diff --git a/source/code/highscore.cpp b/source/code/highscore.cpp index 513fcc2..9296673 100644 --- a/source/code/highscore.cpp +++ b/source/code/highscore.cpp
@@ -141,16 +141,14 @@ bool Highscore::isHighScore(int newScore) {
void Highscore::addScore(const string& newName, int newScore) {
int ranking = top-1;
- while ((tabel[ranking-1].score<newScore) && (ranking != 0)) {
+ while ( (ranking != 0) && (tabel[ranking-1].score<newScore)) {
ranking--;
}
for (int i=top-1; i>ranking; i--) {
tabel[i].score = tabel[i-1].score;
- strcpy(tabel[i].name," \0");
strcpy(tabel[i].name,tabel[i-1].name);
}
tabel[ranking].score = newScore;
- strcpy(tabel[ranking].name," \0");
snprintf(tabel[ranking].name, sizeof(tabel[ranking].name), "%s", newName.c_str());
Highscore::writeFile();
}
diff --git a/source/code/highscore.h b/source/code/highscore.h index 5e241df..5de15c2 100644 --- a/source/code/highscore.h +++ b/source/code/highscore.h
@@ -44,6 +44,7 @@ struct record
int score;
};
+
class Highscore
{
private:
diff --git a/source/code/main.cpp b/source/code/main.cpp index 653168e..d457dc4 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -2502,6 +2502,10 @@ void changePuzzleLevels() {
static BlockGameSdl* player1;
static BlockGameSdl* player2;
+static bool registerEndlessHighscore = false;
+static bool registerTTHighscorePlayer1 = false;
+static bool registerTTHighscorePlayer2 = false;
+
static void StartSinglePlayerEndless() {
//1 player - endless
player1->NewGame(SDL_GetTicks());
@@ -2510,6 +2514,7 @@ static void StartSinglePlayerEndless() {
player2->SetGameOver();
player1->name = player1name;
player2->name = player2name;
+ registerEndlessHighscore = true;
}
static void StartSinglePlayerTimeTrial() {
@@ -2519,6 +2524,7 @@ static void StartSinglePlayerTimeTrial() {
//vsMode = false;
player1->name = player1name;
player2->name = player2name;
+ registerTTHighscorePlayer1 = true;
}
static int StartSinglePlayerPuzzle(int level) {
@@ -2549,11 +2555,15 @@ static void StarTwoPlayerTimeTrial() {
player2->setGameSpeed(player2Speed);
player1->setHandicap(player1handicap);
player2->setHandicap(player2handicap);
+ registerTTHighscorePlayer1 = true;
+ registerTTHighscorePlayer2 = true;
if (player1AI) {
player1->setAIlevel(player1AIlevel);
+ registerTTHighscorePlayer1 = false;
}
if (player2AI) {
player2->setAIlevel(player2AIlevel);
+ registerTTHighscorePlayer2 = false;
}
player1->name = player1name;
player2->name = player2name;
@@ -3121,9 +3131,13 @@ int runGame(int gametype, int level) {
bool mustsetupgame = true;
+
while (done == 0) {
if (mustsetupgame) {
+ registerEndlessHighscore = false;
+ registerTTHighscorePlayer1 = false;
+ registerTTHighscorePlayer2 = false;
switch (gametype) {
case 1:
StartSinglePlayerTimeTrial();
@@ -3743,6 +3757,20 @@ int runGame(int gametype, int level) {
//twoPlayers = false;
}
}
+
+ if (theGame.isGameOver() && registerTTHighscorePlayer1) {
+ registerTTHighscorePlayer1 = false;
+ theTopScoresTimeTrial.addScore(theGame.name, theGame.GetScore());
+ }
+ if (theGame2.isGameOver() && registerTTHighscorePlayer2) {
+ registerTTHighscorePlayer2 = false;
+ theTopScoresTimeTrial.addScore(theGame2.name, theGame2.GetScore());
+ }
+ if (theGame.isGameOver() && registerEndlessHighscore) {
+ registerEndlessHighscore = false;
+ theTopScoresEndless.addScore(theGame.name, theGame.GetScore());
+ theGame.EndlessHighscoreEvent();
+ }
//Once evrything has been checked, update graphics
DrawEverything(xsize,ysize,&theGame,&theGame2);