Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12210 was 12210, checked in by pomselj, 5 years ago

created a folder for our module and started implementing the ball class

File size: 9.5 KB
Line 
1
2//TODO: Sounds (all the sounds are still from the pong module...)
3//TODO: Blocks (the Ball-Block comunication is based on how the blocks are implemented)
4//TODO: The bottom boundary/ the Ball collecter
5//TODO: Ability to shoot the ball (the ball is still constructed like the pong ball)
6
7/*
8 *   ORXONOX - the hottest 3D action shooter ever to exist
9 *                    > www.orxonox.net <
10 *
11 *
12 *   License notice:
13 *
14 *   This program is free software; you can redistribute it and/or
15 *   modify it under the terms of the GNU General Public License
16 *   as published by the Free Software Foundation; either version 2
17 *   of the License, or (at your option) any later version.
18 *
19 *   This program is distributed in the hope that it will be useful,
20 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 *   GNU General Public License for more details.
23 *
24 *   You should have received a copy of the GNU General Public License
25 *   along with this program; if not, write to the Free Software
26 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
27 *
28 *   Author:
29 *      Fabian 'x3n' Landau
30 *   Co-authors:
31 *      ...
32 *
33 */
34
35/**
36    @file OrxoBloxBall.cc
37    @brief Implementation of the OrxoBloxBall class.
38*/
39
40#include "OrxoBloxBall.h"
41
42#include "core/CoreIncludes.h"
43#include "core/GameMode.h"
44
45#include "gametypes/Gametype.h"
46
47#include "OrxoBloxBlocks.h"
48
49#include "sound/WorldSound.h"
50#include "core/XMLPort.h"
51
52namespace orxonox
53{
54    RegisterClass(OrxoBloxBall);
55
56    const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5;
57
58    /**
59    @brief
60        Constructor. Registers and initializes the object.
61    */
62    OrxoBloxBall::OrxoBloxBall(Context* context)
63        : MovableEntity(context)
64    {
65        RegisterObject(OrxoBloxBall);
66
67        this->speed_ = 0;
68        this->accelerationFactor_ = 1.0f;
69        this->block_ = nullptr;
70        this->bDeleteBlock_ = false;
71        this->blockID_ = new unsigned int[100];
72        for (int i = 0; i < 100; i++) {
73                this->blockID_[i] = OBJECTID_UNKNOWN;
74        }
75
76        this->registerVariables();
77
78        //initialize sound
79        if (GameMode::isMaster())
80             {
81                 this->defScoreSound_ = new WorldSound(this->getContext());
82                 this->defScoreSound_->setVolume(1.0f);
83                 this->defBatSound_ = new WorldSound(this->getContext());
84                 this->defBatSound_->setVolume(0.4f);
85                 this->defBoundarySound_ = new WorldSound(this->getContext());
86                 this->defBoundarySound_->setVolume(0.5f);
87             }
88             else
89             {
90                 this->defScoreSound_ = nullptr;
91                 this->defBatSound_ = nullptr;
92                 this->defBoundarySound_ = nullptr;
93             }
94    }
95
96    /**
97    @brief
98        Destructor.
99    */
100    OrxoBloxBall::~OrxoBloxBall()
101    {
102        if (this->isInitialized())
103        {
104            if (this->bDeleteBlock_)
105                delete[] this->block_;
106
107            delete[] this->blockID_;
108        }
109    }
110
111    //xml port for loading sounds
112    void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
113    {
114        SUPER(OrxoBloxBall, XMLPort, xmlelement, mode);
115        XMLPortParam(OrxoBloxBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
116        XMLPortParam(OrxoBloxBall, "defBatSound",  setDefBatSound,  getDefBatSound,  xmlelement, mode);
117        XMLPortParam(OrxoBloxBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
118    }
119
120    /**
121    @brief
122        Register variables to synchronize over the network.
123    */
124    void OrxoBloxBall::registerVariables()
125    {
126        registerVariable( this->fieldWidth_ );
127        registerVariable( this->fieldHeight_ );
128        registerVariable( this->blocklength_ );
129        registerVariable( this->speed_ );
130        registerVariable( this->blockID_[0] );
131        registerVariable( this->blockID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBlock) );
132    }
133
134    /**
135    @brief
136        Is called every tick.
137        Handles the movement of the ball and its interaction with the boundaries and blocks.
138    @param dt
139        The time since the last tick.
140    */
141    void OrxoBloxBall::tick(float dt)
142    {
143        SUPER(OrxoBloxBall, tick, dt);
144
145        // Get the current position, velocity and acceleration of the ball.
146        Vector3 position = this->getPosition();
147        Vector3 velocity = this->getVelocity();
148        Vector3 acceleration = this->getAcceleration();
149
150        // If the ball has hit the boundaries on either the right side or the left
151        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
152        {
153            defBoundarySound_->play(); //play boundary sound
154            // Its velocity in x-direction is inverted (i.e. it bounces off).
155            velocity.x = -velocity.x;
156            // And its position is set as to not overstep the boundary it has just crossed.
157            if (position.x > this->fieldWidth_ / 2)
158                position.x = this->fieldWidth_ / 2;
159            if (position.x < -this->fieldWidth_ / 2)
160                position.x = -this->fieldWidth_ / 2;
161
162            this->fireEvent();
163        }
164
165        // If the ball has hit the boundary on the top
166        if (position.z > this->fieldHeight_ / 2)
167        {
168            defBoundarySound_->play(); //play boundary sound
169            // Its velocity in z-direction is inverted (i.e. it bounces off).
170            velocity.z = -velocity.z;
171            // And its position is set as to not overstep the boundary it has just crossed.
172            position.z = this->fieldHeight_ / 2;
173
174            this->fireEvent();
175        }
176
177        // If the ball has crossed the bottom boundary
178        if (position.z < -this->fieldHeight_ / 2)
179        {
180        //TODO: Ball Collector
181        }
182
183        // Set the position, velocity and acceleration of the ball, if they have changed.
184        if (acceleration != this->getAcceleration())
185            this->setAcceleration(acceleration);
186        if (velocity != this->getVelocity())
187            this->setVelocity(velocity);
188        if (position != this->getPosition())
189            this->setPosition(position);
190    }
191
192    /**
193    @brief
194        Set the speed of the ball (in x-direction).
195    @param speed
196        The speed to be set.
197    */
198    void OrxoBloxBall::setSpeed(float speed)
199    {
200        if (speed != this->speed_) // If the speed changes
201        {
202            this->speed_ = speed;
203
204            // Set the speed in the direction of the balls current velocity.
205            Vector3 velocity = this->getVelocity();
206            if (velocity.x != 0)
207                velocity.x = sgn(velocity.x) * this->speed_;
208            else // If the balls current velocity is zero, the speed is set in a random direction.
209                velocity.x = this->speed_ * sgn(rnd(-1,1));
210
211            this->setVelocity(velocity);
212        }
213    }
214
215    /**
216    @brief
217        Set the blocks for the ball.
218    @param bats
219        An array (of size n (n=#Blocks) of weak pointers, to be set as the new blocks.
220    */
221    void OrxoBloxBall::setBlock(WeakPtr<OrxoBloxBlock>* block, int n)
222    {
223        if (this->bDeleteBlock_) // If there are already some blocks, delete them.
224        {
225            delete[] this->block_;
226            this->bDeleteBlock_ = false;
227        }
228
229        this->block_ = block;
230        // Also store their object IDs, for synchronization.
231        for (int i = 0; i < n; i++) {
232                this->blockID_[i] = this->block_[i]->getObjectID();
233        }
234    }
235
236    /**
237    @brief
238        Get the blocks over the network.
239    */
240    void OrxoBloxBall::applyBlock(int n)
241    {
242        // Make space for the blocks, if they don't exist, yet.
243        if (this->block_ == nullptr)
244        {
245            this->block_ = new WeakPtr<OrxoBloxBlock>[n];
246            this->bDeleteBlock_ = true;
247        }
248       
249        for (int i = 0; i < n; i++) {
250                if (this->blockID_[i] != OBJECTID_UNKNOWN)
251                this->bat_[i] = orxonox_cast<OrxoBloxBlock*>(Synchronisable::getSynchronisable(this->blockID_[i]));
252        }
253    }
254
255    void OrxoBloxBall::setDefScoreSound(const std::string &pongSound)
256    {
257        if( defScoreSound_ )
258            defScoreSound_->setSource(pongSound);
259        else
260            assert(0); // This should never happen, because soundpointer is only available on master
261    }
262
263    const std::string& OrxoBloxBall::getDefScoreSound()
264    {
265        if( defScoreSound_ )
266            return defScoreSound_->getSource();
267        else
268            assert(0);
269        return BLANKSTRING;
270    }
271
272    void OrxoBloxBall::setDefBatSound(const std::string &pongSound)
273    {
274        if( defBatSound_ )
275            defBatSound_->setSource(pongSound);
276        else
277            assert(0); // This should never happen, because soundpointer is only available on master
278    }
279
280    const std::string& OrxoBloxBall::getDefBatSound()
281    {
282        if( defBatSound_ )
283            return defBatSound_->getSource();
284        else
285            assert(0);
286        return BLANKSTRING;
287    }
288
289    void OrxoBloxBall::setDefBoundarySound(const std::string &pongSound)
290    {
291        if( defBoundarySound_ )
292            defBoundarySound_->setSource(pongSound);
293        else
294            assert(0); // This should never happen, because soundpointer is only available on master
295    }
296
297    const std::string& OrxoBloxBall::getDefBoundarySound()
298    {
299        if( defBoundarySound_ )
300            return defBoundarySound_->getSource();
301        else
302            assert(0);
303        return BLANKSTRING;
304    }
305}
Note: See TracBrowser for help on using the repository browser.