Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Bounces ok-ish, still seg fault… stones get destroyed but models stay, maybe destroy wals too

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
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
100            delete[] this->batID_;
101        }
102    }
103
104    //xml port for loading sounds
105    void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
106    {
107        SUPER(OrxoBloxBall, XMLPort, xmlelement, mode);
108        XMLPortParam(OrxoBloxBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
109        XMLPortParam(OrxoBloxBall, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
110        XMLPortParam(OrxoBloxBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
111    }
112
113    /**
114    @brief
115        Register variables to synchronize over the network.
116    */
117    void OrxoBloxBall::registerVariables()
118    {
119        registerVariable( this->fieldWidth_ );
120        registerVariable( this->fieldHeight_ );
121        registerVariable( this->batlength_ );
122        registerVariable( this->speed_ );
123        registerVariable( this->relMercyOffset_ );
124    }
125
126    /**
127    @brief
128        Is called every tick.
129        Handles the movement of the ball and its interaction with the boundaries and bats.
130    @param dt
131        The time since the last tick.
132    */
133    void OrxoBloxBall::tick(float dt)
134    {
135        SUPER(OrxoBloxBall, tick, dt);
136
137        // Get the current position, velocity and acceleration of the ball.
138        Vector3 position = this->getPosition();
139        Vector3 velocity = this->getVelocity();
140        Vector3 acceleration = this->getAcceleration();
141
142        // 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).
143        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
144        {
145            defBoundarySound_->play(); //play boundary sound
146            // Its velocity in z-direction is inverted (i.e. it bounces off).
147            velocity.z = -velocity.z;
148            // And its position is set as to not overstep the boundary it has just crossed.
149            if (position.z > this->fieldHeight_ / 2){
150                // Set the ball to be exactly at the boundary.
151                position.z = this-> fieldHeight_ / 2;
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* Stone) {
299
300        Vector3 velocity = this->getVelocity();
301        Vector3 positionStone = Stone->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 - positionStone.x;
310            int distance_Z = myPosition.z - positionStone.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* Stone)
340    {
341
342        if(Stone == nullptr)
343            return;
344
345        orxout() << "About to Bounce >D" << endl;
346        Bounce(Stone);
347        //if(otherObject->getHealth() <= 0) {
348            Stone->destroy();
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
365}
Note: See TracBrowser for help on using the repository browser.