[Orxonox-commit 4587] r9258 - code/trunk/src/modules/pong
landauf at orxonox.net
landauf at orxonox.net
Mon May 28 14:25:04 CEST 2012
Author: landauf
Date: 2012-05-28 14:25:03 +0200 (Mon, 28 May 2012)
New Revision: 9258
Modified:
code/trunk/src/modules/pong/Pong.cc
code/trunk/src/modules/pong/Pong.h
code/trunk/src/modules/pong/PongScore.cc
code/trunk/src/modules/pong/PongScore.h
Log:
some cleanup
Modified: code/trunk/src/modules/pong/Pong.cc
===================================================================
--- code/trunk/src/modules/pong/Pong.cc 2012-05-28 10:28:44 UTC (rev 9257)
+++ code/trunk/src/modules/pong/Pong.cc 2012-05-28 12:25:03 UTC (rev 9258)
@@ -75,8 +75,9 @@
// Set the type of Bots for this particular Gametype.
this->botclass_ = Class(PongBot);
- this->scoreLimit_ = 10;
- this->setConfigValues();
+
+ this->scoreLimit_ = 10;
+ this->setConfigValues();
}
/**
@@ -89,6 +90,11 @@
this->cleanup();
}
+ void Pong::setConfigValues()
+ {
+ SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins.");
+ }
+
/**
@brief
Cleans up the Gametype by destroying the ball and the bats.
@@ -282,28 +288,21 @@
this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
}
- // If a palyer gets 21 points, he won the game -> end of game
-
- PlayerInfo* player1 = this->getLeftPlayer();
- PlayerInfo* player2 = this->getRightPlayer();
- if(player1==NULL||player2==NULL) return; //safety
- if(this->getScore(player1) >= scoreLimit_)
+ // If a player gets enough points, he won the game -> end of game
+ PlayerInfo* winningPlayer = NULL;
+ if (this->getLeftPlayer() && this->getScore(this->getLeftPlayer()) >= scoreLimit_)
+ winningPlayer = this->getLeftPlayer();
+ else if (this->getRightPlayer() && this->getScore(this->getRightPlayer()) >= scoreLimit_)
+ winningPlayer = this->getRightPlayer();
+
+ if (winningPlayer)
{
- std::string name1=player1->getName();
- std::string message(name1 + " has won!");
- ChatManager::message(message);
- this->end();
- }
- else if(this->getScore(player2) >= scoreLimit_)
- {
- std::string name2=player2->getName();
- std::string message2(name2 + " has won!");
- ChatManager::message(message2);
+ ChatManager::message(winningPlayer->getName() + " has won!");
this->end();
}
+
// Restart the timer to start the ball.
this->starttimer_.startTimer();
-
}
/**
@@ -343,13 +342,4 @@
else
return 0;
}
-
- /**
- @brief
- Make scoreLimit_ configurable e.g. in the menu.
- */
- void Pong::setConfigValues()
- {
- SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins.");
- }
}
Modified: code/trunk/src/modules/pong/Pong.h
===================================================================
--- code/trunk/src/modules/pong/Pong.h 2012-05-28 10:28:44 UTC (rev 9257)
+++ code/trunk/src/modules/pong/Pong.h 2012-05-28 12:25:03 UTC (rev 9258)
@@ -81,8 +81,8 @@
void setCenterpoint(PongCenterpoint* center)
{ this->center_ = center; }
void setConfigValues(); //!< Makes scoreLimit configurable.
-
- PlayerInfo* getLeftPlayer() const; //!< Get the left player.
+
+ PlayerInfo* getLeftPlayer() const; //!< Get the left player.
PlayerInfo* getRightPlayer() const; //!< Get the right player.
protected:
@@ -94,7 +94,7 @@
WeakPtr<PongCenterpoint> center_; //!< The playing field.
WeakPtr<PongBall> ball_; //!< The Pong ball.
WeakPtr<PongBat> bat_[2]; //!< The two bats.
- Timer starttimer_; //!< A timer to delay the start of the game.
+ Timer starttimer_; //!< A timer to delay the start of the game.
int scoreLimit_; //!< If a player scored that much points, the game is ended.
};
}
Modified: code/trunk/src/modules/pong/PongScore.cc
===================================================================
--- code/trunk/src/modules/pong/PongScore.cc 2012-05-28 10:28:44 UTC (rev 9257)
+++ code/trunk/src/modules/pong/PongScore.cc 2012-05-28 12:25:03 UTC (rev 9258)
@@ -60,7 +60,7 @@
this->bShowLeftPlayer_ = false;
this->bShowRightPlayer_ = false;
this->player1_ = NULL;
- this->player2_ = NULL;
+ this->player2_ = NULL;
}
/**
@@ -99,50 +99,46 @@
// If the owner is set. The owner being a Pong game.
if (this->owner_ != NULL)
{
- if(!this->owner_->hasEnded())
+ if (!this->owner_->hasEnded())
{
- //get the two players
+ // Get the two players.
player1_ = this->owner_->getLeftPlayer();
player2_ = this->owner_->getRightPlayer();
}
- if(this->owner_->hasStarted())
- {
- // Get the two players.
+ std::string name1;
+ std::string name2;
- std::string name1;
- std::string name2;
+ std::string score1("0");
+ std::string score2("0");
- std::string score1("0");
- std::string score2("0");
+ // Save the name and score of each player as a string.
+ if (player1_ != NULL)
+ {
+ name1 = player1_->getName();
+ score1 = multi_cast<std::string>(this->owner_->getScore(player1_));
+ }
+ if (player2_ != NULL)
+ {
+ name2 = player2_->getName();
+ score2 = multi_cast<std::string>(this->owner_->getScore(player2_));
+ }
- // Save the name and score of each player as a string.
- if (player1_ != NULL)
- {
- name1 = player1_->getName();
- score1 = multi_cast<std::string>(this->owner_->getScore(player1_));
- }
- if (player2_ != NULL)
- {
- name2 = player2_->getName();
- score2 = multi_cast<std::string>(this->owner_->getScore(player2_));
- }
+ // Assemble the strings, depending on what should all be displayed.
+ std::string output1;
+ if (this->bShowLeftPlayer_)
+ {
+ if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
+ output1 = name1 + " - " + score1;
+ else if (this->bShowScore_)
+ output1 = score1;
+ else if (this->bShowName_)
+ output1 = name1;
+ }
- // Assemble the strings, depending on what should all be displayed.
- std::string output1;
- if (this->bShowLeftPlayer_)
- {
- if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
- output1 = name1 + " - " + score1;
- else if (this->bShowScore_)
- output1 = score1;
- else if (this->bShowName_)
- output1 = name1;
- }
-
- std::string output2;
- if (this->bShowRightPlayer_)
- {
+ std::string output2;
+ if (this->bShowRightPlayer_)
+ {
if (this->bShowName_ && this->bShowScore_ && player2_ != NULL)
output2 = score2 + " - " + name2;
else if (this->bShowScore_)
@@ -151,16 +147,16 @@
output2 = name2;
}
- std::string output("PONG");
- if (this->bShowName_ || this->bShowScore_)
- {
- if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
- output = output1 + ':' + output2;
- else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
- output = output1 + output2;
- }
- this->setCaption(output);
+ std::string output("PONG");
+ if (this->bShowName_ || this->bShowScore_)
+ {
+ if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
+ output = output1 + ':' + output2;
+ else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
+ output = output1 + output2;
}
+
+ this->setCaption(output);
}
}
Modified: code/trunk/src/modules/pong/PongScore.h
===================================================================
--- code/trunk/src/modules/pong/PongScore.h 2012-05-28 10:28:44 UTC (rev 9257)
+++ code/trunk/src/modules/pong/PongScore.h 2012-05-28 12:25:03 UTC (rev 9258)
@@ -121,8 +121,8 @@
bool bShowScore_; //!< Whether the score of the players is shown.
bool bShowLeftPlayer_; //!< Whether the left player is shown.
bool bShowRightPlayer_; //!< Whether the right player is shown.
- PlayerInfo* player1_; //!< Store information about left player permanently.
- PlayerInfo* player2_; //!< Same for the right player. To end the game properly.
+ WeakPtr<PlayerInfo> player1_; //!< Store information about left player permanently.
+ WeakPtr<PlayerInfo> player2_; //!< Same for the right player. To end the game properly.
};
}
#endif /* _PongScore_H__ */
More information about the Orxonox-commit
mailing list