Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pong/Pong.cc @ 7869

Last change on this file since 7869 was 7865, checked in by landauf, 14 years ago

renamed pong level, added a bot, and changed the pong gametype to spawn human players before bots (player will always get the left bat in singleplayer)

  • Property svn:eol-style set to native
File size: 6.9 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
29#include "Pong.h"
30
31#include "core/CoreIncludes.h"
[5929]32#include "core/EventIncludes.h"
[7284]33#include "core/command/Executor.h"
[5725]34#include "PongCenterpoint.h"
35#include "PongBall.h"
36#include "PongBat.h"
37#include "PongBot.h"
38#include "PongAI.h"
[2825]39
40namespace orxonox
41{
[5929]42    CreateEventName(PongCenterpoint, right);
43    CreateEventName(PongCenterpoint, left);
[6417]44
[2825]45    CreateUnloadableFactory(Pong);
46
47    Pong::Pong(BaseObject* creator) : Deathmatch(creator)
48    {
49        RegisterObject(Pong);
50
51        this->center_ = 0;
52        this->ball_ = 0;
53        this->bat_[0] = 0;
54        this->bat_[1] = 0;
55
[2890]56        this->setHUDTemplate("PongHUD");
57
[5929]58        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this)));
[2825]59        this->starttimer_.stopTimer();
[2839]60
61        this->botclass_ = Class(PongBot);
[2825]62    }
63
64    void Pong::start()
65    {
66        if (this->center_)
67        {
68            if (!this->ball_)
69            {
70                this->ball_ = new PongBall(this->center_);
71                this->ball_->addTemplate(this->center_->getBalltemplate());
72            }
73
74            this->center_->attach(this->ball_);
75            this->ball_->setPosition(0, 0, 0);
76            this->ball_->setFieldDimension(this->center_->getFieldDimension());
77            this->ball_->setSpeed(0);
[5929]78            this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor());
[2825]79            this->ball_->setBatLength(this->center_->getBatLength());
80
81            if (!this->bat_[0])
82            {
83                this->bat_[0] = new PongBat(this->center_);
84                this->bat_[0]->addTemplate(this->center_->getBattemplate());
85            }
86            if (!this->bat_[1])
87            {
88                this->bat_[1] = new PongBat(this->center_);
89                this->bat_[1]->addTemplate(this->center_->getBattemplate());
90            }
91
92            this->center_->attach(this->bat_[0]);
93            this->center_->attach(this->bat_[1]);
94            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
95            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
96            this->bat_[0]->yaw(Degree(-90));
97            this->bat_[1]->yaw(Degree(90));
98            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
99            this->bat_[1]->setSpeed(this->center_->getBatSpeed());
100            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
101            this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y);
102            this->bat_[0]->setLength(this->center_->getBatLength());
103            this->bat_[1]->setLength(this->center_->getBatLength());
104
105            this->ball_->setBats(this->bat_);
106        }
107        else
108        {
109            COUT(1) << "Error: No Centerpoint specified." << std::endl;
110        }
111
112        this->starttimer_.startTimer();
113
[2885]114
115        bool temp = this->bForceSpawn_;
116        this->bForceSpawn_ = true;
117
[2825]118        Deathmatch::start();
[2885]119
120        this->bForceSpawn_ = temp;
[2825]121    }
122
123    void Pong::end()
124    {
125        if (this->ball_)
126        {
[5929]127            this->ball_->destroy();
[2825]128            this->ball_ = 0;
129        }
130
131        Deathmatch::end();
132    }
133
[7865]134    void Pong::spawnPlayersIfRequested()
135    {
136        // first spawn human players to assign always the left bat to the player in singleplayer
137        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
138            if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
139                this->spawnPlayer(it->first);
140        // now spawn bots
141        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
142            if (!it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
143                this->spawnPlayer(it->first);
144    }
145
[2825]146    void Pong::spawnPlayer(PlayerInfo* player)
147    {
148        if (!this->bat_[0]->getPlayer())
149        {
150            player->startControl(this->bat_[0]);
151            this->players_[player].state_ = PlayerState::Alive;
152        }
153        else if (!this->bat_[1]->getPlayer())
154        {
155            player->startControl(this->bat_[1]);
156            this->players_[player].state_ = PlayerState::Alive;
157        }
[2839]158        else
159            return;
160
161        if (player && player->getController() && player->getController()->isA(Class(PongAI)))
162        {
[3325]163            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
[2839]164            ai->setPongBall(this->ball_);
165        }
[2825]166    }
167
168    void Pong::playerScored(PlayerInfo* player)
169    {
170        Deathmatch::playerScored(player);
171
[2872]172        if (this->center_)
173        {
[5929]174            if (player == this->getRightPlayer())
175                this->center_->fireEvent(FireEventName(PongCenterpoint, right));
176            else if (player == this->getLeftPlayer())
177                this->center_->fireEvent(FireEventName(PongCenterpoint, left));
[6417]178
[3099]179            if (player)
[5929]180                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
[2872]181        }
182
[2825]183        if (this->ball_)
184        {
185            this->ball_->setPosition(Vector3::ZERO);
186            this->ball_->setVelocity(Vector3::ZERO);
[5929]187            this->ball_->setAcceleration(Vector3::ZERO);
[2825]188            this->ball_->setSpeed(0);
189        }
190
191        if (this->bat_[0] && this->bat_[1])
192        {
193            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
194            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
195        }
196
197        this->starttimer_.startTimer();
198    }
199
200    void Pong::startBall()
201    {
202        if (this->ball_ && this->center_)
203            this->ball_->setSpeed(this->center_->getBallSpeed());
204    }
[2890]205
206    PlayerInfo* Pong::getLeftPlayer() const
207    {
208        if (this->bat_ && this->bat_[0])
209            return this->bat_[0]->getPlayer();
210        else
211            return 0;
212    }
213
214    PlayerInfo* Pong::getRightPlayer() const
215    {
216        if (this->bat_ && this->bat_[1])
217            return this->bat_[1]->getPlayer();
218        else
219            return 0;
220    }
[2825]221}
Note: See TracBrowser for help on using the repository browser.