Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/gametypes/Pong.cc @ 3110

Last change on this file since 3110 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

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