Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Changed counter

File size: 7.1 KB
Line 
1#include "BallProjectile.h"
2#include "gametypes/Gametype.h"
3
4
5#include <OgreParticleEmitter.h>
6#include "core/CoreIncludes.h"
7#include "tools/ParticleInterface.h"
8#include "Scene.h"
9#include "core/command/Executor.h"
10#include "util/Convert.h"
11#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
12#include <bullet/LinearMath/btVector3.h>
13
14namespace orxonox
15{
16    RegisterClass(BallProjectile);
17
18    BallProjectile::BallProjectile(Context* context) : BillboardProjectile(context)
19    {
20        RegisterObject(BallProjectile);
21        this->textureIndex_ = 1;
22        this->setMass(0.1f);
23        this->maxTextureIndex_ = 8;
24        this->setDestroyAfterCollision(false); //I want the ball to bounce, not to be destroyed
25        this->fieldWidth_ = 46;
26        this->fieldHeight_ =  49;
27        this->orxoblox_ = this->getOrxoBlox();
28        this->setCollisionShapeRadius(2.5);
29        this->setDamage(1000);
30        orxoblox_->count();
31        //setEffect("Orxonox/sparks2");
32    }
33
34    void BallProjectile::registerVariables()
35    {
36        registerVariable(this->materialBase_);
37        registerVariable( this->speed_ );
38    }
39
40    /**
41    @brief
42        Set the material.
43    @param material
44        The name of the material. Material names with 1 to 8 appended must exist.
45    */
46    void BallProjectile::setMaterial(const std::string& material)
47    {
48        this->materialBase_ = material;
49
50        BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_));
51    }
52
53    /**
54    @brief
55        Change the texture.
56    */
57    void BallProjectile::changeTexture()
58    {
59        this->textureIndex_++;
60        if (this->textureIndex_ > this->maxTextureIndex_)
61            this->textureIndex_ = 1;
62
63        this->setMaterial(this->materialBase_);
64    }
65
66
67
68    void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) {
69
70        Vector3 velocity = this->getVelocity();
71        Vector3 myPosition = otherObject->getPosition();
72        btVector3 positionOtherObject = contactPoint.getPositionWorldOnA();
73       
74            int distance_X = positionOtherObject.getX() - myPosition.x;
75            int distance_Z = positionOtherObject.getZ() - myPosition.z;
76
77            if (distance_X < 0)
78                distance_X = -distance_X;
79   
80
81            if (distance_Z < 0)
82                distance_Z = -distance_Z;
83
84            if (distance_X < distance_Z) {
85                velocity.z = -velocity.z;
86            }
87            if (distance_Z < distance_X) {
88                velocity.x = -velocity.x;
89            }
90            else {
91                velocity.x = -velocity.x;
92                velocity.z = -velocity.z;
93            }
94            this->setVelocity(velocity);
95    }
96
97   
98    bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs)
99    {
100        bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs);
101        if (result == true) {
102            if (otherObject->isA(Class(OrxoBloxStones))) {
103                Bounce(otherObject, contactPoint, cs);
104               
105            }
106        }
107        return result;
108    }
109
110
111   
112    OrxoBlox* BallProjectile::getOrxoBlox()
113    {
114        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
115        {
116            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
117            return orxobloxGametype;
118        }
119        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
120        return nullptr;
121    }
122   
123
124
125    void BallProjectile::tick(float dt)
126    {
127        SUPER(BallProjectile, tick, dt);
128
129               // Get the current position, velocity and acceleration of the ball.
130        bool suicidal = false;
131        Vector3 position = this->getPosition();
132        Vector3 velocity = this->getVelocity();
133        Vector3 acceleration = this->getAcceleration();
134
135        velocity.y = 0;
136        position.y = 0;
137
138        // 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).
139        if (position.z > this->fieldHeight_ || position.z < -this->fieldHeight_)
140        {
141
142            velocity.z = -velocity.z;
143            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
144            if (position.z > this->fieldHeight_){
145                // Set the ball to be exactly at the boundary.
146                position.z = this-> fieldHeight_;
147               
148                suicidal = true;
149               
150            }
151            if (position.z < -this->fieldHeight_){
152                position.z = -this->fieldHeight_;
153               
154            }
155
156            this->fireEvent();
157        }
158       
159        //Ball hits the right or left wall and should bounce back.
160        // If the ball has crossed the left or right boundary of the playing field.
161        if (position.x > this->fieldWidth_ || position.x < -this->fieldWidth_)
162        {
163            //Ball hits the right Wall
164            if (position.x > this->fieldWidth_)
165                {
166                    // Set the ball to be exactly at the boundary.
167                    position.x = this->fieldWidth_;
168                    // Invert its velocity in x-direction (i.e. it bounces off).
169                    velocity.x = -velocity.x;
170                    this->fireEvent();
171                    }
172
173            //Ball hits the left wall
174            else if (position.x < -this->fieldWidth_)
175                {
176                        // Set the ball to be exactly at the boundary.
177                        position.x = -this->fieldWidth_;
178                        // Invert its velocity in x-direction (i.e. it bounces off).
179                        velocity.x = -velocity.x;
180                        this->fireEvent();
181                    }
182        }
183
184        // Set the position, velocity and acceleration of the ball, if they have changed.
185        if (acceleration != this->getAcceleration())
186            this->setAcceleration(acceleration);
187        if (velocity != this->getVelocity())
188            this->setVelocity(velocity);
189        if (position != this->getPosition())
190            this->setPosition(position);
191        //this->Collides((this->orxoblox_->CheckForCollision(this)));
192        if (suicidal == true) {
193            this->destroy();
194        }
195
196    }
197
198
199
200    void BallProjectile::setSpeed(float speed)
201    {
202        if (speed != this->speed_) // If the speed changes
203        {
204            this->speed_ = speed;
205
206            // Set the speed in the direction of the balls current velocity.
207            Vector3 velocity = this->getVelocity();
208            if (velocity.x != 0)
209                velocity.x = sgn(velocity.x) * this->speed_;
210            else // If the balls current velocity is zero, the speed is set in a random direction.
211                velocity.x = this->speed_ * sgn(rnd(-1,1));
212
213            this->setVelocity(velocity);
214        }
215    }
216
217
218    //This is an override to prevent getting killed by the program
219    void BallProjectile::destroyObject(void)
220    {
221        if(GameMode::isMaster()) {
222        }
223    }
224
225
226
227}
Note: See TracBrowser for help on using the repository browser.