Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Worked on Collision, doesn't work yet, compiles tho

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