Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleanup active Walls

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