Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12394 was 12379, checked in by jeromela, 6 years ago

new commit

File size: 11.7 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#include "core/input/InputManager.h"
48
49
50namespace orxonox
51{
52    RegisterClass(OrxoBloxBall);
53
54    const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5;
55
56    /**
57    @brief
58        Constructor. Registers and initializes the object.
59    */
60    OrxoBloxBall::OrxoBloxBall(Context* context)
61        : Pawn(context)
62    {
63        RegisterObject(OrxoBloxBall);
64
65        this->speed_ = 0;
66        this->accelerationFactor_ = 1.0f;
67       
68        this->relMercyOffset_ = 0.05f;
69        this->orxoblox_ = this->getOrxoBlox();
70        this->radius_ = 3;
71
72        this->registerVariables();
73
74        //initialize sound
75        if (GameMode::isMaster())
76             {
77                 this->defScoreSound_ = new WorldSound(this->getContext());
78                 this->defScoreSound_->setVolume(1.0f);
79                 
80                 this->defBoundarySound_ = new WorldSound(this->getContext());
81                 this->defBoundarySound_->setVolume(0.5f);
82             }
83             else
84             {
85                 this->defScoreSound_ = nullptr;
86                 
87                 this->defBoundarySound_ = nullptr;
88             }
89    }
90
91    /**
92    @brief
93        Destructor.
94    */
95    OrxoBloxBall::~OrxoBloxBall()
96    {
97       
98    }
99
100    //xml port for loading sounds
101    void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
102    {
103        SUPER(OrxoBloxBall, XMLPort, xmlelement, mode);
104        XMLPortParam(OrxoBloxBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
105       
106        XMLPortParam(OrxoBloxBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
107    }
108
109    /**
110    @brief
111        Register variables to synchronize over the network.
112    */
113    void OrxoBloxBall::registerVariables()
114    {
115        registerVariable( this->fieldWidth_ );
116        registerVariable( this->fieldHeight_ );
117       
118        registerVariable( this->speed_ );
119        registerVariable( this->relMercyOffset_ );
120    }
121
122    /**
123    @brief
124        Is called every tick.
125        Handles the movement of the ball and its interaction with the boundaries and bats.
126    @param dt
127        The time since the last tick.
128    */
129    void OrxoBloxBall::tick(float dt)
130    {   
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 - radius_ < -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. Remember z axis is reverted!!!
146            if (position.z > this->fieldHeight_ / 2){
147                // Set the ball to be exactly at the boundary.
148                position.z = this-> fieldHeight_ / 2 - radius_;
149               
150                orxoblox_->LevelUp();
151                orxoblox_->LevelUp();
152                //orxout() << "LevelUp 2 finished, trying to call end() function";
153                //this->end();
154
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                //%%%%%%%%%%%%
169                //MAUSPOSITION
170                //%%%%%%%%%%%%
171                //Reads current mouse position
172                //TODO: read Mouse position on click!
173                //int mousex = InputManager::getInstance().getMousePosition().first;
174                //int mousey = InputManager::getInstance().getMousePosition().second;
175                //ChatManager::message("Read mouse position");
176                //orxout() << "Mouseposition" << endl;
177                //orxout() << mousex << endl;
178                //ChatManager::message(mousex);
179                //ChatManager::message("mousey");
180                //ChatManager::message(mousey);
181                //Set new speed here!!
182                velocity.x = rnd(-100,100);
183                velocity.z = rnd(-50,-100);
184               
185
186            }
187            if (position.z - radius_ < -this->fieldHeight_ / 2){
188                position.z = -this->fieldHeight_ / 2 + radius_;
189               
190            }
191
192            this->fireEvent();
193        }
194       
195        //Ball hits the right or left wall and should bounce back.
196        // If the ball has crossed the left or right boundary of the playing field.
197        if (position.x + radius_ > this->fieldWidth_ / 2 || position.x - radius_ < -this->fieldWidth_ / 2)
198        {
199            //Ball hits the right Wall
200            if (position.x + radius_ > this->fieldWidth_ / 2)
201                {
202                    // Set the ball to be exactly at the boundary.
203                    position.x = this->fieldWidth_ / 2 - radius_;
204                    // Invert its velocity in x-direction (i.e. it bounces off).
205                    velocity.x = -velocity.x;
206                    this->fireEvent();
207                    }
208
209            //Ball hits the left wall
210            else if (position.x - radius_ < -this->fieldWidth_ / 2)
211                {
212                        // Set the ball to be exactly at the boundary.
213                        position.x = -this->fieldWidth_ / 2 + radius_;
214                        // Invert its velocity in x-direction (i.e. it bounces off).
215                        velocity.x = -velocity.x;
216                        this->fireEvent();
217                    }
218        }
219
220        // Set the position, velocity and acceleration of the ball, if they have changed.
221        if (acceleration != this->getAcceleration())
222            this->setAcceleration(acceleration);
223        if (velocity != this->getVelocity())
224            this->setVelocity(velocity);
225        if (position != this->getPosition())
226            this->setPosition(position);
227        //this->Collides((this->orxoblox_->CheckForCollision(this)));
228
229 
230    }
231
232    /**
233    @brief
234        Set the speed of the ball (in x-direction).
235    @param speed
236        The speed to be set.
237    */
238    void OrxoBloxBall::setSpeed(float speed)
239    {   
240
241        if (speed != this->speed_) // If the speed changes
242        {
243            this->speed_ = speed;
244            // Set the speed in the direction of the balls current velocity.
245            Vector3 velocity = this->getVelocity();
246            if (velocity.x != 0)
247                velocity.x = speed;
248                //velocity.x = sgn(velocity.x) * speed;
249            else // If the balls current velocity is zero, the speed is set in a random direction.
250                velocity.x = speed * sgn(rnd(-1,1));
251            //velocity.y = this->speed_;
252            velocity.z = speed;
253
254            this->setVelocity(velocity);
255        }
256    }
257
258
259    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
260    {
261        if( defScoreSound_ )
262            defScoreSound_->setSource(OrxoBloxSound);
263        else
264            assert(0); // This should never happen, because soundpointer is only available on master
265    }
266
267    const std::string& OrxoBloxBall::getDefScoreSound()
268    {
269        if( defScoreSound_ )
270            return defScoreSound_->getSource();
271        else
272            assert(0);
273        return BLANKSTRING;
274    }
275
276   
277   
278    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
279    {
280        if( defBoundarySound_ )
281            defBoundarySound_->setSource(OrxoBloxSound);
282        else
283            assert(0); // This should never happen, because soundpointer is only available on master
284    }
285
286    const std::string& OrxoBloxBall::getDefBoundarySound()
287    {
288        if( defBoundarySound_ )
289            return defBoundarySound_->getSource();
290        else
291            assert(0);
292        return BLANKSTRING;
293    }
294
295
296    void OrxoBloxBall::Bounce(WorldEntity* Stone) {
297
298        Vector3 velocity = this->getVelocity();
299        Vector3 positionStone = Stone->getPosition();
300        Vector3 myPosition = this->getPosition();
301        //orxout() << "About to Bounce >D" << endl;
302       
303            int distance_X = myPosition.x - positionStone.x;
304            int distance_Z = myPosition.z - positionStone.z;
305
306            if (distance_X < 0)
307                distance_X = -distance_X;
308   
309
310            if (distance_Z < 0)
311                distance_Z = -distance_Z;
312
313            //orxout() << distance_X << endl;
314            //orxout() << distance_Z << endl;
315
316            if (distance_X < distance_Z) {
317                velocity.z = -velocity.z;
318                //orxout() << "z" << endl; 
319            }
320            else if (distance_Z < distance_X) {
321                velocity.x = -velocity.x;
322                //orxout() << "x" << endl;       
323            }
324            else {
325                velocity.x = -velocity.x;
326                velocity.z = -velocity.z;
327                //orxout() << "both" << endl;
328            }                                 
329
330            this->setVelocity(velocity);
331            this->setPosition(myPosition);
332    }
333
334
335    void OrxoBloxBall::Collides(OrxoBloxStones* Stone)
336    {
337
338        if(Stone == nullptr)
339            return;
340
341        orxout() << "About to Bounce >D" << endl;
342        Bounce(Stone);
343        //if(otherObject->getHealth() <= 0) {
344        //Stone->destroy();
345
346        //}
347        //otherObject->reduceHealth();
348    }
349
350    bool OrxoBloxBall::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint)
351    {
352        orxout() << "detected collision" << endl;
353        Bounce(otherObject);
354
355        return true;
356    }
357
358    OrxoBlox* OrxoBloxBall::getOrxoBlox()
359    {
360        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
361        {
362            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
363            return orxobloxGametype;
364        }
365        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
366        return nullptr;
367    }
368
369    float OrxoBloxBall::getRadius() {
370        return this->radius_;
371    }
372
373
374}
Note: See TracBrowser for help on using the repository browser.