Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sfxThilo/src/modules/pong/PongBall.cc @ 9771

Last change on this file since 9771 was 9720, checked in by thiweber, 11 years ago

im Pong Score-Sound eingefuegt (PongBall.cc)

  • Property svn:eol-style set to native
File size: 12.3 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/**
30    @file PongBall.cc
31    @brief Implementation of the PongBall class.
32*/
33
34#include "PongBall.h"
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38
39#include "gametypes/Gametype.h"
40
41#include "PongBat.h"
42
43#include "sound/WorldSound.h" //Thilo
44#include "core/XMLPort.h"
45
46namespace orxonox
47{
48    RegisterClass(PongBall);
49
50    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
51
52    /**
53    @brief
54        Constructor. Registers and initializes the object.
55    */
56    PongBall::PongBall(Context* context)
57        : MovableEntity(context)
58    {
59        RegisterObject(PongBall);
60
61        this->speed_ = 0;
62        this->accelerationFactor_ = 1.0f;
63        this->bat_ = 0;
64        this->bDeleteBats_ = false;
65        this->batID_ = new unsigned int[2];
66        this->batID_[0] = OBJECTID_UNKNOWN;
67        this->batID_[1] = OBJECTID_UNKNOWN;
68        this->relMercyOffset_ = 0.05f;
69
70        this->registerVariables();
71
72        //Thilo
73        if (GameMode::isMaster())
74             {
75                 this->defScoreSound_ = new WorldSound(this->getContext());
76                 this->defScoreSound_->setLooping(false);
77                 this->defBatSound_ = new WorldSound(this->getContext());
78                 this->defBatSound_->setLooping(false);
79                 this->defBoundarySound_ = new WorldSound(this->getContext());
80                 this->defBoundarySound_->setLooping(false);
81             }
82             else
83             {
84                 this->defScoreSound_ = 0;
85             }
86    }
87
88    /**
89    @brief
90        Destructor.
91    */
92    PongBall::~PongBall()
93    {
94        if (this->isInitialized())
95        {
96            if (this->bDeleteBats_)
97                delete[] this->bat_;
98
99            delete[] this->batID_;
100        }
101    }
102
103    //Thilo
104    void PongBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
105    {
106        SUPER(PongBall, XMLPort, xmlelement, mode);
107        XMLPortParam(PongBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
108        XMLPortParam(PongBall, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
109        XMLPortParam(PongBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
110    }
111
112    /**
113    @brief
114        Register variables to synchronize over the network.
115    */
116    void PongBall::registerVariables()
117    {
118        registerVariable( this->fieldWidth_ );
119        registerVariable( this->fieldHeight_ );
120        registerVariable( this->batlength_ );
121        registerVariable( this->speed_ );
122        registerVariable( this->relMercyOffset_ );
123        registerVariable( this->batID_[0] );
124        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
125    }
126
127    /**
128    @brief
129        Is called every tick.
130        Handles the movement of the ball and its interaction with the boundaries and bats.
131    @param dt
132        The time since the last tick.
133    */
134    void PongBall::tick(float dt)
135    {
136        SUPER(PongBall, tick, dt);
137
138        // Get the current position, velocity and acceleration of the ball.
139        Vector3 position = this->getPosition();
140        Vector3 velocity = this->getVelocity();
141        Vector3 acceleration = this->getAcceleration();
142
143        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
144        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
145        {
146            // Its velocity in z-direction is inverted (i.e. it bounces off).
147            velocity.z = -velocity.z;
148            // And its position is set as to not overstep the boundary it has just crossed.
149            if (position.z > this->fieldHeight_ / 2)
150                position.z = this->fieldHeight_ / 2;
151            if (position.z < -this->fieldHeight_ / 2)
152                position.z = -this->fieldHeight_ / 2;
153
154            this->fireEvent();
155        }
156
157        // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry).
158        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
159        {
160            float distance = 0;
161
162            if (this->bat_ != NULL) // If there are bats.
163            {
164                // If the right boundary has been crossed.
165                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
166                {
167                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
168                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
169                    if (fabs(distance) <= 1) // If the bat is there to parry.
170                    {
171                        // Set the ball to be exactly at the boundary.
172                        position.x = this->fieldWidth_ / 2;
173                        // Invert its velocity in x-direction (i.e. it bounces off).
174                        velocity.x = -velocity.x;
175                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
176                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
177                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
178
179                        this->fireEvent();
180                    }
181                    // If the left player scores.
182                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
183                    {
184                        defScoreSound_->play();//Thilo
185                        if (this->getGametype() && this->bat_[0])
186                        {
187                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
188                            return;
189                        }
190                    }
191                }
192                // If the left boundary has been crossed.
193                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
194                {
195                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
196                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
197                    if (fabs(distance) <= 1) // If the bat is there to parry.
198                    {
199                        // Set the ball to be exactly at the boundary.
200                        position.x = -this->fieldWidth_ / 2;
201                        // Invert its velocity in x-direction (i.e. it bounces off).
202                        velocity.x = -velocity.x;
203                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
204                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
205                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
206
207                        this->fireEvent();
208                    }
209                    // If the right player scores.
210                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
211                    {
212                        defScoreSound_->play();//Thilo
213                        if (this->getGametype() && this->bat_[1])
214                        {
215                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
216                            return;
217                        }
218                    }
219                }
220            }
221        }
222
223        // Set the position, velocity and acceleration of the ball, if they have changed.
224        if (acceleration != this->getAcceleration())
225            this->setAcceleration(acceleration);
226        if (velocity != this->getVelocity())
227            this->setVelocity(velocity);
228        if (position != this->getPosition())
229            this->setPosition(position);
230    }
231
232    /**
233    @brief
234        Set the speed of the ball (in x-direction).
235    @param speed
236        The speed to be set.
237    */
238    void PongBall::setSpeed(float speed)
239    {
240        if (speed != this->speed_) // If the speed changes
241        {
242            this->speed_ = speed;
243
244            // Set the speed in the direction of the balls current velocity.
245            Vector3 velocity = this->getVelocity();
246            if (velocity.x != 0)
247                velocity.x = sgn(velocity.x) * this->speed_;
248            else // If the balls current velocity is zero, the speed is set in a random direction.
249                velocity.x = this->speed_ * sgn(rnd(-1,1));
250
251            this->setVelocity(velocity);
252        }
253    }
254
255    /**
256    @brief
257        Set the bats for the ball.
258    @param bats
259        An array (of size 2) of weak pointers, to be set as the new bats.
260    */
261    void PongBall::setBats(WeakPtr<PongBat>* bats)
262    {
263        if (this->bDeleteBats_) // If there are already some bats, delete them.
264        {
265            delete[] this->bat_;
266            this->bDeleteBats_ = false;
267        }
268
269        this->bat_ = bats;
270        // Also store their object IDs, for synchronization.
271        this->batID_[0] = this->bat_[0]->getObjectID();
272        this->batID_[1] = this->bat_[1]->getObjectID();
273    }
274
275    /**
276    @brief
277        Get the bats over the network.
278    */
279    void PongBall::applyBats()
280    {
281        // Make space for the bats, if they don't exist, yet.
282        if (this->bat_ == NULL)
283        {
284            this->bat_ = new WeakPtr<PongBat>[2];
285            this->bDeleteBats_ = true;
286        }
287
288        if (this->batID_[0] != OBJECTID_UNKNOWN)
289            this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
290        if (this->batID_[1] != OBJECTID_UNKNOWN)
291            this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
292    }
293
294    void PongBall::setDefScoreSound(const std::string &pongSound)
295    {
296        if( defScoreSound_ )
297            defScoreSound_->setSource(pongSound);
298        else
299            assert(0); // This should never happen, because soundpointer is only available on master
300    }
301
302    const std::string& PongBall::getDefScoreSound()
303    {
304        if( defScoreSound_ )
305            return defScoreSound_->getSource();
306        else
307            assert(0);
308        return BLANKSTRING;
309    }
310
311    void PongBall::setDefBatSound(const std::string &pongSound)
312    {
313        if( defBatSound_ )
314            defBatSound_->setSource(pongSound);
315        else
316            assert(0); // This should never happen, because soundpointer is only available on master
317    }
318
319    const std::string& PongBall::getDefBatSound()
320    {
321        if( defBatSound_ )
322            return defBatSound_->getSource();
323        else
324            assert(0);
325        return BLANKSTRING;
326    }
327
328    void PongBall::setDefBoundarySound(const std::string &pongSound)
329    {
330        if( defBoundarySound_ )
331            defBoundarySound_->setSource(pongSound);
332        else
333            assert(0); // This should never happen, because soundpointer is only available on master
334    }
335
336    const std::string& PongBall::getDefBoundarySound()
337    {
338        if( defBoundarySound_ )
339            return defBoundarySound_->getSource();
340        else
341            assert(0);
342        return BLANKSTRING;
343    }
344}
Note: See TracBrowser for help on using the repository browser.