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
RevLine 
[12210]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"
[12356]35#include "OrxoBloxStones.h"
[12347]36#include "OrxoBlox.h"
[12310]37#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
[12210]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"
[12364]47#include "core/input/InputManager.h"
[12210]48
[12347]49
[12210]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)
[12346]61        : Pawn(context)
[12210]62    {
63        RegisterObject(OrxoBloxBall);
64
65        this->speed_ = 0;
66        this->accelerationFactor_ = 1.0f;
[12366]67       
[12212]68        this->relMercyOffset_ = 0.05f;
[12346]69        this->orxoblox_ = this->getOrxoBlox();
[12368]70        this->radius_ = 3;
[12210]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);
[12366]79                 
[12210]80                 this->defBoundarySound_ = new WorldSound(this->getContext());
81                 this->defBoundarySound_->setVolume(0.5f);
82             }
83             else
84             {
85                 this->defScoreSound_ = nullptr;
[12366]86                 
[12210]87                 this->defBoundarySound_ = nullptr;
88             }
89    }
90
91    /**
92    @brief
93        Destructor.
94    */
95    OrxoBloxBall::~OrxoBloxBall()
96    {
[12366]97       
[12210]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);
[12366]105       
[12210]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_ );
[12366]117       
[12210]118        registerVariable( this->speed_ );
[12212]119        registerVariable( this->relMercyOffset_ );
[12210]120    }
121
122    /**
123    @brief
124        Is called every tick.
[12212]125        Handles the movement of the ball and its interaction with the boundaries and bats.
[12210]126    @param dt
127        The time since the last tick.
128    */
129    void OrxoBloxBall::tick(float dt)
[12376]130    {   
131       
[12210]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
[12212]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).
[12368]140        if (position.z > this->fieldHeight_ / 2 || position.z - radius_ < -this->fieldHeight_ / 2)
[12210]141        {
142            defBoundarySound_->play(); //play boundary sound
[12212]143            // Its velocity in z-direction is inverted (i.e. it bounces off).
144            velocity.z = -velocity.z;
[12368]145            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
[12336]146            if (position.z > this->fieldHeight_ / 2){
[12339]147                // Set the ball to be exactly at the boundary.
[12368]148                position.z = this-> fieldHeight_ / 2 - radius_;
[12349]149               
150                orxoblox_->LevelUp();
[12376]151                orxoblox_->LevelUp();
[12379]152                //orxout() << "LevelUp 2 finished, trying to call end() function";
[12376]153                //this->end();
[12344]154
[12349]155
[12376]156
[12344]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
[12361]164                //ChatManager::message("Waiting for your input");
[12344]165                //Input new speed here:
[12361]166                //ChatManager::message("Setting new speed");
[12364]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);
[12344]181                //Set new speed here!!
182                velocity.x = rnd(-100,100);
[12364]183                velocity.z = rnd(-50,-100);
[12344]184               
185
[12336]186            }
[12368]187            if (position.z - radius_ < -this->fieldHeight_ / 2){
188                position.z = -this->fieldHeight_ / 2 + radius_;
[12336]189               
190            }
[12210]191
192            this->fireEvent();
193        }
[12294]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.
[12368]197        if (position.x + radius_ > this->fieldWidth_ / 2 || position.x - radius_ < -this->fieldWidth_ / 2)
[12210]198        {
[12294]199            //Ball hits the right Wall
[12368]200            if (position.x + radius_ > this->fieldWidth_ / 2)
[12212]201                {
[12294]202                    // Set the ball to be exactly at the boundary.
[12368]203                    position.x = this->fieldWidth_ / 2 - radius_;
[12294]204                    // Invert its velocity in x-direction (i.e. it bounces off).
205                    velocity.x = -velocity.x;
206                    this->fireEvent();
[12212]207                    }
[12278]208
[12294]209            //Ball hits the left wall
[12368]210            else if (position.x - radius_ < -this->fieldWidth_ / 2)
[12212]211                {
212                        // Set the ball to be exactly at the boundary.
[12368]213                        position.x = -this->fieldWidth_ / 2 + radius_;
[12212]214                        // Invert its velocity in x-direction (i.e. it bounces off).
215                        velocity.x = -velocity.x;
216                        this->fireEvent();
217                    }
[12210]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);
[12368]227        //this->Collides((this->orxoblox_->CheckForCollision(this)));
[12346]228
229 
[12210]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)
[12344]239    {   
240
[12210]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();
[12344]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));
[12305]251            //velocity.y = this->speed_;
[12344]252            velocity.z = speed;
[12210]253
254            this->setVelocity(velocity);
255        }
256    }
257
258
[12212]259    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
[12210]260    {
261        if( defScoreSound_ )
[12212]262            defScoreSound_->setSource(OrxoBloxSound);
[12210]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
[12366]276   
277   
[12212]278    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
[12210]279    {
280        if( defBoundarySound_ )
[12212]281            defBoundarySound_->setSource(OrxoBloxSound);
[12210]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    }
[12310]294
295
[12368]296    void OrxoBloxBall::Bounce(WorldEntity* Stone) {
[12310]297
298        Vector3 velocity = this->getVelocity();
[12356]299        Vector3 positionStone = Stone->getPosition();
[12346]300        Vector3 myPosition = this->getPosition();
[12376]301        //orxout() << "About to Bounce >D" << endl;
[12310]302       
[12356]303            int distance_X = myPosition.x - positionStone.x;
304            int distance_Z = myPosition.z - positionStone.z;
[12310]305
306            if (distance_X < 0)
307                distance_X = -distance_X;
308   
309
310            if (distance_Z < 0)
311                distance_Z = -distance_Z;
312
[12376]313            //orxout() << distance_X << endl;
314            //orxout() << distance_Z << endl;
[12310]315
316            if (distance_X < distance_Z) {
317                velocity.z = -velocity.z;
[12376]318                //orxout() << "z" << endl; 
[12310]319            }
[12360]320            else if (distance_Z < distance_X) {
[12310]321                velocity.x = -velocity.x;
[12376]322                //orxout() << "x" << endl;       
[12310]323            }
324            else {
325                velocity.x = -velocity.x;
326                velocity.z = -velocity.z;
[12376]327                //orxout() << "both" << endl;
[12360]328            }                                 
329
[12310]330            this->setVelocity(velocity);
[12360]331            this->setPosition(myPosition);
[12310]332    }
333
334
[12356]335    void OrxoBloxBall::Collides(OrxoBloxStones* Stone)
[12310]336    {
[12346]337
[12356]338        if(Stone == nullptr)
[12346]339            return;
340
[12310]341        orxout() << "About to Bounce >D" << endl;
[12356]342        Bounce(Stone);
343        //if(otherObject->getHealth() <= 0) {
[12368]344        //Stone->destroy();
[12360]345
[12356]346        //}
347        //otherObject->reduceHealth();
[12310]348    }
349
[12368]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
[12346]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    }
[12310]368
[12360]369    float OrxoBloxBall::getRadius() {
370        return this->radius_;
371    }
[12346]372
[12360]373
[12210]374}
Note: See TracBrowser for help on using the repository browser.