Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2011/src/modules/pong/Pong.cc @ 8986

Last change on this file since 8986 was 8980, checked in by jo, 13 years ago

teamgametype merged into trunk

  • Property svn:eol-style set to native
File size: 11.4 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"
[8980]39#include "core/ConfigValueIncludes.h"
[8108]40
41#include "gamestates/GSLevel.h"
[8980]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"
[2825]49namespace orxonox
50{
[8108]51    // Events to allow to react to scoring of a player, in the level-file.
[5929]52    CreateEventName(PongCenterpoint, right);
53    CreateEventName(PongCenterpoint, left);
[6417]54
[2825]55    CreateUnloadableFactory(Pong);
56
[8108]57    /**
58    @brief
59        Constructor. Registers and initializes the object.
60    */
[2825]61    Pong::Pong(BaseObject* creator) : Deathmatch(creator)
62    {
63        RegisterObject(Pong);
64
65        this->center_ = 0;
66        this->ball_ = 0;
67        this->bat_[0] = 0;
68        this->bat_[1] = 0;
69
[2890]70        this->setHUDTemplate("PongHUD");
71
[8108]72        // Pre-set the timer, but don't start it yet.
[5929]73        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this)));
[2825]74        this->starttimer_.stopTimer();
[2839]75
[8108]76        // Set the type of Bots for this particular Gametype.
[2839]77        this->botclass_ = Class(PongBot);
[8980]78        this->scoreLimit_ = 10;
79        this->setConfigValues();
[2825]80    }
81
[8108]82    /**
83    @brief
84        Destructor. Cleans up, if initialized.
85    */
[7911]86    Pong::~Pong()
87    {
88        if (this->isInitialized())
89            this->cleanup();
90    }
91
[8108]92    /**
93    @brief
94        Cleans up the Gametype by destroying the ball and the bats.
95    */
[7911]96    void Pong::cleanup()
97    {
[8108]98        if (this->ball_ != NULL) // Destroy the ball, if present.
[7911]99        {
100            this->ball_->destroy();
101            this->ball_ = 0;
102        }
103
[8108]104        // Destroy both bats, if present.
[7911]105        for (size_t i = 0; i < 2; ++i)
106        {
[8108]107            if (this->bat_[0] != NULL)
[7911]108            {
109                this->bat_[0]->destroy();
110                this->bat_[0] = 0;
111            }
112        }
113    }
114
[8108]115    /**
116    @brief
117        Starts the Pong minigame.
118    */
[2825]119    void Pong::start()
120    {
[8108]121        if (this->center_ != NULL) // There needs to be a PongCenterpoint, i.e. the area the game takes place.
[2825]122        {
[8108]123            if (this->ball_ == NULL) // If there is no ball, create a new ball.
[2825]124            {
125                this->ball_ = new PongBall(this->center_);
[8108]126                // Apply the template for the ball specified by the centerpoint.
[2825]127                this->ball_->addTemplate(this->center_->getBalltemplate());
128            }
129
[8108]130            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
[2825]131            this->center_->attach(this->ball_);
132            this->ball_->setPosition(0, 0, 0);
133            this->ball_->setFieldDimension(this->center_->getFieldDimension());
134            this->ball_->setSpeed(0);
[5929]135            this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor());
[2825]136            this->ball_->setBatLength(this->center_->getBatLength());
137
[8108]138            // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint.
139            for (size_t i = 0; i < 2; ++i)
[2825]140            {
[8108]141                if (this->bat_[i] == NULL)
142                {
143                    this->bat_[i] = new PongBat(this->center_);
144                    this->bat_[i]->addTemplate(this->center_->getBattemplate());
145                }
[2825]146            }
147
[8108]148            // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to.
[2825]149            this->center_->attach(this->bat_[0]);
150            this->center_->attach(this->bat_[1]);
151            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
152            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
153            this->bat_[0]->yaw(Degree(-90));
154            this->bat_[1]->yaw(Degree(90));
155            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
156            this->bat_[1]->setSpeed(this->center_->getBatSpeed());
157            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
158            this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y);
159            this->bat_[0]->setLength(this->center_->getBatLength());
160            this->bat_[1]->setLength(this->center_->getBatLength());
161
[8108]162            // Set the bats for the ball.
[2825]163            this->ball_->setBats(this->bat_);
164        }
[8108]165        else // If no centerpoint was specified, an error is thrown and the level is exited.
[2825]166        {
[8858]167            orxout(internal_error) << "Pong: No Centerpoint specified." << endl;
[8108]168            GSLevel::startMainMenu();
169            return;
[2825]170        }
171
[8108]172        // Start the timer. After it has expired the ball is started.
[2825]173        this->starttimer_.startTimer();
174
[8108]175        // Set variable to temporarily force the player to spawn.
[2885]176        bool temp = this->bForceSpawn_;
177        this->bForceSpawn_ = true;
178
[8108]179        // Call start for the parent class.
[2825]180        Deathmatch::start();
[2885]181
[8108]182        // Reset the variable.
[2885]183        this->bForceSpawn_ = temp;
[2825]184    }
185
[8108]186    /**
187    @brief
188        Ends the Pong minigame.
189    */
[2825]190    void Pong::end()
191    {
[7911]192        this->cleanup();
[2825]193
[8108]194        // Call end for the parent class.
[2825]195        Deathmatch::end();
196    }
197
[8108]198    /**
199    @brief
200        Spawns players, and fills the rest up with bots.
201    */
[7865]202    void Pong::spawnPlayersIfRequested()
203    {
204        // first spawn human players to assign always the left bat to the player in singleplayer
205        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
206            if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
207                this->spawnPlayer(it->first);
208        // now spawn bots
209        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
210            if (!it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
211                this->spawnPlayer(it->first);
212    }
213
[8108]214    /**
215    @brief
216        Spawns the input player.
217    @param player
218        The player to be spawned.
219    */
[2825]220    void Pong::spawnPlayer(PlayerInfo* player)
221    {
[8108]222        assert(player);
223
224        // If the first (left) bat has no player.
225        if (this->bat_[0]->getPlayer() == NULL)
[2825]226        {
227            player->startControl(this->bat_[0]);
228            this->players_[player].state_ = PlayerState::Alive;
229        }
[8108]230        // If the second (right) bat has no player.
231        else if (this->bat_[1]->getPlayer() == NULL)
[2825]232        {
233            player->startControl(this->bat_[1]);
234            this->players_[player].state_ = PlayerState::Alive;
235        }
[8108]236        // If both bats are taken.
[2839]237        else
238            return;
239
[8108]240        // If the player is an AI, it receives a pointer to the ball.
241        if (player->getController() != NULL && player->getController()->isA(Class(PongAI)))
[2839]242        {
[3325]243            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
[2839]244            ai->setPongBall(this->ball_);
245        }
[2825]246    }
247
[8108]248    /**
249    @brief
250        Is called when the player scored.
251    */
[2825]252    void Pong::playerScored(PlayerInfo* player)
253    {
254        Deathmatch::playerScored(player);
255
[8108]256        if (this->center_ != NULL) // If there is a centerpoint.
[2872]257        {
[8108]258            // 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]259            if (player == this->getRightPlayer())
260                this->center_->fireEvent(FireEventName(PongCenterpoint, right));
261            else if (player == this->getLeftPlayer())
262                this->center_->fireEvent(FireEventName(PongCenterpoint, left));
[6417]263
[8108]264            // Also announce, that the player has scored.
265            if (player != NULL)
[5929]266                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
[2872]267        }
268
[8108]269        // If there is a ball present, reset its position, velocity and acceleration.
270        if (this->ball_ != NULL)
[2825]271        {
272            this->ball_->setPosition(Vector3::ZERO);
273            this->ball_->setVelocity(Vector3::ZERO);
[5929]274            this->ball_->setAcceleration(Vector3::ZERO);
[2825]275            this->ball_->setSpeed(0);
276        }
277
[8108]278        // If there are bats reset them to the middle position.
279        if (this->bat_[0] != NULL && this->bat_[1] != NULL)
[2825]280        {
281            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
282            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
283        }
284
[8980]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);
302             this->end();
303        }
[8108]304        // Restart the timer to start the ball.
[2825]305        this->starttimer_.startTimer();
[8980]306
[2825]307    }
308
[8108]309    /**
310    @brief
311        Starts the ball with some default speed.
312    */
[2825]313    void Pong::startBall()
314    {
[8108]315        if (this->ball_ != NULL && this->center_ != NULL)
[2825]316            this->ball_->setSpeed(this->center_->getBallSpeed());
317    }
[2890]318
[8108]319    /**
320    @brief
321        Get the left player.
322    @return
323        Returns a pointer to the player playing on the left. If there is no left player, NULL is returned.
324    */
[2890]325    PlayerInfo* Pong::getLeftPlayer() const
326    {
[8108]327        if (this->bat_ != NULL && this->bat_[0] != NULL)
[2890]328            return this->bat_[0]->getPlayer();
329        else
330            return 0;
331    }
332
[8108]333    /**
334    @brief
335        Get the right player.
336    @return
337        Returns a pointer to the player playing on the right. If there is no right player, NULL is returned.
338    */
[2890]339    PlayerInfo* Pong::getRightPlayer() const
340    {
[8108]341        if (this->bat_ != NULL && this->bat_[1] != NULL)
[2890]342            return this->bat_[1]->getPlayer();
343        else
344            return 0;
345    }
[8980]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    }
[2825]355}
Note: See TracBrowser for help on using the repository browser.