Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12361 was 12361, checked in by ahuwyler, 5 years ago

Nomal fuer de Jerome

File size: 11.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 OrxoBloxBall.cc
31    @brief Implementation of the OrxoBloxBall class.
32*/
33
34#include "OrxoBloxBall.h"
35#include "OrxoBloxStones.h"
36#include "OrxoBlox.h"
37#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
38
39#include "core/CoreIncludes.h"
40#include "core/GameMode.h"
41
42#include "gametypes/Gametype.h"
43
44
45#include "sound/WorldSound.h"
46#include "core/XMLPort.h"
47
48
49namespace orxonox
50{
51    RegisterClass(OrxoBloxBall);
52
53    const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5;
54
55    /**
56    @brief
57        Constructor. Registers and initializes the object.
58    */
59    OrxoBloxBall::OrxoBloxBall(Context* context)
60        : Pawn(context)
61    {
62        RegisterObject(OrxoBloxBall);
63
64        this->speed_ = 0;
65        this->accelerationFactor_ = 1.0f;
66        this->bDeleteBats_ = false;
67        this->relMercyOffset_ = 0.05f;
68        this->orxoblox_ = this->getOrxoBlox();
69        this->radius_ = 1.5;
70
71        this->registerVariables();
72
73        //initialize sound
74        if (GameMode::isMaster())
75             {
76                 this->defScoreSound_ = new WorldSound(this->getContext());
77                 this->defScoreSound_->setVolume(1.0f);
78                 this->defBatSound_ = new WorldSound(this->getContext());
79                 this->defBatSound_->setVolume(0.4f);
80                 this->defBoundarySound_ = new WorldSound(this->getContext());
81                 this->defBoundarySound_->setVolume(0.5f);
82             }
83             else
84             {
85                 this->defScoreSound_ = nullptr;
86                 this->defBatSound_ = nullptr;
87                 this->defBoundarySound_ = nullptr;
88             }
89    }
90
91    /**
92    @brief
93        Destructor.
94    */
95    OrxoBloxBall::~OrxoBloxBall()
96    {
97        if (this->isInitialized())
98        {
99            if (this->bDeleteBats_)
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    }
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 OrxoBloxBall::tick(float dt)
135    {
136        SUPER(OrxoBloxBall, 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            defBoundarySound_->play(); //play boundary sound
147            // Its velocity in z-direction is inverted (i.e. it bounces off).
148            velocity.z = -velocity.z;
149            // And its position is set as to not overstep the boundary it has just crossed.
150            if (position.z > this->fieldHeight_ / 2){
151                // Set the ball to be exactly at the boundary.
152                position.z = this-> fieldHeight_ / 2;
153               
154                orxoblox_->LevelUp();
155
156
157                //this->setSpeed(0); // doesn't work here, why??;
158                //Stopping ball
159                orxout() << "Ball stopped" << endl;
160                velocity.x = 0;
161                velocity.y = 0;
162                velocity.z = 0; 
163
164                //ChatManager::message("Waiting for your input");
165                //Input new speed here:
166                //ChatManager::message("Setting new speed");
167
168                //Set new speed here!!
169                velocity.x = rnd(-100,100);
170                velocity.z = rnd(-10,-100);
171               
172               
173
174            }
175            if (position.z < -this->fieldHeight_ / 2){
176                position.z = -this->fieldHeight_ / 2;
177               
178            }
179
180            this->fireEvent();
181        }
182       
183        //Ball hits the right or left wall and should bounce back.
184        // If the ball has crossed the left or right boundary of the playing field.
185        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
186        {
187            //Ball hits the right Wall
188            if (position.x > this->fieldWidth_ / 2)
189                {
190                    // Set the ball to be exactly at the boundary.
191                    position.x = this->fieldWidth_ / 2;
192                    // Invert its velocity in x-direction (i.e. it bounces off).
193                    velocity.x = -velocity.x;
194                    this->fireEvent();
195                    }
196
197            //Ball hits the left wall
198            else if (position.x < -this->fieldWidth_ / 2)
199                {
200                        // Set the ball to be exactly at the boundary.
201                        position.x = -this->fieldWidth_ / 2;
202                        // Invert its velocity in x-direction (i.e. it bounces off).
203                        velocity.x = -velocity.x;
204                        this->fireEvent();
205                    }
206        }
207
208        // Set the position, velocity and acceleration of the ball, if they have changed.
209        if (acceleration != this->getAcceleration())
210            this->setAcceleration(acceleration);
211        if (velocity != this->getVelocity())
212            this->setVelocity(velocity);
213        if (position != this->getPosition())
214            this->setPosition(position);
215        this->Collides((this->orxoblox_->CheckForCollision(this)));
216
217 
218    }
219
220    /**
221    @brief
222        Set the speed of the ball (in x-direction).
223    @param speed
224        The speed to be set.
225    */
226    void OrxoBloxBall::setSpeed(float speed)
227    {   
228
229        if (speed != this->speed_) // If the speed changes
230        {
231            this->speed_ = speed;
232            // Set the speed in the direction of the balls current velocity.
233            Vector3 velocity = this->getVelocity();
234            if (velocity.x != 0)
235                velocity.x = speed;
236                //velocity.x = sgn(velocity.x) * speed;
237            else // If the balls current velocity is zero, the speed is set in a random direction.
238                velocity.x = speed * sgn(rnd(-1,1));
239            //velocity.y = this->speed_;
240            velocity.z = speed;
241
242            this->setVelocity(velocity);
243        }
244    }
245
246
247    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
248    {
249        if( defScoreSound_ )
250            defScoreSound_->setSource(OrxoBloxSound);
251        else
252            assert(0); // This should never happen, because soundpointer is only available on master
253    }
254
255    const std::string& OrxoBloxBall::getDefScoreSound()
256    {
257        if( defScoreSound_ )
258            return defScoreSound_->getSource();
259        else
260            assert(0);
261        return BLANKSTRING;
262    }
263
264    void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound)
265    {
266        if( defBatSound_ )
267            defBatSound_->setSource(OrxoBloxSound);
268        else
269            assert(0); // This should never happen, because soundpointer is only available on master
270    }
271
272    const std::string& OrxoBloxBall::getDefBatSound()
273    {
274        if( defBatSound_ )
275            return defBatSound_->getSource();
276        else
277            assert(0);
278        return BLANKSTRING;
279    }
280
281    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
282    {
283        if( defBoundarySound_ )
284            defBoundarySound_->setSource(OrxoBloxSound);
285        else
286            assert(0); // This should never happen, because soundpointer is only available on master
287    }
288
289    const std::string& OrxoBloxBall::getDefBoundarySound()
290    {
291        if( defBoundarySound_ )
292            return defBoundarySound_->getSource();
293        else
294            assert(0);
295        return BLANKSTRING;
296    }
297
298
299    void OrxoBloxBall::Bounce(OrxoBloxStones* Stone) {
300
301        Vector3 velocity = this->getVelocity();
302        Vector3 positionStone = Stone->getPosition();
303        Vector3 myPosition = this->getPosition();
304        orxout() << "About to Bounce >D" << endl;
305       
306            int distance_X = myPosition.x - positionStone.x;
307            int distance_Z = myPosition.z - positionStone.z;
308
309            if (distance_X < 0)
310                distance_X = -distance_X;
311   
312
313            if (distance_Z < 0)
314                distance_Z = -distance_Z;
315
316            orxout() << distance_X << endl;
317            orxout() << distance_Z << endl;
318
319            if (distance_X < distance_Z) {
320                velocity.z = -velocity.z;
321                orxout() << "z" << endl; 
322            }
323            else if (distance_Z < distance_X) {
324                velocity.x = -velocity.x;
325                orxout() << "x" << endl;       
326            }
327            else {
328                velocity.x = -velocity.x;
329                velocity.z = -velocity.z;
330                orxout() << "both" << endl;
331            }                                 
332
333            this->setVelocity(velocity);
334            this->setPosition(myPosition);
335    }
336
337
338    void OrxoBloxBall::Collides(OrxoBloxStones* Stone)
339    {
340
341        if(Stone == nullptr)
342            return;
343
344        orxout() << "About to Bounce >D" << endl;
345        Bounce(Stone);
346        //if(otherObject->getHealth() <= 0) {
347        Stone->destroy();
348
349        //}
350        //otherObject->reduceHealth();
351    }
352
353    OrxoBlox* OrxoBloxBall::getOrxoBlox()
354    {
355        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
356        {
357            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
358            return orxobloxGametype;
359        }
360        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
361        return nullptr;
362    }
363
364    float OrxoBloxBall::getRadius() {
365        return this->radius_;
366    }
367
368
369}
Note: See TracBrowser for help on using the repository browser.