Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12308 was 12308, checked in by ahuwyler, 5 years ago

Bats are gone

File size: 8.4 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
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38
39#include "gametypes/Gametype.h"
40
41
42#include "sound/WorldSound.h"
43#include "core/XMLPort.h"
44
45namespace orxonox
46{
47    RegisterClass(OrxoBloxBall);
48
49    const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5;
50
51    /**
52    @brief
53        Constructor. Registers and initializes the object.
54    */
55    OrxoBloxBall::OrxoBloxBall(Context* context)
56        : MovableEntity(context)
57    {
58        RegisterObject(OrxoBloxBall);
59
60        this->speed_ = 0;
61        this->accelerationFactor_ = 1.0f;
62        this->bDeleteBats_ = false;
63        this->relMercyOffset_ = 0.05f;
64
65        this->registerVariables();
66
67        //initialize sound
68        if (GameMode::isMaster())
69             {
70                 this->defScoreSound_ = new WorldSound(this->getContext());
71                 this->defScoreSound_->setVolume(1.0f);
72                 this->defBatSound_ = new WorldSound(this->getContext());
73                 this->defBatSound_->setVolume(0.4f);
74                 this->defBoundarySound_ = new WorldSound(this->getContext());
75                 this->defBoundarySound_->setVolume(0.5f);
76             }
77             else
78             {
79                 this->defScoreSound_ = nullptr;
80                 this->defBatSound_ = nullptr;
81                 this->defBoundarySound_ = nullptr;
82             }
83    }
84
85    /**
86    @brief
87        Destructor.
88    */
89    OrxoBloxBall::~OrxoBloxBall()
90    {
91        if (this->isInitialized())
92        {
93            if (this->bDeleteBats_)
94
95            delete[] this->batID_;
96        }
97    }
98
99    //xml port for loading sounds
100    void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(OrxoBloxBall, XMLPort, xmlelement, mode);
103        XMLPortParam(OrxoBloxBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
104        XMLPortParam(OrxoBloxBall, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
105        XMLPortParam(OrxoBloxBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
106    }
107
108    /**
109    @brief
110        Register variables to synchronize over the network.
111    */
112    void OrxoBloxBall::registerVariables()
113    {
114        registerVariable( this->fieldWidth_ );
115        registerVariable( this->fieldHeight_ );
116        registerVariable( this->batlength_ );
117        registerVariable( this->speed_ );
118        registerVariable( this->relMercyOffset_ );
119    }
120
121    /**
122    @brief
123        Is called every tick.
124        Handles the movement of the ball and its interaction with the boundaries and bats.
125    @param dt
126        The time since the last tick.
127    */
128    void OrxoBloxBall::tick(float dt)
129    {
130        SUPER(OrxoBloxBall, tick, dt);
131
132        // Get the current position, velocity and acceleration of the ball.
133        Vector3 position = this->getPosition();
134        Vector3 velocity = this->getVelocity();
135        Vector3 acceleration = this->getAcceleration();
136
137        // 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).
138        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
139        {
140            defBoundarySound_->play(); //play boundary sound
141            // Its velocity in z-direction is inverted (i.e. it bounces off).
142            velocity.z = -velocity.z;
143            // And its position is set as to not overstep the boundary it has just crossed.
144            if (position.z > this->fieldHeight_ / 2)
145                position.z = this->fieldHeight_ / 2;
146            if (position.z < -this->fieldHeight_ / 2)
147                position.z = -this->fieldHeight_ / 2;
148
149            this->fireEvent();
150        }
151       
152        //Ball hits the right or left wall and should bounce back.
153        // If the ball has crossed the left or right boundary of the playing field.
154        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
155        {
156            //Ball hits the right Wall
157            if (position.x > this->fieldWidth_ / 2)
158                {
159                    // Set the ball to be exactly at the boundary.
160                    position.x = this->fieldWidth_ / 2;
161                    // Invert its velocity in x-direction (i.e. it bounces off).
162                    velocity.x = -velocity.x;
163                    this->fireEvent();
164                    }
165
166            //Ball hits the left wall
167            else if (position.x < -this->fieldWidth_ / 2)
168                {
169                        // Set the ball to be exactly at the boundary.
170                        position.x = -this->fieldWidth_ / 2;
171                        // Invert its velocity in x-direction (i.e. it bounces off).
172                        velocity.x = -velocity.x;
173                        this->fireEvent();
174                    }
175        }
176
177        // Set the position, velocity and acceleration of the ball, if they have changed.
178        if (acceleration != this->getAcceleration())
179            this->setAcceleration(acceleration);
180        if (velocity != this->getVelocity())
181            this->setVelocity(velocity);
182        if (position != this->getPosition())
183            this->setPosition(position);
184    }
185
186    /**
187    @brief
188        Set the speed of the ball (in x-direction).
189    @param speed
190        The speed to be set.
191    */
192    void OrxoBloxBall::setSpeed(float speed)
193    {
194        if (speed != this->speed_) // If the speed changes
195        {
196            this->speed_ = speed;
197
198            // Set the speed in the direction of the balls current velocity.
199            Vector3 velocity = this->getVelocity();
200            if (velocity.x != 0)
201                velocity.x = sgn(velocity.x) * this->speed_;
202            else // If the balls current velocity is zero, the speed is set in a random direction.
203                velocity.x = this->speed_ * sgn(rnd(-1,1));
204            //velocity.y = this->speed_;
205            velocity.z = this->speed_;
206
207            this->setVelocity(velocity);
208        }
209    }
210
211
212    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
213    {
214        if( defScoreSound_ )
215            defScoreSound_->setSource(OrxoBloxSound);
216        else
217            assert(0); // This should never happen, because soundpointer is only available on master
218    }
219
220    const std::string& OrxoBloxBall::getDefScoreSound()
221    {
222        if( defScoreSound_ )
223            return defScoreSound_->getSource();
224        else
225            assert(0);
226        return BLANKSTRING;
227    }
228
229    void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound)
230    {
231        if( defBatSound_ )
232            defBatSound_->setSource(OrxoBloxSound);
233        else
234            assert(0); // This should never happen, because soundpointer is only available on master
235    }
236
237    const std::string& OrxoBloxBall::getDefBatSound()
238    {
239        if( defBatSound_ )
240            return defBatSound_->getSource();
241        else
242            assert(0);
243        return BLANKSTRING;
244    }
245
246    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
247    {
248        if( defBoundarySound_ )
249            defBoundarySound_->setSource(OrxoBloxSound);
250        else
251            assert(0); // This should never happen, because soundpointer is only available on master
252    }
253
254    const std::string& OrxoBloxBall::getDefBoundarySound()
255    {
256        if( defBoundarySound_ )
257            return defBoundarySound_->getSource();
258        else
259            assert(0);
260        return BLANKSTRING;
261    }
262}
Note: See TracBrowser for help on using the repository browser.