Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9258 for code


Ignore:
Timestamp:
May 28, 2012, 2:25:03 PM (12 years ago)
Author:
landauf
Message:

some cleanup

Location:
code/trunk/src/modules/pong
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/pong/Pong.cc

    r9016 r9258  
    7676        // Set the type of Bots for this particular Gametype.
    7777        this->botclass_ = Class(PongBot);
    78         this->scoreLimit_ = 10;
    79         this->setConfigValues();
     78
     79        this->scoreLimit_ = 10;
     80        this->setConfigValues();
    8081    }
    8182
     
    8889        if (this->isInitialized())
    8990            this->cleanup();
     91    }
     92
     93    void Pong::setConfigValues()
     94    {
     95        SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins.");
    9096    }
    9197
     
    283289        }
    284290
    285         // If a palyer gets 21 points, he won the game -> end of game
    286        
    287         PlayerInfo* player1 = this->getLeftPlayer();
    288         PlayerInfo* player2 = this->getRightPlayer();
    289         if(player1==NULL||player2==NULL) return; //safety
    290         if(this->getScore(player1) >= scoreLimit_)
    291         {
    292             std::string name1=player1->getName();
    293             std::string message(name1 + " has won!");
    294             ChatManager::message(message);
    295             this->end();
    296         }
    297         else if(this->getScore(player2) >= scoreLimit_)
    298         {
    299              std::string name2=player2->getName();
    300              std::string message2(name2 + " has won!");
    301              ChatManager::message(message2);
     291        // If a player gets enough points, he won the game -> end of game
     292        PlayerInfo* winningPlayer = NULL;
     293        if (this->getLeftPlayer() && this->getScore(this->getLeftPlayer()) >= scoreLimit_)
     294            winningPlayer = this->getLeftPlayer();
     295        else if (this->getRightPlayer() && this->getScore(this->getRightPlayer()) >= scoreLimit_)
     296            winningPlayer = this->getRightPlayer();
     297
     298        if (winningPlayer)
     299        {
     300             ChatManager::message(winningPlayer->getName() + " has won!");
    302301             this->end();
    303302        }
     303
    304304        // Restart the timer to start the ball.
    305305        this->starttimer_.startTimer();
    306 
    307306    }
    308307
     
    344343            return 0;
    345344    }
    346 
    347     /**
    348      @brief
    349          Make scoreLimit_ configurable e.g. in the menu.
    350      */
    351     void Pong::setConfigValues()
    352     {
    353         SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins.");
    354     }
    355345}
  • code/trunk/src/modules/pong/Pong.h

    r9016 r9258  
    8282                { this->center_ = center; }
    8383            void setConfigValues(); //!< Makes scoreLimit configurable.
    84            
    85             PlayerInfo* getLeftPlayer() const; //!< Get the left player.
     84
     85            PlayerInfo* getLeftPlayer() const; //!< Get the left player.
    8686            PlayerInfo* getRightPlayer() const; //!< Get the right player.
    8787
     
    9595            WeakPtr<PongBall> ball_; //!< The Pong ball.
    9696            WeakPtr<PongBat> bat_[2]; //!< The two bats.
    97             Timer starttimer_; //!< A timer to delay the start of the game.
     97            Timer starttimer_; //!< A timer to delay the start of the game.
    9898            int scoreLimit_; //!< If a player scored that much points, the game is ended.
    9999    };
  • code/trunk/src/modules/pong/PongScore.cc

    r9016 r9258  
    6161        this->bShowRightPlayer_ = false;
    6262        this->player1_ = NULL;
    63         this->player2_ = NULL;
     63        this->player2_ = NULL;
    6464    }
    6565
     
    100100        if (this->owner_ != NULL)
    101101        {
    102             if(!this->owner_->hasEnded())
     102            if (!this->owner_->hasEnded())
    103103            {
    104                 //get the two players
     104                // Get the two players.
    105105                player1_ = this->owner_->getLeftPlayer();
    106106                player2_ = this->owner_->getRightPlayer();
    107107            }
    108108
    109             if(this->owner_->hasStarted())
     109            std::string name1;
     110            std::string name2;
     111
     112            std::string score1("0");
     113            std::string score2("0");
     114
     115            // Save the name and score of each player as a string.
     116            if (player1_ != NULL)
    110117            {
    111                 // Get the two players.
     118                name1 = player1_->getName();
     119                score1 = multi_cast<std::string>(this->owner_->getScore(player1_));
     120            }
     121            if (player2_ != NULL)
     122            {
     123                name2 = player2_->getName();
     124                score2 = multi_cast<std::string>(this->owner_->getScore(player2_));
     125            }
    112126
    113                 std::string name1;
    114                 std::string name2;
     127            // Assemble the strings, depending on what should all be displayed.
     128            std::string output1;
     129            if (this->bShowLeftPlayer_)
     130            {
     131                if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
     132                        output1 = name1 + " - " + score1;
     133                else if (this->bShowScore_)
     134                        output1 = score1;
     135                else if (this->bShowName_)
     136                        output1 = name1;
     137            }
    115138
    116                 std::string score1("0");
    117                 std::string score2("0");
    118 
    119                 // Save the name and score of each player as a string.
    120                 if (player1_ != NULL)
    121                 {
    122                     name1 = player1_->getName();
    123                     score1 = multi_cast<std::string>(this->owner_->getScore(player1_));
    124                 }
    125                 if (player2_ != NULL)
    126                 {
    127                     name2 = player2_->getName();
    128                     score2 = multi_cast<std::string>(this->owner_->getScore(player2_));
    129                 }
    130 
    131                 // Assemble the strings, depending on what should all be displayed.
    132                 std::string output1;
    133                 if (this->bShowLeftPlayer_)
    134                 {
    135                     if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
    136                          output1 = name1 + " - " + score1;
    137                     else if (this->bShowScore_)
    138                          output1 = score1;
    139                     else if (this->bShowName_)
    140                          output1 = name1;
    141                 }
    142 
    143                 std::string output2;
    144                 if (this->bShowRightPlayer_)
    145                 {
     139            std::string output2;
     140            if (this->bShowRightPlayer_)
     141            {
    146142                if (this->bShowName_ && this->bShowScore_ && player2_ != NULL)
    147143                    output2 = score2 + " - " + name2;
     
    152148            }
    153149
    154                 std::string output("PONG");
    155                 if (this->bShowName_ || this->bShowScore_)
    156                 {
    157                     if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
    158                         output = output1 + ':' + output2;
    159                     else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
    160                         output = output1 + output2;
    161                 }
    162                 this->setCaption(output);
     150            std::string output("PONG");
     151            if (this->bShowName_ || this->bShowScore_)
     152            {
     153                if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
     154                    output = output1 + ':' + output2;
     155                else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
     156                    output = output1 + output2;
    163157            }
     158
     159            this->setCaption(output);
    164160        }
    165161    }
  • code/trunk/src/modules/pong/PongScore.h

    r9016 r9258  
    122122            bool bShowLeftPlayer_; //!< Whether the left player is shown.
    123123            bool bShowRightPlayer_; //!< Whether the right player is shown.
    124             PlayerInfo* player1_; //!< Store information about left player permanently.
    125             PlayerInfo* player2_; //!< Same for the right player. To end the game properly.
     124            WeakPtr<PlayerInfo> player1_; //!< Store information about left player permanently.
     125            WeakPtr<PlayerInfo> player2_; //!< Same for the right player. To end the game properly.
    126126    };
    127127}
Note: See TracChangeset for help on using the changeset viewer.