Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Jesus safed our souls and stopped the crashing. Hallowed be his name and hallowed be his followers sevy and aryo, first of their names, saviors of the andals the raynars and the first nerds. Fourier is love btw

File size: 10.7 KB
Line 
1#include "OrxoBloxBall.h"
2#include "OrxoBloxStones.h"
3#include "OrxoBlox.h"
4#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
5
6#include "core/CoreIncludes.h"
7#include "core/GameMode.h"
8
9#include "gametypes/Gametype.h"
10
11
12#include "sound/WorldSound.h"
13#include "core/XMLPort.h"
14#include "core/input/InputManager.h"
15
16
17namespace orxonox
18{
19    RegisterClass(OrxoBloxBall);
20
21    const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5;
22
23    /**
24    @brief
25        Constructor. Registers and initializes the object.
26    */
27    OrxoBloxBall::OrxoBloxBall(Context* context)
28        : Pawn(context)
29    {
30        RegisterObject(OrxoBloxBall);
31
32        this->speed_ = 0;
33        this->accelerationFactor_ = 1.0f;
34       
35        this->relMercyOffset_ = 0.05f;
36        this->orxoblox_ = this->getOrxoBlox();
37        this->radius_ = 3;
38
39        this->registerVariables();
40
41        //initialize sound
42        if (GameMode::isMaster())
43             {
44                 this->defScoreSound_ = new WorldSound(this->getContext());
45                 this->defScoreSound_->setVolume(1.0f);
46                 
47                 this->defBoundarySound_ = new WorldSound(this->getContext());
48                 this->defBoundarySound_->setVolume(0.5f);
49             }
50             else
51             {
52                 this->defScoreSound_ = nullptr;
53                 
54                 this->defBoundarySound_ = nullptr;
55             }
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    OrxoBloxBall::~OrxoBloxBall()
63    {
64       
65    }
66
67    //xml port for loading sounds
68    void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode)
69    {
70        SUPER(OrxoBloxBall, XMLPort, xmlelement, mode);
71        XMLPortParam(OrxoBloxBall, "defScoreSound",  setDefScoreSound,  getDefScoreSound,  xmlelement, mode);
72       
73        XMLPortParam(OrxoBloxBall, "defBoundarySound",  setDefBoundarySound,  getDefBoundarySound,  xmlelement, mode);
74    }
75
76    /**
77    @brief
78        Register variables to synchronize over the network.
79    */
80    void OrxoBloxBall::registerVariables()
81    {
82        registerVariable( this->fieldWidth_ );
83        registerVariable( this->fieldHeight_ );
84       
85        registerVariable( this->speed_ );
86        registerVariable( this->relMercyOffset_ );
87    }
88
89    /**
90    @brief
91        Is called every tick.
92        Handles the movement of the ball and its interaction with the boundaries and bats.
93    @param dt
94        The time since the last tick.
95    */
96    void OrxoBloxBall::tick(float dt)
97    {   
98       
99        SUPER(OrxoBloxBall, tick, dt);
100
101        // Get the current position, velocity and acceleration of the ball.
102        Vector3 position = this->getPosition();
103        Vector3 velocity = this->getVelocity();
104        Vector3 acceleration = this->getAcceleration();
105
106        // 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).
107        if (position.z > this->fieldHeight_ / 2 || position.z - radius_ < -this->fieldHeight_ / 2)
108        {
109            defBoundarySound_->play(); //play boundary sound
110            // Its velocity in z-direction is inverted (i.e. it bounces off).
111            velocity.z = -velocity.z;
112            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
113            if (position.z > this->fieldHeight_ / 2){
114                // Set the ball to be exactly at the boundary.
115                position.z = this-> fieldHeight_ / 2 - radius_;
116               
117                orxoblox_->LevelUp();
118                orxoblox_->LevelUp();
119                //orxout() << "LevelUp 2 finished, trying to call end() function";
120                //this->end();
121
122
123
124                //this->setSpeed(0); // doesn't work here, why??;
125                //Stopping ball
126                orxout() << "Ball stopped" << endl;
127                velocity.x = 0;
128                velocity.y = 0;
129                velocity.z = 0; 
130
131                //ChatManager::message("Waiting for your input");
132                //Input new speed here:
133                //ChatManager::message("Setting new speed");
134               
135                //%%%%%%%%%%%%
136                //MAUSPOSITION
137                //%%%%%%%%%%%%
138                //Reads current mouse position
139                //TODO: read Mouse position on click!
140                //int mousex = InputManager::getInstance().getMousePosition().first;
141                //int mousey = InputManager::getInstance().getMousePosition().second;
142                //ChatManager::message("Read mouse position");
143                //orxout() << "Mouseposition" << endl;
144                //orxout() << mousex << endl;
145                //ChatManager::message(mousex);
146                //ChatManager::message("mousey");
147                //ChatManager::message(mousey);
148                //Set new speed here!!
149                velocity.x = rnd(-100,100);
150                velocity.z = rnd(-50,-100);
151               
152
153            }
154            if (position.z - radius_ < -this->fieldHeight_ / 2){
155                position.z = -this->fieldHeight_ / 2 + radius_;
156               
157            }
158
159            this->fireEvent();
160        }
161       
162        //Ball hits the right or left wall and should bounce back.
163        // If the ball has crossed the left or right boundary of the playing field.
164        if (position.x + radius_ > this->fieldWidth_ / 2 || position.x - radius_ < -this->fieldWidth_ / 2)
165        {
166            //Ball hits the right Wall
167            if (position.x + radius_ > this->fieldWidth_ / 2)
168                {
169                    // Set the ball to be exactly at the boundary.
170                    position.x = this->fieldWidth_ / 2 - radius_;
171                    // Invert its velocity in x-direction (i.e. it bounces off).
172                    velocity.x = -velocity.x;
173                    this->fireEvent();
174                    }
175
176            //Ball hits the left wall
177            else if (position.x - radius_ < -this->fieldWidth_ / 2)
178                {
179                        // Set the ball to be exactly at the boundary.
180                        position.x = -this->fieldWidth_ / 2 + radius_;
181                        // Invert its velocity in x-direction (i.e. it bounces off).
182                        velocity.x = -velocity.x;
183                        this->fireEvent();
184                    }
185        }
186
187        // Set the position, velocity and acceleration of the ball, if they have changed.
188        if (acceleration != this->getAcceleration())
189            this->setAcceleration(acceleration);
190        if (velocity != this->getVelocity())
191            this->setVelocity(velocity);
192        if (position != this->getPosition())
193            this->setPosition(position);
194        //this->Collides((this->orxoblox_->CheckForCollision(this)));
195
196 
197    }
198
199    /**
200    @brief
201        Set the speed of the ball (in x-direction).
202    @param speed
203        The speed to be set.
204    */
205    void OrxoBloxBall::setSpeed(float speed)
206    {   
207
208        if (speed != this->speed_) // If the speed changes
209        {
210            this->speed_ = speed;
211            // Set the speed in the direction of the balls current velocity.
212            Vector3 velocity = this->getVelocity();
213            if (velocity.x != 0)
214                velocity.x = speed;
215                //velocity.x = sgn(velocity.x) * speed;
216            else // If the balls current velocity is zero, the speed is set in a random direction.
217                velocity.x = speed * sgn(rnd(-1,1));
218            //velocity.y = this->speed_;
219            velocity.z = speed;
220
221            this->setVelocity(velocity);
222        }
223    }
224
225
226    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
227    {
228        if( defScoreSound_ )
229            defScoreSound_->setSource(OrxoBloxSound);
230        else
231            assert(0); // This should never happen, because soundpointer is only available on master
232    }
233
234    const std::string& OrxoBloxBall::getDefScoreSound()
235    {
236        if( defScoreSound_ )
237            return defScoreSound_->getSource();
238        else
239            assert(0);
240        return BLANKSTRING;
241    }
242
243   
244   
245    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
246    {
247        if( defBoundarySound_ )
248            defBoundarySound_->setSource(OrxoBloxSound);
249        else
250            assert(0); // This should never happen, because soundpointer is only available on master
251    }
252
253    const std::string& OrxoBloxBall::getDefBoundarySound()
254    {
255        if( defBoundarySound_ )
256            return defBoundarySound_->getSource();
257        else
258            assert(0);
259        return BLANKSTRING;
260    }
261
262
263    void OrxoBloxBall::Bounce(WorldEntity* Stone) {
264
265        Vector3 velocity = this->getVelocity();
266        Vector3 positionStone = Stone->getPosition();
267        Vector3 myPosition = this->getPosition();
268        //orxout() << "About to Bounce >D" << endl;
269       
270            int distance_X = myPosition.x - positionStone.x;
271            int distance_Z = myPosition.z - positionStone.z;
272
273            if (distance_X < 0)
274                distance_X = -distance_X;
275   
276
277            if (distance_Z < 0)
278                distance_Z = -distance_Z;
279
280            //orxout() << distance_X << endl;
281            //orxout() << distance_Z << endl;
282
283            if (distance_X < distance_Z) {
284                velocity.z = -velocity.z;
285                //orxout() << "z" << endl; 
286            }
287            else if (distance_Z < distance_X) {
288                velocity.x = -velocity.x;
289                //orxout() << "x" << endl;       
290            }
291            else {
292                velocity.x = -velocity.x;
293                velocity.z = -velocity.z;
294                //orxout() << "both" << endl;
295            }                                 
296
297            this->setVelocity(velocity);
298            this->setPosition(myPosition);
299    }
300
301
302    void OrxoBloxBall::Collides(OrxoBloxStones* Stone)
303    {
304
305        if(Stone == nullptr)
306            return;
307
308        orxout() << "About to Bounce >D" << endl;
309        Bounce(Stone);
310        //if(otherObject->getHealth() <= 0) {
311        //Stone->destroy();
312
313        //}
314        //otherObject->reduceHealth();
315    }
316
317    bool OrxoBloxBall::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint)
318    {
319        orxout() << "detected collision" << endl;
320        Bounce(otherObject);
321
322        return true;
323    }
324
325    OrxoBlox* OrxoBloxBall::getOrxoBlox()
326    {
327        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
328        {
329            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
330            return orxobloxGametype;
331        }
332        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
333        return nullptr;
334    }
335
336    float OrxoBloxBall::getRadius() {
337        return this->radius_;
338    }
339
340
341}
Note: See TracBrowser for help on using the repository browser.