Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

We can shoot

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