Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

LevelUp!

File size: 11.1 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                //this->setSpeed(0); // doesn't work here, why??;
154                //Stopping ball
155                orxout() << "Ball stopped" << endl;
156                velocity.x = 0;
157                velocity.y = 0;
158                velocity.z = 0; 
159
160                ChatManager::message("Waiting for your input");
161                //Input new speed here:
162                ChatManager::message("Setting new speed");
163
164                //Set new speed here!!
165                velocity.x = rnd(-100,100);
166                velocity.z = rnd(-10,-100);
167               
168               
169
170            }
171            if (position.z < -this->fieldHeight_ / 2){
172                position.z = -this->fieldHeight_ / 2;
173               
174            }
175
176            this->fireEvent();
177        }
178       
179        //Ball hits the right or left wall and should bounce back.
180        // If the ball has crossed the left or right boundary of the playing field.
181        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
182        {
183            //Ball hits the right Wall
184            if (position.x > this->fieldWidth_ / 2)
185                {
186                    // Set the ball to be exactly at the boundary.
187                    position.x = this->fieldWidth_ / 2;
188                    // Invert its velocity in x-direction (i.e. it bounces off).
189                    velocity.x = -velocity.x;
190                    this->fireEvent();
191                    }
192
193            //Ball hits the left wall
194            else if (position.x < -this->fieldWidth_ / 2)
195                {
196                        // Set the ball to be exactly at the boundary.
197                        position.x = -this->fieldWidth_ / 2;
198                        // Invert its velocity in x-direction (i.e. it bounces off).
199                        velocity.x = -velocity.x;
200                        this->fireEvent();
201                    }
202        }
203
204        // Set the position, velocity and acceleration of the ball, if they have changed.
205        if (acceleration != this->getAcceleration())
206            this->setAcceleration(acceleration);
207        if (velocity != this->getVelocity())
208            this->setVelocity(velocity);
209        if (position != this->getPosition())
210            this->setPosition(position);
211        this->Collides((this->orxoblox_->CheckForCollision(this)));
212
213 
214    }
215
216    /**
217    @brief
218        Set the speed of the ball (in x-direction).
219    @param speed
220        The speed to be set.
221    */
222    void OrxoBloxBall::setSpeed(float speed)
223    {   
224
225        if (speed != this->speed_) // If the speed changes
226        {
227            this->speed_ = speed;
228            // Set the speed in the direction of the balls current velocity.
229            Vector3 velocity = this->getVelocity();
230            if (velocity.x != 0)
231                velocity.x = speed;
232                //velocity.x = sgn(velocity.x) * speed;
233            else // If the balls current velocity is zero, the speed is set in a random direction.
234                velocity.x = speed * sgn(rnd(-1,1));
235            //velocity.y = this->speed_;
236            velocity.z = speed;
237
238            this->setVelocity(velocity);
239        }
240    }
241
242
243    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
244    {
245        if( defScoreSound_ )
246            defScoreSound_->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::getDefScoreSound()
252    {
253        if( defScoreSound_ )
254            return defScoreSound_->getSource();
255        else
256            assert(0);
257        return BLANKSTRING;
258    }
259
260    void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound)
261    {
262        if( defBatSound_ )
263            defBatSound_->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::getDefBatSound()
269    {
270        if( defBatSound_ )
271            return defBatSound_->getSource();
272        else
273            assert(0);
274        return BLANKSTRING;
275    }
276
277    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
278    {
279        if( defBoundarySound_ )
280            defBoundarySound_->setSource(OrxoBloxSound);
281        else
282            assert(0); // This should never happen, because soundpointer is only available on master
283    }
284
285    const std::string& OrxoBloxBall::getDefBoundarySound()
286    {
287        if( defBoundarySound_ )
288            return defBoundarySound_->getSource();
289        else
290            assert(0);
291        return BLANKSTRING;
292    }
293
294
295    void OrxoBloxBall::Bounce(OrxoBloxStones* otherObject) {
296
297        Vector3 velocity = this->getVelocity();
298        Vector3 positionOtherObject = otherObject->getPosition();
299        Vector3 myPosition = this->getPosition();
300        orxout() << "About to Bounce >D" << endl;
301        //if (positionOtherObject.y < 0) {
302            //this.destroy()
303        //}S
304        //else {
305       
306            int distance_X = myPosition.x - positionOtherObject.x;
307            int distance_Z = myPosition.z - positionOtherObject.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            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            this->setVelocity(velocity);
333    }
334
335
336    void OrxoBloxBall::Collides(OrxoBloxStones* otherObject)
337    {
338
339        if(otherObject == nullptr)
340            return;
341
342        orxout() << "About to Bounce >D" << endl;
343        Bounce(otherObject);
344    }
345
346    OrxoBlox* OrxoBloxBall::getOrxoBlox()
347    {
348        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
349        {
350            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
351            return orxobloxGametype;
352        }
353        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
354        return nullptr;
355    }
356
357
358}
Note: See TracBrowser for help on using the repository browser.