Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

spielfeldgroesse

File size: 10.6 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        : MovableEntity(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
66        this->registerVariables();
67
68        //initialize sound
69        if (GameMode::isMaster())
70             {
71                 this->defScoreSound_ = new WorldSound(this->getContext());
72                 this->defScoreSound_->setVolume(1.0f);
73                 this->defBatSound_ = new WorldSound(this->getContext());
74                 this->defBatSound_->setVolume(0.4f);
75                 this->defBoundarySound_ = new WorldSound(this->getContext());
76                 this->defBoundarySound_->setVolume(0.5f);
77             }
78             else
79             {
80                 this->defScoreSound_ = nullptr;
81                 this->defBatSound_ = nullptr;
82                 this->defBoundarySound_ = nullptr;
83             }
84    }
85
86    /**
87    @brief
88        Destructor.
89    */
90    OrxoBloxBall::~OrxoBloxBall()
91    {
92        if (this->isInitialized())
93        {
94            if (this->bDeleteBats_)
95
96            delete[] this->batID_;
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        XMLPortParam(OrxoBloxBall, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
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        registerVariable( this->batlength_ );
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        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
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).
139        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
140        {
141            defBoundarySound_->play(); //play boundary sound
142            // Its velocity in z-direction is inverted (i.e. it bounces off).
143            velocity.z = -velocity.z;
144            // And its position is set as to not overstep the boundary it has just crossed.
145            if (position.z > this->fieldHeight_ / 2){
146                // Set the ball to be exactly at the boundary.
147                position.z = this-> fieldHeight_ / 2;
148                // Set the velocity to zero
149                this->setSpeed(0); // doesn't work here, why??;
150                //velocity.x = 0;
151                //velocity.y = 0;
152                //velocity.z = 0;
153                orxout() << "Output" << endl;
154                ChatManager::message("Waiting");
155            }
156            if (position.z < -this->fieldHeight_ / 2){
157                position.z = -this->fieldHeight_ / 2;
158               
159            }
160
161            this->fireEvent();
162        }
163       
164        //Ball hits the right or left wall and should bounce back.
165        // If the ball has crossed the left or right boundary of the playing field.
166        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
167        {
168            //Ball hits the right Wall
169            if (position.x > this->fieldWidth_ / 2)
170                {
171                    // Set the ball to be exactly at the boundary.
172                    position.x = this->fieldWidth_ / 2;
173                    // Invert its velocity in x-direction (i.e. it bounces off).
174                    velocity.x = -velocity.x;
175                    this->fireEvent();
176                    }
177
178            //Ball hits the left wall
179            else if (position.x < -this->fieldWidth_ / 2)
180                {
181                        // Set the ball to be exactly at the boundary.
182                        position.x = -this->fieldWidth_ / 2;
183                        // Invert its velocity in x-direction (i.e. it bounces off).
184                        velocity.x = -velocity.x;
185                        this->fireEvent();
186                    }
187        }
188
189        // Set the position, velocity and acceleration of the ball, if they have changed.
190        if (acceleration != this->getAcceleration())
191            this->setAcceleration(acceleration);
192        if (velocity != this->getVelocity())
193            this->setVelocity(velocity);
194        if (position != this->getPosition())
195            this->setPosition(position);
196    }
197
198    /**
199    @brief
200        Set the speed of the ball (in x-direction).
201    @param speed
202        The speed to be set.
203    */
204    void OrxoBloxBall::setSpeed(float speed)
205    {
206        if (speed != this->speed_) // If the speed changes
207        {
208            this->speed_ = speed;
209
210            // Set the speed in the direction of the balls current velocity.
211            Vector3 velocity = this->getVelocity();
212            //if (velocity.x != 0)
213                velocity.x = sgn(velocity.x) * this->speed_;
214            //else // If the balls current velocity is zero, the speed is set in a random direction.
215            //    velocity.x = this->speed_ * sgn(rnd(-1,1));
216            //velocity.y = this->speed_;
217            velocity.z = this->speed_;
218
219            this->setVelocity(velocity);
220        }
221    }
222
223
224    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
225    {
226        if( defScoreSound_ )
227            defScoreSound_->setSource(OrxoBloxSound);
228        else
229            assert(0); // This should never happen, because soundpointer is only available on master
230    }
231
232    const std::string& OrxoBloxBall::getDefScoreSound()
233    {
234        if( defScoreSound_ )
235            return defScoreSound_->getSource();
236        else
237            assert(0);
238        return BLANKSTRING;
239    }
240
241    void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound)
242    {
243        if( defBatSound_ )
244            defBatSound_->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::getDefBatSound()
250    {
251        if( defBatSound_ )
252            return defBatSound_->getSource();
253        else
254            assert(0);
255        return BLANKSTRING;
256    }
257
258    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
259    {
260        if( defBoundarySound_ )
261            defBoundarySound_->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::getDefBoundarySound()
267    {
268        if( defBoundarySound_ )
269            return defBoundarySound_->getSource();
270        else
271            assert(0);
272        return BLANKSTRING;
273    }
274
275
276    void OrxoBloxBall::Bounce(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) {
277
278        Vector3 velocity = this->getVelocity();
279        Vector3 myPosition = otherObject->getPosition();
280        btVector3 positionOtherObject = contactPoint.getPositionWorldOnA();
281        orxout() << "About to Bounce >D" << endl;
282        //if (positionOtherObject.y < 0) {
283            //this.destroy()
284        //}S
285        //else {
286       
287            int distance_X = positionOtherObject.getX() - myPosition.x;
288            int distance_Z = positionOtherObject.getZ() - myPosition.z;
289
290            if (distance_X < 0)
291                distance_X = -distance_X;
292   
293
294            if (distance_Z < 0)
295                distance_Z = -distance_Z;
296
297            orxout() << distance_X << endl;
298            orxout() << distance_Z << endl;
299
300            if (distance_X < distance_Z) {
301                velocity.z = -velocity.z;
302                orxout() << "z" << endl;
303            }
304            if (distance_Z < distance_X) {
305                velocity.x = -velocity.x;
306                orxout() << "x" << endl;
307            }
308            else {
309                velocity.x = -velocity.x;
310                velocity.z = -velocity.z;
311                orxout() << "both" << endl;
312            }
313            this->setVelocity(velocity);
314        //}
315    }
316
317
318    bool OrxoBloxBall::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint)
319    {
320        orxout() << "About to Bounce >D" << endl;
321        bool result = MovableEntity::collidesAgainst(otherObject, ownCollisionShape, contactPoint);
322        Bounce(otherObject, ownCollisionShape, contactPoint);
323        return result;
324    }
325
326
327}
Note: See TracBrowser for help on using the repository browser.