Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoBlox_FS19/src/modules/OrxoBlox/OrxoBloxBall.cc @ 12305

Last change on this file since 12305 was 12305, checked in by jeromela, 5 years ago

Ball bewegt sich in zwei Richtungen

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