Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/modules/pong/Pong.cc @ 10108

Last change on this file since 10108 was 7911, checked in by landauf, 14 years ago

Pong gametype should delete the objects it created

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