Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12340 was 12340, checked in by pomselj, 5 years ago

Some changes in bounce method + added destroy in bounce method to be able to destroy stones

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