Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoBlox_FS19/src/modules/weapons/projectiles/BallProjectile.cc @ 12376

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

cleanup function worksls!

File size: 9.5 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 ParticleProjectile.h
31    @brief Implementation of the ParticleProjectile class.
32*/
33
34#include "BallProjectile.h"
35#include "../OrxoBlox/OrxoBlox.h"
36#include "../OrxoBlox/OrxoBloxStones.h"
37#include "gametypes/Gametype.h"
38
39
40#include <OgreParticleEmitter.h>
41#include "core/CoreIncludes.h"
42#include "tools/ParticleInterface.h"
43#include "Scene.h"
44#include "core/command/Executor.h"
45#include "util/Convert.h"
46#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
47#include <bullet/LinearMath/btVector3.h>
48
49namespace orxonox
50{
51    RegisterClass(BallProjectile);
52
53    BallProjectile::BallProjectile(Context* context) : BillboardProjectile(context)
54    {
55        RegisterObject(BallProjectile);
56        this->textureIndex_ = 1;
57        this->setMass(0.1f);
58        this->maxTextureIndex_ = 8;
59        this->setDestroyAfterCollision(false); //I want the ball to bounce, not to be destroyed
60        this->fieldWidth_ = 46;
61        this->fieldHeight_ =  49;
62        //this->orxoblox_ = this->getOrxoBlox();
63
64        //setEffect("Orxonox/sparks2");
65    }
66
67    void BallProjectile::registerVariables()
68    {
69        registerVariable(this->materialBase_);
70        registerVariable( this->speed_ );
71    }
72
73    /**
74    @brief
75        Set the material.
76    @param material
77        The name of the material. Material names with 1 to 8 appended must exist.
78    */
79    void BallProjectile::setMaterial(const std::string& material)
80    {
81        this->materialBase_ = material;
82
83        BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_));
84    }
85
86    /**
87    @brief
88        Change the texture.
89    */
90    void BallProjectile::changeTexture()
91    {
92        this->textureIndex_++;
93        if (this->textureIndex_ > this->maxTextureIndex_)
94            this->textureIndex_ = 1;
95
96        this->setMaterial(this->materialBase_);
97    }
98
99
100
101    void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) {
102
103        Vector3 velocity = this->getVelocity();
104        Vector3 myPosition = otherObject->getPosition();
105        btVector3 positionOtherObject = contactPoint.getPositionWorldOnA();
106        //orxout() << "About to Bounce >D" << endl;
107        //if (positionOtherObject.y < 0) {
108            //this.destroy()
109        //}S
110        //else {
111       
112            int distance_X = positionOtherObject.getX() - myPosition.x;
113            int distance_Z = positionOtherObject.getZ() - myPosition.z;
114
115            if (distance_X < 0)
116                distance_X = -distance_X;
117   
118
119            if (distance_Z < 0)
120                distance_Z = -distance_Z;
121
122            //orxout() << distance_X << endl;
123            //orxout() << distance_Z << endl;
124
125            if (distance_X < distance_Z) {
126                velocity.z = -velocity.z;
127                //orxout() << "z" << endl;
128            }
129            if (distance_Z < distance_X) {
130                velocity.x = -velocity.x;
131                //orxout() << "x" << endl;
132            }
133            else {
134                velocity.x = -velocity.x;
135                velocity.z = -velocity.z;
136                //orxout() << "both" << endl;
137            }
138            this->setVelocity(velocity);
139        //}
140    }
141
142   
143    bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs)
144    {
145
146        //orxout() << "wanna bounce..." << endl;
147        bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs);
148        if (result == true) {
149            if (otherObject->isA(Class(OrxoBloxStones))) {
150                Bounce(otherObject, contactPoint, cs);
151            }
152        }
153        //orxout() << "BOUNCED!" << endl;
154
155        return result;
156    }
157
158
159    /*
160    OrxoBlox* BallProjectile::getOrxoBlox()
161    {
162        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
163        {
164            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
165            return orxobloxGametype;
166        }
167        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
168        return nullptr;
169    }
170    */
171
172
173    void BallProjectile::tick(float dt)
174    {
175        SUPER(BallProjectile, tick, dt);
176
177               // Get the current position, velocity and acceleration of the ball.
178        Vector3 position = this->getPosition();
179        Vector3 velocity = this->getVelocity();
180        Vector3 acceleration = this->getAcceleration();
181
182        velocity.y = 0;
183        position.y = 0;
184
185        // 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).
186        if (position.z > this->fieldHeight_ || position.z < -this->fieldHeight_)
187        {
188
189            velocity.z = -velocity.z;
190            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
191            if (position.z > this->fieldHeight_){
192                // Set the ball to be exactly at the boundary.
193                position.z = this-> fieldHeight_;
194               
195                //orxoblox_->LevelUp();
196
197
198                //this->setSpeed(0); // doesn't work here, why??;
199                //Stopping ball
200                //orxout() << "Ball stopped" << endl;
201                velocity.x = 0;
202                velocity.y = 0;
203                velocity.z = 0; 
204
205                //ChatManager::message("Waiting for your input");
206                //Input new speed here:
207                //ChatManager::message("Setting new speed");
208               
209                //%%%%%%%%%%%%
210                //MAUSPOSITION
211                //%%%%%%%%%%%%
212                //Reads current mouse position
213                //TODO: read Mouse position on click!
214                //int mousex = InputManager::getInstance().getMousePosition().first;
215                //int mousey = InputManager::getInstance().getMousePosition().second;
216                //ChatManager::message("Read mouse position");
217                //orxout() << "Mouseposition" << endl;
218                //orxout() << mousex << endl;
219                //ChatManager::message(mousex);
220                //ChatManager::message("mousey");
221                //ChatManager::message(mousey);
222                //Set new speed here!!
223                velocity.x = rnd(-100,100);
224                velocity.z = rnd(-50,-100);
225               
226
227            }
228            if (position.z < -this->fieldHeight_){
229                position.z = -this->fieldHeight_;
230               
231            }
232
233            this->fireEvent();
234        }
235       
236        //Ball hits the right or left wall and should bounce back.
237        // If the ball has crossed the left or right boundary of the playing field.
238        if (position.x > this->fieldWidth_ || position.x < -this->fieldWidth_)
239        {
240            //Ball hits the right Wall
241            if (position.x > this->fieldWidth_)
242                {
243                    // Set the ball to be exactly at the boundary.
244                    position.x = this->fieldWidth_;
245                    // Invert its velocity in x-direction (i.e. it bounces off).
246                    velocity.x = -velocity.x;
247                    this->fireEvent();
248                    }
249
250            //Ball hits the left wall
251            else if (position.x < -this->fieldWidth_)
252                {
253                        // Set the ball to be exactly at the boundary.
254                        position.x = -this->fieldWidth_;
255                        // Invert its velocity in x-direction (i.e. it bounces off).
256                        velocity.x = -velocity.x;
257                        this->fireEvent();
258                    }
259        }
260
261        // Set the position, velocity and acceleration of the ball, if they have changed.
262        if (acceleration != this->getAcceleration())
263            this->setAcceleration(acceleration);
264        if (velocity != this->getVelocity())
265            this->setVelocity(velocity);
266        if (position != this->getPosition())
267            this->setPosition(position);
268        //this->Collides((this->orxoblox_->CheckForCollision(this)));
269
270    }
271
272
273
274    void BallProjectile::setSpeed(float speed)
275    {
276        if (speed != this->speed_) // If the speed changes
277        {
278            this->speed_ = speed;
279
280            // Set the speed in the direction of the balls current velocity.
281            Vector3 velocity = this->getVelocity();
282            if (velocity.x != 0)
283                velocity.x = sgn(velocity.x) * this->speed_;
284            else // If the balls current velocity is zero, the speed is set in a random direction.
285                velocity.x = this->speed_ * sgn(rnd(-1,1));
286
287            this->setVelocity(velocity);
288        }
289    }
290
291
292}
Note: See TracBrowser for help on using the repository browser.