Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12364 was 12364, checked in by jeromela, 5 years ago

orxoblox.oxw: Auskommentiert, orxobloxball.cc: mauskoordinaten auslesen.

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