Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8104


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

Started documenting Pong.

Location:
code/branches/tetris/src/modules/pong
Files:
4 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
  • code/branches/tetris/src/modules/pong/Pong.h

    r7911 r8104  
    2727 */
    2828
     29/**
     30    @file Pong.h
     31    @brief Declaration of the Pong class.
     32    @ingroup Pong
     33*/
     34
    2935#ifndef _Pong_H__
    3036#define _Pong_H__
     
    3339
    3440#include "tools/Timer.h"
     41
    3542#include "gametypes/Deathmatch.h"
    3643
    3744namespace orxonox
    3845{
     46
     47    /**
     48    @brief
     49        Implements a Pong minigame.
     50
     51        //TODO: Add details to different classes used and how.
     52        PongBall, is the ball, PongBats are the things that can be moved by the players (ControllableEntities), PongCenterpoint is the playing field. (x-z area)
     53
     54    @author
     55        Fabian 'x3n' Landau
     56
     57    @ingroup Pong
     58    */
    3959    class _PongExport Pong : public Deathmatch
    4060    {
    4161        public:
    42             Pong(BaseObject* creator);
    43             virtual ~Pong();
     62            Pong(BaseObject* creator); //!< Constructor. Registers and initializes the object.
     63            virtual ~Pong(); //!< Destructor. Cleans up, if initialized.
    4464
    45             virtual void start();
    46             virtual void end();
     65            virtual void start(); //!< Starts the Pong minigame.
     66            virtual void end(); ///!< Ends the Pong minigame.
    4767
    48             virtual void spawnPlayer(PlayerInfo* player);
     68            virtual void spawnPlayer(PlayerInfo* player); //!< Spawns the input player.
    4969
    50             virtual void playerScored(PlayerInfo* player);
     70            virtual void playerScored(PlayerInfo* player); //!< Is called when the player scored.
    5171
     72            /**
     73            @brief Set the PongCenterpoint (the playing field).
     74            @param center A pointer to the PongCenterpoint to be set.
     75            */
    5276            void setCenterpoint(PongCenterpoint* center)
    5377                { this->center_ = center; }
    5478
    55             PlayerInfo* getLeftPlayer() const;
    56             PlayerInfo* getRightPlayer() const;
     79            PlayerInfo* getLeftPlayer() const; //!< Get the left player.
     80            PlayerInfo* getRightPlayer() const; //!< Get the right player.
    5781
    5882        protected:
    59             virtual void spawnPlayersIfRequested();
     83            virtual void spawnPlayersIfRequested(); //!< Spawns players, and fills the rest up with bots.
    6084
    61             void startBall();
    62             void cleanup();
     85            void startBall(); //!< Starts the ball with some default speed.
     86            void cleanup(); //!< Cleans up the Gametype by destroying the ball and the bats.
    6387
    64             WeakPtr<PongCenterpoint> center_;
    65             WeakPtr<PongBall> ball_;
    66             WeakPtr<PongBat> bat_[2];
    67             Timer starttimer_;
     88            WeakPtr<PongCenterpoint> center_; //!< The playing field.
     89            WeakPtr<PongBall> ball_; //!< The Pong ball.
     90            WeakPtr<PongBat> bat_[2]; //!< The two bats.
     91            Timer starttimer_; //!< A timer to delay the start of the game.
    6892    };
    6993}
  • code/branches/tetris/src/modules/pong/PongBall.cc

    r7886 r8104  
    2727 */
    2828
     29/**
     30    @file PongBall.cc
     31    @brief Implementation of the PongBall class.
     32*/
     33
    2934#include "PongBall.h"
    3035
    3136#include "core/CoreIncludes.h"
    3237#include "core/GameMode.h"
     38
    3339#include "gametypes/Gametype.h"
     40
    3441#include "PongBat.h"
    3542
     
    4047    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
    4148
     49    /**
     50    @brief
     51        Constructor. Registers and initializes the object.
     52    */
    4253    PongBall::PongBall(BaseObject* creator)
    4354        : MovableEntity(creator)
     
    5768    }
    5869
     70    /**
     71    @brief
     72        Destructor.
     73    */
    5974    PongBall::~PongBall()
    6075    {
     
    6883    }
    6984
     85    /**
     86    @brief
     87        Register variables to synchronize over the network.
     88    */
    7089    void PongBall::registerVariables()
    7190    {
     
    7998    }
    8099
     100    /**
     101    @brief
     102        Is called every tick.
     103        //TODO: Explain in detail what happens here.
     104    @param dt
     105        The time since the last tick.
     106    */
    81107    void PongBall::tick(float dt)
    82108    {
    83109        SUPER(PongBall, tick, dt);
    84110
     111        // Get the current position, velocity and acceleration of the ball.
    85112        Vector3 position = this->getPosition();
    86113        Vector3 velocity = this->getVelocity();
    87114        Vector3 acceleration = this->getAcceleration();
    88115
     116        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
    89117        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
    90118        {
     119            // Its velocity in z-direction is inverted (i.e. it bounces off).
    91120            velocity.z = -velocity.z;
     121            // And its position is set as to not overstep the boundary it has just crossed.
    92122            if (position.z > this->fieldHeight_ / 2)
    93123                position.z = this->fieldHeight_ / 2;
     
    98128        }
    99129
     130        // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry).
    100131        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
    101132        {
    102133            float distance = 0;
    103134
    104             if (this->bat_)
     135            if (this->bat_ != NULL) // If there are bats.
    105136            {
    106                 if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
     137                // If the right boundary has been crossed.
     138                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
    107139                {
     140                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
    108141                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
    109                     if (fabs(distance) <= 1)
    110                     {
     142                    if (fabs(distance) <= 1) // If the bat is there to parry.
     143                    {
     144                        // Set the ball to be exactly at the boundary.
    111145                        position.x = this->fieldWidth_ / 2;
     146                        // Invert its veloxity in x-direction (i.e. it bounces off).
    112147                        velocity.x = -velocity.x;
     148                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
    113149                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
    114150                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
     
    116152                        this->fireEvent();
    117153                    }
     154                    // If the left player scores.
    118155                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
    119156                    {
     
    125162                    }
    126163                }
    127                 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
     164                // If the left boundary has been crossed.
     165                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
    128166                {
     167                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
    129168                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
    130                     if (fabs(distance) <= 1)
    131                     {
     169                    if (fabs(distance) <= 1) // If the bat is there to parry.
     170                    {
     171                        // Set the ball to be exactly at the boundary.
    132172                        position.x = -this->fieldWidth_ / 2;
     173                        // Invert its veloxity in x-direction (i.e. it bounces off).
    133174                        velocity.x = -velocity.x;
     175                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
    134176                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
    135177                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
     
    137179                        this->fireEvent();
    138180                    }
     181                    // If the right player scores.
    139182                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
    140183                    {
     
    149192        }
    150193
     194        // Set the position, velocity and acceleration of the ball, if they have changed.
    151195        if (acceleration != this->getAcceleration())
    152196            this->setAcceleration(acceleration);
     
    157201    }
    158202
     203    /**
     204    @brief
     205        Set the speed of the ball (in x-direction).
     206    @param speed
     207        The speed to be set.
     208    */
    159209    void PongBall::setSpeed(float speed)
    160210    {
    161         if (speed != this->speed_)
     211        if (speed != this->speed_) // If the speed changes
    162212        {
    163213            this->speed_ = speed;
    164214
     215            // Set the speed in the direction of the balls current velocity.
    165216            Vector3 velocity = this->getVelocity();
    166217            if (velocity.x != 0)
    167218                velocity.x = sgn(velocity.x) * this->speed_;
    168             else
     219            else // If the balls current velocity is zero, the speed is set in a random direction.
    169220                velocity.x = this->speed_ * sgn(rnd(-1,1));
    170221
     
    173224    }
    174225
     226    /**
     227    @brief
     228        Set the
     229    */
    175230    void PongBall::setBats(WeakPtr<PongBat>* bats)
    176231    {
  • code/branches/tetris/src/modules/pong/PongBall.h

    r7885 r8104  
    2727 */
    2828
     29/**
     30    @file PongBall.h
     31    @brief Declaration of the PongBall class.
     32    @ingroup Pong
     33*/
     34
    2935#ifndef _PongBall_H__
    3036#define _PongBall_H__
     
    3339
    3440#include "util/Math.h"
     41
    3542#include "worldentities/MovableEntity.h"
    3643
    3744namespace orxonox
    3845{
     46
     47    /**
     48    @brief
     49        This class manages the ball for Pong.
     50
     51        //TODO: Describe what it's responnsibilities are.
     52        Only moves in x-z area.
     53
     54    @author
     55        Fabian 'x3n' Landau
     56
     57    @ingroup Pong
     58    */
    3959    class _PongExport PongBall : public MovableEntity
    4060    {
Note: See TracChangeset for help on using the changeset viewer.