Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pong/PongBall.cc @ 9232

Last change on this file since 9232 was 8108, checked in by dafrick, 14 years ago

Merging changes from tetris branch into trunk, since they are also useful, there.

  • Property svn:eol-style set to native
File size: 9.7 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
43namespace orxonox
44{
45    CreateFactory(PongBall);
46
[2885]47    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
48
[8108]49    /**
50    @brief
51        Constructor. Registers and initializes the object.
52    */
[5929]53    PongBall::PongBall(BaseObject* creator)
54        : MovableEntity(creator)
[2825]55    {
56        RegisterObject(PongBall);
57
58        this->speed_ = 0;
[5929]59        this->accelerationFactor_ = 1.0f;
[2825]60        this->bat_ = 0;
[7885]61        this->bDeleteBats_ = false;
[3084]62        this->batID_ = new unsigned int[2];
63        this->batID_[0] = OBJECTID_UNKNOWN;
64        this->batID_[1] = OBJECTID_UNKNOWN;
[3196]65        this->relMercyOffset_ = 0.05f;
[3108]66
[3084]67        this->registerVariables();
[5929]68    }
[3108]69
[8108]70    /**
71    @brief
72        Destructor.
73    */
[5929]74    PongBall::~PongBall()
75    {
[7885]76        if (this->isInitialized())
77        {
78            if (this->bDeleteBats_)
79                delete[] this->bat_;
80
81            delete[] this->batID_;
82        }
[2825]83    }
[3108]84
[8108]85    /**
86    @brief
87        Register variables to synchronize over the network.
88    */
[3084]89    void PongBall::registerVariables()
90    {
91        registerVariable( this->fieldWidth_ );
92        registerVariable( this->fieldHeight_ );
93        registerVariable( this->batlength_ );
94        registerVariable( this->speed_ );
95        registerVariable( this->relMercyOffset_ );
96        registerVariable( this->batID_[0] );
[3280]97        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
[3084]98    }
[2825]99
[8108]100    /**
101    @brief
102        Is called every tick.
103        Handles the movement of the ball and its interaction with the boundaries and bats.
104    @param dt
105        The time since the last tick.
106    */
[2825]107    void PongBall::tick(float dt)
108    {
109        SUPER(PongBall, tick, dt);
110
[8108]111        // Get the current position, velocity and acceleration of the ball.
[5929]112        Vector3 position = this->getPosition();
113        Vector3 velocity = this->getVelocity();
114        Vector3 acceleration = this->getAcceleration();
115
[8108]116        // 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]117        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
[2825]118        {
[8108]119            // Its velocity in z-direction is inverted (i.e. it bounces off).
[5929]120            velocity.z = -velocity.z;
[8108]121            // And its position is set as to not overstep the boundary it has just crossed.
[5929]122            if (position.z > this->fieldHeight_ / 2)
123                position.z = this->fieldHeight_ / 2;
124            if (position.z < -this->fieldHeight_ / 2)
125                position.z = -this->fieldHeight_ / 2;
[2825]126
[5929]127            this->fireEvent();
128        }
[2825]129
[8108]130        // 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]131        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
132        {
133            float distance = 0;
[2825]134
[8108]135            if (this->bat_ != NULL) // If there are bats.
[2825]136            {
[8108]137                // If the right boundary has been crossed.
138                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
[2825]139                {
[8108]140                    // 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]141                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
[8108]142                    if (fabs(distance) <= 1) // If the bat is there to parry.
[2825]143                    {
[8108]144                        // Set the ball to be exactly at the boundary.
[5929]145                        position.x = this->fieldWidth_ / 2;
[8108]146                        // Invert its velocity in x-direction (i.e. it bounces off).
[5929]147                        velocity.x = -velocity.x;
[8108]148                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
[5929]149                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
150                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
[6417]151
[5929]152                        this->fireEvent();
153                    }
[8108]154                    // If the left player scores.
[5929]155                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
156                    {
157                        if (this->getGametype() && this->bat_[0])
[2825]158                        {
[5929]159                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
160                            return;
[2825]161                        }
162                    }
[5929]163                }
[8108]164                // If the left boundary has been crossed.
165                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
[5929]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_[0]->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_[0]->getVelocity() * this->accelerationFactor_ * -1;
178
179                        this->fireEvent();
180                    }
[8108]181                    // If the right player scores.
[5929]182                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
183                    {
184                        if (this->getGametype() && this->bat_[1])
[2825]185                        {
[5929]186                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
187                            return;
[2825]188                        }
189                    }
190                }
191            }
192        }
[3084]193
[8108]194        // Set the position, velocity and acceleration of the ball, if they have changed.
[5929]195        if (acceleration != this->getAcceleration())
196            this->setAcceleration(acceleration);
197        if (velocity != this->getVelocity())
[3084]198            this->setVelocity(velocity);
[5929]199        if (position != this->getPosition())
[3084]200            this->setPosition(position);
[2825]201    }
202
[8108]203    /**
204    @brief
205        Set the speed of the ball (in x-direction).
206    @param speed
207        The speed to be set.
208    */
[2825]209    void PongBall::setSpeed(float speed)
210    {
[8108]211        if (speed != this->speed_) // If the speed changes
[2825]212        {
213            this->speed_ = speed;
214
[8108]215            // Set the speed in the direction of the balls current velocity.
[2825]216            Vector3 velocity = this->getVelocity();
217            if (velocity.x != 0)
218                velocity.x = sgn(velocity.x) * this->speed_;
[8108]219            else // If the balls current velocity is zero, the speed is set in a random direction.
[2825]220                velocity.x = this->speed_ * sgn(rnd(-1,1));
221
222            this->setVelocity(velocity);
223        }
224    }
[3196]225
[8108]226    /**
227    @brief
228        Set the bats for the ball.
229    @param bats
230        An array (of size 2) of weak pointers, to be set as the new bats.
231    */
[7852]232    void PongBall::setBats(WeakPtr<PongBat>* bats)
[3196]233    {
[8108]234        if (this->bDeleteBats_) // If there are already some bats, delete them.
[7885]235        {
236            delete[] this->bat_;
237            this->bDeleteBats_ = false;
238        }
239
[3196]240        this->bat_ = bats;
[8108]241        // Also store their object IDs, for synchronization.
[3196]242        this->batID_[0] = this->bat_[0]->getObjectID();
243        this->batID_[1] = this->bat_[1]->getObjectID();
244    }
245
[8108]246    /**
247    @brief
248        Get the bats over the network.
249    */
[3196]250    void PongBall::applyBats()
251    {
[8108]252        // Make space for the bats, if they don't exist, yet.
253        if (this->bat_ == NULL)
[7885]254        {
255            this->bat_ = new WeakPtr<PongBat>[2];
[7886]256            this->bDeleteBats_ = true;
[7885]257        }
258
[3196]259        if (this->batID_[0] != OBJECTID_UNKNOWN)
[3325]260            this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
[3196]261        if (this->batID_[1] != OBJECTID_UNKNOWN)
[3325]262            this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
[3196]263    }
[2825]264}
Note: See TracBrowser for help on using the repository browser.