Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12373 was 12368, checked in by pomselj, 6 years ago

We can shoot

File size: 11.5 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)
130    {
131        SUPER(OrxoBloxBall, tick, dt);
132
133        // Get the current position, velocity and acceleration of the ball.
134        Vector3 position = this->getPosition();
135        Vector3 velocity = this->getVelocity();
136        Vector3 acceleration = this->getAcceleration();
137
[12212]138        // 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]139        if (position.z > this->fieldHeight_ / 2 || position.z - radius_ < -this->fieldHeight_ / 2)
[12210]140        {
141            defBoundarySound_->play(); //play boundary sound
[12212]142            // Its velocity in z-direction is inverted (i.e. it bounces off).
143            velocity.z = -velocity.z;
[12368]144            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
[12336]145            if (position.z > this->fieldHeight_ / 2){
[12339]146                // Set the ball to be exactly at the boundary.
[12368]147                position.z = this-> fieldHeight_ / 2 - radius_;
[12349]148               
149                orxoblox_->LevelUp();
[12344]150
[12349]151
[12344]152                //this->setSpeed(0); // doesn't work here, why??;
153                //Stopping ball
154                orxout() << "Ball stopped" << endl;
155                velocity.x = 0;
156                velocity.y = 0;
157                velocity.z = 0; 
158
[12361]159                //ChatManager::message("Waiting for your input");
[12344]160                //Input new speed here:
[12361]161                //ChatManager::message("Setting new speed");
[12364]162               
163                //%%%%%%%%%%%%
164                //MAUSPOSITION
165                //%%%%%%%%%%%%
166                //Reads current mouse position
167                //TODO: read Mouse position on click!
168                //int mousex = InputManager::getInstance().getMousePosition().first;
169                //int mousey = InputManager::getInstance().getMousePosition().second;
170                //ChatManager::message("Read mouse position");
171                //orxout() << "Mouseposition" << endl;
172                //orxout() << mousex << endl;
173                //ChatManager::message(mousex);
174                //ChatManager::message("mousey");
175                //ChatManager::message(mousey);
[12344]176                //Set new speed here!!
177                velocity.x = rnd(-100,100);
[12364]178                velocity.z = rnd(-50,-100);
[12344]179               
180
[12336]181            }
[12368]182            if (position.z - radius_ < -this->fieldHeight_ / 2){
183                position.z = -this->fieldHeight_ / 2 + radius_;
[12336]184               
185            }
[12210]186
187            this->fireEvent();
188        }
[12294]189       
190        //Ball hits the right or left wall and should bounce back.
191        // If the ball has crossed the left or right boundary of the playing field.
[12368]192        if (position.x + radius_ > this->fieldWidth_ / 2 || position.x - radius_ < -this->fieldWidth_ / 2)
[12210]193        {
[12294]194            //Ball hits the right Wall
[12368]195            if (position.x + radius_ > this->fieldWidth_ / 2)
[12212]196                {
[12294]197                    // Set the ball to be exactly at the boundary.
[12368]198                    position.x = this->fieldWidth_ / 2 - radius_;
[12294]199                    // Invert its velocity in x-direction (i.e. it bounces off).
200                    velocity.x = -velocity.x;
201                    this->fireEvent();
[12212]202                    }
[12278]203
[12294]204            //Ball hits the left wall
[12368]205            else if (position.x - radius_ < -this->fieldWidth_ / 2)
[12212]206                {
207                        // Set the ball to be exactly at the boundary.
[12368]208                        position.x = -this->fieldWidth_ / 2 + radius_;
[12212]209                        // Invert its velocity in x-direction (i.e. it bounces off).
210                        velocity.x = -velocity.x;
211                        this->fireEvent();
212                    }
[12210]213        }
214
215        // Set the position, velocity and acceleration of the ball, if they have changed.
216        if (acceleration != this->getAcceleration())
217            this->setAcceleration(acceleration);
218        if (velocity != this->getVelocity())
219            this->setVelocity(velocity);
220        if (position != this->getPosition())
221            this->setPosition(position);
[12368]222        //this->Collides((this->orxoblox_->CheckForCollision(this)));
[12346]223
224 
[12210]225    }
226
227    /**
228    @brief
229        Set the speed of the ball (in x-direction).
230    @param speed
231        The speed to be set.
232    */
233    void OrxoBloxBall::setSpeed(float speed)
[12344]234    {   
235
[12210]236        if (speed != this->speed_) // If the speed changes
237        {
238            this->speed_ = speed;
239            // Set the speed in the direction of the balls current velocity.
240            Vector3 velocity = this->getVelocity();
[12344]241            if (velocity.x != 0)
242                velocity.x = speed;
243                //velocity.x = sgn(velocity.x) * speed;
244            else // If the balls current velocity is zero, the speed is set in a random direction.
245                velocity.x = speed * sgn(rnd(-1,1));
[12305]246            //velocity.y = this->speed_;
[12344]247            velocity.z = speed;
[12210]248
249            this->setVelocity(velocity);
250        }
251    }
252
253
[12212]254    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
[12210]255    {
256        if( defScoreSound_ )
[12212]257            defScoreSound_->setSource(OrxoBloxSound);
[12210]258        else
259            assert(0); // This should never happen, because soundpointer is only available on master
260    }
261
262    const std::string& OrxoBloxBall::getDefScoreSound()
263    {
264        if( defScoreSound_ )
265            return defScoreSound_->getSource();
266        else
267            assert(0);
268        return BLANKSTRING;
269    }
270
[12366]271   
272   
[12212]273    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
[12210]274    {
275        if( defBoundarySound_ )
[12212]276            defBoundarySound_->setSource(OrxoBloxSound);
[12210]277        else
278            assert(0); // This should never happen, because soundpointer is only available on master
279    }
280
281    const std::string& OrxoBloxBall::getDefBoundarySound()
282    {
283        if( defBoundarySound_ )
284            return defBoundarySound_->getSource();
285        else
286            assert(0);
287        return BLANKSTRING;
288    }
[12310]289
290
[12368]291    void OrxoBloxBall::Bounce(WorldEntity* Stone) {
[12310]292
293        Vector3 velocity = this->getVelocity();
[12356]294        Vector3 positionStone = Stone->getPosition();
[12346]295        Vector3 myPosition = this->getPosition();
[12310]296        orxout() << "About to Bounce >D" << endl;
297       
[12356]298            int distance_X = myPosition.x - positionStone.x;
299            int distance_Z = myPosition.z - positionStone.z;
[12310]300
301            if (distance_X < 0)
302                distance_X = -distance_X;
303   
304
305            if (distance_Z < 0)
306                distance_Z = -distance_Z;
307
308            orxout() << distance_X << endl;
309            orxout() << distance_Z << endl;
310
311            if (distance_X < distance_Z) {
312                velocity.z = -velocity.z;
[12360]313                orxout() << "z" << endl; 
[12310]314            }
[12360]315            else if (distance_Z < distance_X) {
[12310]316                velocity.x = -velocity.x;
[12360]317                orxout() << "x" << endl;       
[12310]318            }
319            else {
320                velocity.x = -velocity.x;
321                velocity.z = -velocity.z;
322                orxout() << "both" << endl;
[12360]323            }                                 
324
[12310]325            this->setVelocity(velocity);
[12360]326            this->setPosition(myPosition);
[12310]327    }
328
329
[12356]330    void OrxoBloxBall::Collides(OrxoBloxStones* Stone)
[12310]331    {
[12346]332
[12356]333        if(Stone == nullptr)
[12346]334            return;
335
[12310]336        orxout() << "About to Bounce >D" << endl;
[12356]337        Bounce(Stone);
338        //if(otherObject->getHealth() <= 0) {
[12368]339        //Stone->destroy();
[12360]340
[12356]341        //}
342        //otherObject->reduceHealth();
[12310]343    }
344
[12368]345    bool OrxoBloxBall::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint)
346    {
347        orxout() << "detected collision" << endl;
348        Bounce(otherObject);
349
350        return true;
351    }
352
[12346]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    }
[12310]363
[12360]364    float OrxoBloxBall::getRadius() {
365        return this->radius_;
366    }
[12346]367
[12360]368
[12210]369}
Note: See TracBrowser for help on using the repository browser.