Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS09/src/modules/pong/Pong.cc @ 9098

Last change on this file since 9098 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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