Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/pong/Pong.cc @ 5889

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

Using named events in Pong. Also firing events in PongBall now.

  • Property svn:eol-style set to native
File size: 6.1 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/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    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);
78            this->ball_->setBatLength(this->center_->getBatLength());
79
80            if (!this->bat_[0])
81            {
82                this->bat_[0] = new PongBat(this->center_);
83                this->bat_[0]->addTemplate(this->center_->getBattemplate());
84            }
85            if (!this->bat_[1])
86            {
87                this->bat_[1] = new PongBat(this->center_);
88                this->bat_[1]->addTemplate(this->center_->getBattemplate());
89            }
90
91            this->center_->attach(this->bat_[0]);
92            this->center_->attach(this->bat_[1]);
93            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
94            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
95            this->bat_[0]->yaw(Degree(-90));
96            this->bat_[1]->yaw(Degree(90));
97            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
98            this->bat_[1]->setSpeed(this->center_->getBatSpeed());
99            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
100            this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y);
101            this->bat_[0]->setLength(this->center_->getBatLength());
102            this->bat_[1]->setLength(this->center_->getBatLength());
103
104            this->ball_->setBats(this->bat_);
105        }
106        else
107        {
108            COUT(1) << "Error: No Centerpoint specified." << std::endl;
109        }
110
111        this->starttimer_.startTimer();
112
113
114        bool temp = this->bForceSpawn_;
115        this->bForceSpawn_ = true;
116
117        Deathmatch::start();
118
119        this->bForceSpawn_ = temp;
120    }
121
122    void Pong::end()
123    {
124        if (this->ball_)
125        {
126            this->ball_->destroy();
127            this->ball_ = 0;
128        }
129
130        Deathmatch::end();
131    }
132
133    void Pong::spawnPlayer(PlayerInfo* player)
134    {
135        if (!this->bat_[0]->getPlayer())
136        {
137            player->startControl(this->bat_[0]);
138            this->players_[player].state_ = PlayerState::Alive;
139        }
140        else if (!this->bat_[1]->getPlayer())
141        {
142            player->startControl(this->bat_[1]);
143            this->players_[player].state_ = PlayerState::Alive;
144        }
145        else
146            return;
147
148        if (player && player->getController() && player->getController()->isA(Class(PongAI)))
149        {
150            PongAI* ai = orxonox_cast<PongAI*>(player->getController());
151            ai->setPongBall(this->ball_);
152        }
153    }
154
155    void Pong::playerScored(PlayerInfo* player)
156    {
157        Deathmatch::playerScored(player);
158
159        if (this->center_)
160        {
161            if (player == this->getRightPlayer())
162                this->center_->fireEvent(EventName(PongCenterpoint, right));
163            else if (player == this->getLeftPlayer())
164                this->center_->fireEvent(EventName(PongCenterpoint, left));
165           
166            if (player)
167                this->gtinfo_->sendAnnounceMessage(player->getName() + " scored");
168        }
169
170        if (this->ball_)
171        {
172            this->ball_->setPosition(Vector3::ZERO);
173            this->ball_->setVelocity(Vector3::ZERO);
174            this->ball_->setSpeed(0);
175        }
176
177        if (this->bat_[0] && this->bat_[1])
178        {
179            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
180            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
181        }
182
183        this->starttimer_.startTimer();
184    }
185
186    void Pong::startBall()
187    {
188        if (this->ball_ && this->center_)
189            this->ball_->setSpeed(this->center_->getBallSpeed());
190    }
191
192    PlayerInfo* Pong::getLeftPlayer() const
193    {
194        if (this->bat_ && this->bat_[0])
195            return this->bat_[0]->getPlayer();
196        else
197            return 0;
198    }
199
200    PlayerInfo* Pong::getRightPlayer() const
201    {
202        if (this->bat_ && this->bat_[1])
203            return this->bat_[1]->getPlayer();
204        else
205            return 0;
206    }
207}
Note: See TracBrowser for help on using the repository browser.