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
RevLine 
[2825]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
[8108]29/**
30    @file PongBall.cc
31    @brief Implementation of the PongBall class.
32*/
33
[2825]34#include "PongBall.h"
35
36#include "core/CoreIncludes.h"
[2896]37#include "core/GameMode.h"
[8108]38
[5735]39#include "gametypes/Gametype.h"
[8108]40
[5725]41#include "PongBat.h"
[2825]42
[9720]43#include "sound/WorldSound.h" //Thilo
44#include "core/XMLPort.h"
45
[2825]46namespace orxonox
47{
[9667]48    RegisterClass(PongBall);
[2825]49
[2885]50    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
51
[8108]52    /**
53    @brief
54        Constructor. Registers and initializes the object.
55    */
[9667]56    PongBall::PongBall(Context* context)
57        : MovableEntity(context)
[2825]58    {
59        RegisterObject(PongBall);
60
61        this->speed_ = 0;
[5929]62        this->accelerationFactor_ = 1.0f;
[2825]63        this->bat_ = 0;
[7885]64        this->bDeleteBats_ = false;
[3084]65        this->batID_ = new unsigned int[2];
66        this->batID_[0] = OBJECTID_UNKNOWN;
67        this->batID_[1] = OBJECTID_UNKNOWN;
[3196]68        this->relMercyOffset_ = 0.05f;
[3108]69
[3084]70        this->registerVariables();
[9720]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             }
[5929]86    }
[3108]87
[8108]88    /**
89    @brief
90        Destructor.
91    */
[5929]92    PongBall::~PongBall()
93    {
[7885]94        if (this->isInitialized())
95        {
96            if (this->bDeleteBats_)
97                delete[] this->bat_;
98
99            delete[] this->batID_;
100        }
[2825]101    }
[3108]102
[9720]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
[8108]112    /**
113    @brief
114        Register variables to synchronize over the network.
115    */
[3084]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] );
[3280]124        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
[3084]125    }
[2825]126
[8108]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    */
[2825]134    void PongBall::tick(float dt)
135    {
136        SUPER(PongBall, tick, dt);
137
[8108]138        // Get the current position, velocity and acceleration of the ball.
[5929]139        Vector3 position = this->getPosition();
140        Vector3 velocity = this->getVelocity();
141        Vector3 acceleration = this->getAcceleration();
142
[8108]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).
[5929]144        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
[2825]145        {
[8108]146            // Its velocity in z-direction is inverted (i.e. it bounces off).
[5929]147            velocity.z = -velocity.z;
[8108]148            // And its position is set as to not overstep the boundary it has just crossed.
[5929]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;
[2825]153
[5929]154            this->fireEvent();
155        }
[2825]156
[8108]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).
[5929]158        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
159        {
160            float distance = 0;
[2825]161
[8108]162            if (this->bat_ != NULL) // If there are bats.
[2825]163            {
[8108]164                // If the right boundary has been crossed.
165                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
[2825]166                {
[8108]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%)
[5929]168                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
[8108]169                    if (fabs(distance) <= 1) // If the bat is there to parry.
[2825]170                    {
[8108]171                        // Set the ball to be exactly at the boundary.
[5929]172                        position.x = this->fieldWidth_ / 2;
[8108]173                        // Invert its velocity in x-direction (i.e. it bounces off).
[5929]174                        velocity.x = -velocity.x;
[8108]175                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
[5929]176                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
177                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
[6417]178
[5929]179                        this->fireEvent();
180                    }
[8108]181                    // If the left player scores.
[5929]182                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
183                    {
[9720]184                        defScoreSound_->play();//Thilo
[5929]185                        if (this->getGametype() && this->bat_[0])
[2825]186                        {
[5929]187                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
188                            return;
[2825]189                        }
190                    }
[5929]191                }
[8108]192                // If the left boundary has been crossed.
193                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
[5929]194                {
[8108]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%)
[5929]196                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
[8108]197                    if (fabs(distance) <= 1) // If the bat is there to parry.
[2825]198                    {
[8108]199                        // Set the ball to be exactly at the boundary.
[5929]200                        position.x = -this->fieldWidth_ / 2;
[8108]201                        // Invert its velocity in x-direction (i.e. it bounces off).
[5929]202                        velocity.x = -velocity.x;
[8108]203                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
[5929]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                    }
[8108]209                    // If the right player scores.
[5929]210                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
211                    {
[9720]212                        defScoreSound_->play();//Thilo
[5929]213                        if (this->getGametype() && this->bat_[1])
[2825]214                        {
[5929]215                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
216                            return;
[2825]217                        }
218                    }
219                }
220            }
221        }
[3084]222
[8108]223        // Set the position, velocity and acceleration of the ball, if they have changed.
[5929]224        if (acceleration != this->getAcceleration())
225            this->setAcceleration(acceleration);
226        if (velocity != this->getVelocity())
[3084]227            this->setVelocity(velocity);
[5929]228        if (position != this->getPosition())
[3084]229            this->setPosition(position);
[2825]230    }
231
[8108]232    /**
233    @brief
234        Set the speed of the ball (in x-direction).
235    @param speed
236        The speed to be set.
237    */
[2825]238    void PongBall::setSpeed(float speed)
239    {
[8108]240        if (speed != this->speed_) // If the speed changes
[2825]241        {
242            this->speed_ = speed;
243
[8108]244            // Set the speed in the direction of the balls current velocity.
[2825]245            Vector3 velocity = this->getVelocity();
246            if (velocity.x != 0)
247                velocity.x = sgn(velocity.x) * this->speed_;
[8108]248            else // If the balls current velocity is zero, the speed is set in a random direction.
[2825]249                velocity.x = this->speed_ * sgn(rnd(-1,1));
250
251            this->setVelocity(velocity);
252        }
253    }
[3196]254
[8108]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    */
[7852]261    void PongBall::setBats(WeakPtr<PongBat>* bats)
[3196]262    {
[8108]263        if (this->bDeleteBats_) // If there are already some bats, delete them.
[7885]264        {
265            delete[] this->bat_;
266            this->bDeleteBats_ = false;
267        }
268
[3196]269        this->bat_ = bats;
[8108]270        // Also store their object IDs, for synchronization.
[3196]271        this->batID_[0] = this->bat_[0]->getObjectID();
272        this->batID_[1] = this->bat_[1]->getObjectID();
273    }
274
[8108]275    /**
276    @brief
277        Get the bats over the network.
278    */
[3196]279    void PongBall::applyBats()
280    {
[8108]281        // Make space for the bats, if they don't exist, yet.
282        if (this->bat_ == NULL)
[7885]283        {
284            this->bat_ = new WeakPtr<PongBat>[2];
[7886]285            this->bDeleteBats_ = true;
[7885]286        }
287
[3196]288        if (this->batID_[0] != OBJECTID_UNKNOWN)
[3325]289            this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
[3196]290        if (this->batID_[1] != OBJECTID_UNKNOWN)
[3325]291            this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
[3196]292    }
[9720]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    }
[2825]344}
Note: See TracBrowser for help on using the repository browser.