Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 23, 2011, 9:57:54 PM (13 years ago)
Author:
dafrick
Message:

Merging changes from tetris branch into trunk, since they are also useful, there.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/modules/pong/Pong.cc

    r7911 r8108  
    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
     40#include "gamestates/GSLevel.h"
     41
    3442#include "PongCenterpoint.h"
    3543#include "PongBall.h"
     
    4048namespace orxonox
    4149{
     50    // Events to allow to react to scoring of a player, in the level-file.
    4251    CreateEventName(PongCenterpoint, right);
    4352    CreateEventName(PongCenterpoint, left);
     
    4554    CreateUnloadableFactory(Pong);
    4655
     56    /**
     57    @brief
     58        Constructor. Registers and initializes the object.
     59    */
    4760    Pong::Pong(BaseObject* creator) : Deathmatch(creator)
    4861    {
     
    5669        this->setHUDTemplate("PongHUD");
    5770
     71        // Pre-set the timer, but don't start it yet.
    5872        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this)));
    5973        this->starttimer_.stopTimer();
    6074
     75        // Set the type of Bots for this particular Gametype.
    6176        this->botclass_ = Class(PongBot);
    6277    }
    6378
     79    /**
     80    @brief
     81        Destructor. Cleans up, if initialized.
     82    */
    6483    Pong::~Pong()
    6584    {
     
    6887    }
    6988
     89    /**
     90    @brief
     91        Cleans up the Gametype by destroying the ball and the bats.
     92    */
    7093    void Pong::cleanup()
    7194    {
    72         if (this->ball_)
     95        if (this->ball_ != NULL) // Destroy the ball, if present.
    7396        {
    7497            this->ball_->destroy();
     
    7699        }
    77100
     101        // Destroy both bats, if present.
    78102        for (size_t i = 0; i < 2; ++i)
    79103        {
    80             if (this->bat_[0])
     104            if (this->bat_[0] != NULL)
    81105            {
    82106                this->bat_[0]->destroy();
     
    86110    }
    87111
     112    /**
     113    @brief
     114        Starts the Pong minigame.
     115    */
    88116    void Pong::start()
    89117    {
    90         if (this->center_)
    91         {
    92             if (!this->ball_)
     118        if (this->center_ != NULL) // There needs to be a PongCenterpoint, i.e. the area the game takes place.
     119        {
     120            if (this->ball_ == NULL) // If there is no ball, create a new ball.
    93121            {
    94122                this->ball_ = new PongBall(this->center_);
     123                // Apply the template for the ball specified by the centerpoint.
    95124                this->ball_->addTemplate(this->center_->getBalltemplate());
    96125            }
    97126
     127            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
    98128            this->center_->attach(this->ball_);
    99129            this->ball_->setPosition(0, 0, 0);
     
    103133            this->ball_->setBatLength(this->center_->getBatLength());
    104134
    105             if (!this->bat_[0])
     135            // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint.
     136            for (size_t i = 0; i < 2; ++i)
    106137            {
    107                 this->bat_[0] = new PongBat(this->center_);
    108                 this->bat_[0]->addTemplate(this->center_->getBattemplate());
     138                if (this->bat_[i] == NULL)
     139                {
     140                    this->bat_[i] = new PongBat(this->center_);
     141                    this->bat_[i]->addTemplate(this->center_->getBattemplate());
     142                }
    109143            }
    110             if (!this->bat_[1])
    111             {
    112                 this->bat_[1] = new PongBat(this->center_);
    113                 this->bat_[1]->addTemplate(this->center_->getBattemplate());
    114             }
    115 
     144
     145            // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to.
    116146            this->center_->attach(this->bat_[0]);
    117147            this->center_->attach(this->bat_[1]);
     
    127157            this->bat_[1]->setLength(this->center_->getBatLength());
    128158
     159            // Set the bats for the ball.
    129160            this->ball_->setBats(this->bat_);
    130161        }
    131         else
     162        else // If no centerpoint was specified, an error is thrown and the level is exited.
    132163        {
    133164            COUT(1) << "Error: No Centerpoint specified." << std::endl;
    134         }
    135 
     165            GSLevel::startMainMenu();
     166            return;
     167        }
     168
     169        // Start the timer. After it has expired the ball is started.
    136170        this->starttimer_.startTimer();
    137171
    138 
     172        // Set variable to temporarily force the player to spawn.
    139173        bool temp = this->bForceSpawn_;
    140174        this->bForceSpawn_ = true;
    141175
     176        // Call start for the parent class.
    142177        Deathmatch::start();
    143178
     179        // Reset the variable.
    144180        this->bForceSpawn_ = temp;
    145181    }
    146182
     183    /**
     184    @brief
     185        Ends the Pong minigame.
     186    */
    147187    void Pong::end()
    148188    {
    149189        this->cleanup();
    150190
     191        // Call end for the parent class.
    151192        Deathmatch::end();
    152193    }
    153194
     195    /**
     196    @brief
     197        Spawns players, and fills the rest up with bots.
     198    */
    154199    void Pong::spawnPlayersIfRequested()
    155200    {
     
    164209    }
    165210
     211    /**
     212    @brief
     213        Spawns the input player.
     214    @param player
     215        The player to be spawned.
     216    */
    166217    void Pong::spawnPlayer(PlayerInfo* player)
    167218    {
    168         if (!this->bat_[0]->getPlayer())
     219        assert(player);
     220
     221        // If the first (left) bat has no player.
     222        if (this->bat_[0]->getPlayer() == NULL)
    169223        {
    170224            player->startControl(this->bat_[0]);
    171225            this->players_[player].state_ = PlayerState::Alive;
    172226        }
    173         else if (!this->bat_[1]->getPlayer())
     227        // If the second (right) bat has no player.
     228        else if (this->bat_[1]->getPlayer() == NULL)
    174229        {
    175230            player->startControl(this->bat_[1]);
    176231            this->players_[player].state_ = PlayerState::Alive;
    177232        }
     233        // If both bats are taken.
    178234        else
    179235            return;
    180236
    181         if (player && player->getController() && player->getController()->isA(Class(PongAI)))
     237        // If the player is an AI, it receives a pointer to the ball.
     238        if (player->getController() != NULL && player->getController()->isA(Class(PongAI)))
    182239        {
    183240            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
     
    186243    }
    187244
     245    /**
     246    @brief
     247        Is called when the player scored.
     248    */
    188249    void Pong::playerScored(PlayerInfo* player)
    189250    {
    190251        Deathmatch::playerScored(player);
    191252
    192         if (this->center_)
    193         {
     253        if (this->center_ != NULL) // If there is a centerpoint.
     254        {
     255            // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks.
    194256            if (player == this->getRightPlayer())
    195257                this->center_->fireEvent(FireEventName(PongCenterpoint, right));
     
    197259                this->center_->fireEvent(FireEventName(PongCenterpoint, left));
    198260
    199             if (player)
     261            // Also announce, that the player has scored.
     262            if (player != NULL)
    200263                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
    201264        }
    202265
    203         if (this->ball_)
     266        // If there is a ball present, reset its position, velocity and acceleration.
     267        if (this->ball_ != NULL)
    204268        {
    205269            this->ball_->setPosition(Vector3::ZERO);
     
    209273        }
    210274
    211         if (this->bat_[0] && this->bat_[1])
     275        // If there are bats reset them to the middle position.
     276        if (this->bat_[0] != NULL && this->bat_[1] != NULL)
    212277        {
    213278            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
     
    215280        }
    216281
     282        // Restart the timer to start the ball.
    217283        this->starttimer_.startTimer();
    218284    }
    219285
     286    /**
     287    @brief
     288        Starts the ball with some default speed.
     289    */
    220290    void Pong::startBall()
    221291    {
    222         if (this->ball_ && this->center_)
     292        if (this->ball_ != NULL && this->center_ != NULL)
    223293            this->ball_->setSpeed(this->center_->getBallSpeed());
    224294    }
    225295
     296    /**
     297    @brief
     298        Get the left player.
     299    @return
     300        Returns a pointer to the player playing on the left. If there is no left player, NULL is returned.
     301    */
    226302    PlayerInfo* Pong::getLeftPlayer() const
    227303    {
    228         if (this->bat_ && this->bat_[0])
     304        if (this->bat_ != NULL && this->bat_[0] != NULL)
    229305            return this->bat_[0]->getPlayer();
    230306        else
     
    232308    }
    233309
     310    /**
     311    @brief
     312        Get the right player.
     313    @return
     314        Returns a pointer to the player playing on the right. If there is no right player, NULL is returned.
     315    */
    234316    PlayerInfo* Pong::getRightPlayer() const
    235317    {
    236         if (this->bat_ && this->bat_[1])
     318        if (this->bat_ != NULL && this->bat_[1] != NULL)
    237319            return this->bat_[1]->getPlayer();
    238320        else
Note: See TracChangeset for help on using the changeset viewer.