Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 23, 2011, 7:40:36 AM (13 years ago)
Author:
dafrick
Message:

Started documenting Pong.

File:
1 edited

Legend:

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

    r7911 r8104  
    2727 */
    2828
     29/**
     30    @file Pong.cc
     31    @brief Implementation of the Pong class.
     32*/
     33
    2934#include "Pong.h"
    3035
     
    3237#include "core/EventIncludes.h"
    3338#include "core/command/Executor.h"
     39
    3440#include "PongCenterpoint.h"
    3541#include "PongBall.h"
     
    4046namespace orxonox
    4147{
     48    // Events to allow to react to scoring of a player, in the level-file.
    4249    CreateEventName(PongCenterpoint, right);
    4350    CreateEventName(PongCenterpoint, left);
     
    4552    CreateUnloadableFactory(Pong);
    4653
     54    /**
     55    @brief
     56        Constructor. Registers and initializes the object.
     57    */
    4758    Pong::Pong(BaseObject* creator) : Deathmatch(creator)
    4859    {
     
    5667        this->setHUDTemplate("PongHUD");
    5768
     69        // Pre-set the timer, but don't start it yet.
    5870        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this)));
    5971        this->starttimer_.stopTimer();
    6072
     73        // Set the type of Bots for this particular Gametype.
    6174        this->botclass_ = Class(PongBot);
    6275    }
    6376
     77    /**
     78    @brief
     79        Destructor. Cleans up, if initialized.
     80    */
    6481    Pong::~Pong()
    6582    {
     
    6885    }
    6986
     87    /**
     88    @brief
     89        Cleans up the Gametype by destroying the ball and the bats.
     90    */
    7091    void Pong::cleanup()
    7192    {
    72         if (this->ball_)
     93        if (this->ball_ != NULL) // Destroy the ball, if present.
    7394        {
    7495            this->ball_->destroy();
     
    7697        }
    7798
     99        // Destroy both bats, if present.
    78100        for (size_t i = 0; i < 2; ++i)
    79101        {
    80             if (this->bat_[0])
     102            if (this->bat_[0] != NULL)
    81103            {
    82104                this->bat_[0]->destroy();
     
    86108    }
    87109
     110    /**
     111    @brief
     112        Starts the Pong minigame.
     113    */
    88114    void Pong::start()
    89115    {
    90         if (this->center_)
    91         {
    92             if (!this->ball_)
     116        if (this->center_ != NULL) // There needs to be a PongCenterpoint, i.e. the area the game takes place.
     117        {
     118            if (this->ball_ == NULL) // If there is no ball, create a new ball.
    93119            {
    94120                this->ball_ = new PongBall(this->center_);
     121                // Apply the template for the ball specified by the centerpoint.
    95122                this->ball_->addTemplate(this->center_->getBalltemplate());
    96123            }
    97124
     125            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
    98126            this->center_->attach(this->ball_);
    99127            this->ball_->setPosition(0, 0, 0);
     
    103131            this->ball_->setBatLength(this->center_->getBatLength());
    104132
    105             if (!this->bat_[0])
     133            // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint.
     134            for (size_t i = 0; i < 2; ++i)
    106135            {
    107                 this->bat_[0] = new PongBat(this->center_);
    108                 this->bat_[0]->addTemplate(this->center_->getBattemplate());
     136                if (this->bat_[i] == NULL)
     137                {
     138                    this->bat_[i] = new PongBat(this->center_);
     139                    this->bat_[i]->addTemplate(this->center_->getBattemplate());
     140                }
    109141            }
    110             if (!this->bat_[1])
    111             {
    112                 this->bat_[1] = new PongBat(this->center_);
    113                 this->bat_[1]->addTemplate(this->center_->getBattemplate());
    114             }
    115 
     142
     143            // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to.
    116144            this->center_->attach(this->bat_[0]);
    117145            this->center_->attach(this->bat_[1]);
     
    127155            this->bat_[1]->setLength(this->center_->getBatLength());
    128156
     157            // Set the bats for the ball.
    129158            this->ball_->setBats(this->bat_);
    130159        }
    131         else
     160        else // If no centerpoint was specified, an error is thrown.
    132161        {
    133162            COUT(1) << "Error: No Centerpoint specified." << std::endl;
    134         }
    135 
     163            // TODO: End the game?
     164        }
     165
     166        // Start the timer. After it has expired the ball is started.
    136167        this->starttimer_.startTimer();
    137168
    138 
     169        // Set variable to temporarily force the player to spawn.
    139170        bool temp = this->bForceSpawn_;
    140171        this->bForceSpawn_ = true;
    141172
     173        // Call start for the parent class.
    142174        Deathmatch::start();
    143175
     176        // Reset the variable.
    144177        this->bForceSpawn_ = temp;
    145178    }
    146179
     180    /**
     181    @brief
     182        Ends the Pong minigame.
     183    */
    147184    void Pong::end()
    148185    {
    149186        this->cleanup();
    150187
     188        // Call end for the parent class.
    151189        Deathmatch::end();
    152190    }
    153191
     192    /**
     193    @brief
     194        Spawns players, and fills the rest up with bots.
     195    */
    154196    void Pong::spawnPlayersIfRequested()
    155197    {
     
    164206    }
    165207
     208    /**
     209    @brief
     210        Spawns the input player.
     211    @param player
     212        The player tp be spawned.
     213    */
    166214    void Pong::spawnPlayer(PlayerInfo* player)
    167215    {
    168         if (!this->bat_[0]->getPlayer())
     216        assert(player);
     217
     218        // If the first (left) bat has no player.
     219        if (this->bat_[0]->getPlayer() == NULL)
    169220        {
    170221            player->startControl(this->bat_[0]);
    171222            this->players_[player].state_ = PlayerState::Alive;
    172223        }
    173         else if (!this->bat_[1]->getPlayer())
     224        // If the second (right) bat has no player.
     225        else if (this->bat_[1]->getPlayer() == NULL)
    174226        {
    175227            player->startControl(this->bat_[1]);
    176228            this->players_[player].state_ = PlayerState::Alive;
    177229        }
     230        // If both bats are taken.
    178231        else
    179232            return;
    180233
    181         if (player && player->getController() && player->getController()->isA(Class(PongAI)))
     234        // If the player is an AI, it receives a pointer to the ball.
     235        if (player->getController() != NULL && player->getController()->isA(Class(PongAI)))
    182236        {
    183237            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
     
    186240    }
    187241
     242    /**
     243    @brief
     244        Is called when the player scored.
     245    */
    188246    void Pong::playerScored(PlayerInfo* player)
    189247    {
    190248        Deathmatch::playerScored(player);
    191249
    192         if (this->center_)
    193         {
     250        if (this->center_ != NULL) // If there is a centerpoint.
     251        {
     252            // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks.
    194253            if (player == this->getRightPlayer())
    195254                this->center_->fireEvent(FireEventName(PongCenterpoint, right));
     
    197256                this->center_->fireEvent(FireEventName(PongCenterpoint, left));
    198257
    199             if (player)
     258            // Also announce, that the player has scored.
     259            if (player != NULL)
    200260                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
    201261        }
    202262
    203         if (this->ball_)
     263        // If there is a ball present, reset its position, velocity and acceleration.
     264        if (this->ball_ != NULL)
    204265        {
    205266            this->ball_->setPosition(Vector3::ZERO);
     
    209270        }
    210271
    211         if (this->bat_[0] && this->bat_[1])
     272        // If there are bats reset them to the middle position.
     273        if (this->bat_[0] != NULL && this->bat_[1] != NULL)
    212274        {
    213275            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
     
    215277        }
    216278
     279        // Restart the timer to start the ball.
    217280        this->starttimer_.startTimer();
    218281    }
    219282
     283    /**
     284    @brief
     285        Starts the ball with some default speed.
     286    */
    220287    void Pong::startBall()
    221288    {
    222         if (this->ball_ && this->center_)
     289        if (this->ball_ != NULL && this->center_ != NULL)
    223290            this->ball_->setSpeed(this->center_->getBallSpeed());
    224291    }
    225292
     293    /**
     294    @brief
     295        Get the left player.
     296    @return
     297        Returns a pointer to the player playing on the left. If there is no left player, NULL is returned.
     298    */
    226299    PlayerInfo* Pong::getLeftPlayer() const
    227300    {
    228         if (this->bat_ && this->bat_[0])
     301        if (this->bat_ != NULL && this->bat_[0] != NULL)
    229302            return this->bat_[0]->getPlayer();
    230303        else
     
    232305    }
    233306
     307    /**
     308    @brief
     309        Get the right player.
     310    @return
     311        Returns a pointer to the player playing on the right. If there is no right player, NULL is returned.
     312    */
    234313    PlayerInfo* Pong::getRightPlayer() const
    235314    {
    236         if (this->bat_ && this->bat_[1])
     315        if (this->bat_ != NULL && this->bat_[1] != NULL)
    237316            return this->bat_[1]->getPlayer();
    238317        else
Note: See TracChangeset for help on using the changeset viewer.