Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_FS19/src/modules/pong/Pong.cc @ 12411

Last change on this file since 12411 was 12411, checked in by wiesep, 5 years ago

Merged OrxoBlox_FS19

  • Property svn:eol-style set to native
File size: 11.2 KB
RevLine 
[2825]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[8108]29/**
30    @file Pong.cc
31    @brief Implementation of the Pong class.
32*/
33
[2825]34#include "Pong.h"
35
36#include "core/CoreIncludes.h"
[5929]37#include "core/EventIncludes.h"
[7284]38#include "core/command/Executor.h"
[9667]39#include "core/config/ConfigValueIncludes.h"
[8108]40
41#include "gamestates/GSLevel.h"
[9016]42#include "chat/ChatManager.h"
[8108]43
[5725]44#include "PongCenterpoint.h"
45#include "PongBall.h"
46#include "PongBat.h"
47#include "PongBot.h"
48#include "PongAI.h"
[9939]49
[2825]50namespace orxonox
51{
[8108]52    // Events to allow to react to scoring of a player, in the level-file.
[5929]53    CreateEventName(PongCenterpoint, right);
54    CreateEventName(PongCenterpoint, left);
[6417]55
[9667]56    RegisterUnloadableClass(Pong);
[2825]57
[8108]58    /**
59    @brief
60        Constructor. Registers and initializes the object.
61    */
[9667]62    Pong::Pong(Context* context) : Deathmatch(context)
[2825]63    {
64        RegisterObject(Pong);
65
[11071]66        this->center_ = nullptr;
67        this->ball_ = nullptr;
68        this->bat_[0] = nullptr;
69        this->bat_[1] = nullptr;
[2825]70
[2890]71        this->setHUDTemplate("PongHUD");
72
[8108]73        // Pre-set the timer, but don't start it yet.
[5929]74        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this)));
[2825]75        this->starttimer_.stopTimer();
[2839]76
[8108]77        // Set the type of Bots for this particular Gametype.
[2839]78        this->botclass_ = Class(PongBot);
[9258]79
80        this->scoreLimit_ = 10;
81        this->setConfigValues();
[2825]82    }
83
[8108]84    /**
85    @brief
86        Destructor. Cleans up, if initialized.
87    */
[7911]88    Pong::~Pong()
89    {
90        if (this->isInitialized())
91            this->cleanup();
92    }
93
[9258]94    void Pong::setConfigValues()
95    {
96        SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins.");
97    }
98
[8108]99    /**
100    @brief
101        Cleans up the Gametype by destroying the ball and the bats.
102    */
[7911]103    void Pong::cleanup()
104    {
[11071]105        if (this->ball_ != nullptr) // Destroy the ball, if present.
[7911]106        {
107            this->ball_->destroy();
[11071]108            this->ball_ = nullptr;
[7911]109        }
110
[8108]111        // Destroy both bats, if present.
[7911]112        for (size_t i = 0; i < 2; ++i)
113        {
[11071]114            if (this->bat_[0] != nullptr)
[7911]115            {
116                this->bat_[0]->destroy();
[11071]117                this->bat_[0] = nullptr;
[7911]118            }
119        }
[9939]120
[7911]121    }
122
[8108]123    /**
124    @brief
125        Starts the Pong minigame.
126    */
[2825]127    void Pong::start()
128    {
[12411]129        orxout() << "Hello" << endl;
[11071]130        if (this->center_ != nullptr) // There needs to be a PongCenterpoint, i.e. the area the game takes place.
[2825]131        {
[11071]132            if (this->ball_ == nullptr) // If there is no ball, create a new ball.
[2825]133            {
[9667]134                this->ball_ = new PongBall(this->center_->getContext());
[8108]135                // Apply the template for the ball specified by the centerpoint.
[2825]136                this->ball_->addTemplate(this->center_->getBalltemplate());
137            }
138
[8108]139            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
[2825]140            this->center_->attach(this->ball_);
141            this->ball_->setPosition(0, 0, 0);
142            this->ball_->setFieldDimension(this->center_->getFieldDimension());
143            this->ball_->setSpeed(0);
[5929]144            this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor());
[2825]145            this->ball_->setBatLength(this->center_->getBatLength());
146
[8108]147            // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint.
[11071]148            for (WeakPtr<orxonox::PongBat>& bat : this->bat_)
[2825]149            {
[11071]150                if (bat == nullptr)
[8108]151                {
[11071]152                    bat = new PongBat(this->center_->getContext());
153                    bat->addTemplate(this->center_->getBattemplate());
[8108]154                }
[2825]155            }
156
[8108]157            // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to.
[2825]158            this->center_->attach(this->bat_[0]);
159            this->center_->attach(this->bat_[1]);
160            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
161            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
162            this->bat_[0]->yaw(Degree(-90));
163            this->bat_[1]->yaw(Degree(90));
164            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
165            this->bat_[1]->setSpeed(this->center_->getBatSpeed());
166            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
167            this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y);
168            this->bat_[0]->setLength(this->center_->getBatLength());
169            this->bat_[1]->setLength(this->center_->getBatLength());
170
[8108]171            // Set the bats for the ball.
[2825]172            this->ball_->setBats(this->bat_);
173        }
[8108]174        else // If no centerpoint was specified, an error is thrown and the level is exited.
[2825]175        {
[8858]176            orxout(internal_error) << "Pong: No Centerpoint specified." << endl;
[8108]177            GSLevel::startMainMenu();
178            return;
[2825]179        }
180
[8108]181        // Start the timer. After it has expired the ball is started.
[2825]182        this->starttimer_.startTimer();
183
[8108]184        // Set variable to temporarily force the player to spawn.
[2885]185        bool temp = this->bForceSpawn_;
186        this->bForceSpawn_ = true;
187
[8108]188        // Call start for the parent class.
[2825]189        Deathmatch::start();
[2885]190
[8108]191        // Reset the variable.
[2885]192        this->bForceSpawn_ = temp;
[2825]193    }
194
[8108]195    /**
196    @brief
197        Ends the Pong minigame.
198    */
[2825]199    void Pong::end()
200    {
[7911]201        this->cleanup();
[2825]202
[8108]203        // Call end for the parent class.
[2825]204        Deathmatch::end();
205    }
206
[8108]207    /**
208    @brief
209        Spawns players, and fills the rest up with bots.
210    */
[7865]211    void Pong::spawnPlayersIfRequested()
212    {
213        // first spawn human players to assign always the left bat to the player in singleplayer
[11071]214        for (const auto& mapEntry : this->players_)
215            if (mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_))
216                this->spawnPlayer(mapEntry.first);
[7865]217        // now spawn bots
[11071]218        for (const auto& mapEntry : this->players_)
219            if (!mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_))
220                this->spawnPlayer(mapEntry.first);
[7865]221    }
222
[8108]223    /**
224    @brief
225        Spawns the input player.
226    @param player
227        The player to be spawned.
228    */
[2825]229    void Pong::spawnPlayer(PlayerInfo* player)
230    {
[8108]231        assert(player);
232
233        // If the first (left) bat has no player.
[11071]234        if (this->bat_[0]->getPlayer() == nullptr)
[2825]235        {
236            player->startControl(this->bat_[0]);
237            this->players_[player].state_ = PlayerState::Alive;
238        }
[8108]239        // If the second (right) bat has no player.
[11071]240        else if (this->bat_[1]->getPlayer() == nullptr)
[2825]241        {
242            player->startControl(this->bat_[1]);
243            this->players_[player].state_ = PlayerState::Alive;
244        }
[8108]245        // If both bats are taken.
[2839]246        else
247            return;
248
[8108]249        // If the player is an AI, it receives a pointer to the ball.
[11071]250        if (player->getController() != nullptr && player->getController()->isA(Class(PongAI)))
[2839]251        {
[3325]252            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
[2839]253            ai->setPongBall(this->ball_);
254        }
[2825]255    }
256
[8108]257    /**
258    @brief
259        Is called when the player scored.
260    */
[9348]261    void Pong::playerScored(PlayerInfo* player, int score)
[2825]262    {
[9348]263        Deathmatch::playerScored(player, score);
[2825]264
[11071]265        if (this->center_ != nullptr) // If there is a centerpoint.
[2872]266        {
[8108]267            // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks.
[5929]268            if (player == this->getRightPlayer())
269                this->center_->fireEvent(FireEventName(PongCenterpoint, right));
270            else if (player == this->getLeftPlayer())
271                this->center_->fireEvent(FireEventName(PongCenterpoint, left));
[6417]272
[8108]273            // Also announce, that the player has scored.
[11071]274            if (player != nullptr)
[5929]275                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
[2872]276        }
277
[8108]278        // If there is a ball present, reset its position, velocity and acceleration.
[11071]279        if (this->ball_ != nullptr)
[2825]280        {
281            this->ball_->setPosition(Vector3::ZERO);
282            this->ball_->setVelocity(Vector3::ZERO);
[5929]283            this->ball_->setAcceleration(Vector3::ZERO);
[2825]284            this->ball_->setSpeed(0);
285        }
286
[8108]287        // If there are bats reset them to the middle position.
[11071]288        if (this->bat_[0] != nullptr && this->bat_[1] != nullptr)
[2825]289        {
290            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
291            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
292        }
293
[9258]294        // If a player gets enough points, he won the game -> end of game
[11071]295        PlayerInfo* winningPlayer = nullptr;
[9258]296        if (this->getLeftPlayer() && this->getScore(this->getLeftPlayer()) >= scoreLimit_)
297            winningPlayer = this->getLeftPlayer();
298        else if (this->getRightPlayer() && this->getScore(this->getRightPlayer()) >= scoreLimit_)
299            winningPlayer = this->getRightPlayer();
300
301        if (winningPlayer)
[9016]302        {
[9258]303             ChatManager::message(winningPlayer->getName() + " has won!");
[9016]304             this->end();
305        }
[9258]306
[8108]307        // Restart the timer to start the ball.
[2825]308        this->starttimer_.startTimer();
309    }
310
[8108]311    /**
312    @brief
313        Starts the ball with some default speed.
314    */
[2825]315    void Pong::startBall()
316    {
[11071]317        if (this->ball_ != nullptr && this->center_ != nullptr)
[2825]318            this->ball_->setSpeed(this->center_->getBallSpeed());
319    }
[2890]320
[8108]321    /**
322    @brief
323        Get the left player.
324    @return
[11071]325        Returns a pointer to the player playing on the left. If there is no left player, nullptr is returned.
[8108]326    */
[2890]327    PlayerInfo* Pong::getLeftPlayer() const
328    {
[11083]329        if (this->bat_[0] != nullptr)
[2890]330            return this->bat_[0]->getPlayer();
331        else
[11071]332            return nullptr;
[2890]333    }
334
[8108]335    /**
336    @brief
337        Get the right player.
338    @return
[11071]339        Returns a pointer to the player playing on the right. If there is no right player, nullptr is returned.
[8108]340    */
[2890]341    PlayerInfo* Pong::getRightPlayer() const
342    {
[11083]343        if (this->bat_[1] != nullptr)
[2890]344            return this->bat_[1]->getPlayer();
345        else
[11071]346            return nullptr;
[2890]347    }
[2825]348}
Note: See TracBrowser for help on using the repository browser.