Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed count function (reverted changes)

File size: 7.1 KB
RevLine 
[12281]1#include "BallProjectile.h"
[12368]2#include "gametypes/Gametype.h"
[12281]3
[12368]4
[12281]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"
[12310]11#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
12#include <bullet/LinearMath/btVector3.h>
[12281]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
[12371]25        this->fieldWidth_ = 46;
26        this->fieldHeight_ =  49;
[12384]27        this->orxoblox_ = this->getOrxoBlox();
[12387]28        this->setCollisionShapeRadius(2.5);
[12394]29        this->setDamage(1000);
[12401]30                //setEffect("Orxonox/sparks2");
[12281]31    }
32
33    void BallProjectile::registerVariables()
34    {
35        registerVariable(this->materialBase_);
[12310]36        registerVariable( this->speed_ );
[12281]37    }
38
39    /**
40    @brief
41        Set the material.
42    @param material
43        The name of the material. Material names with 1 to 8 appended must exist.
44    */
45    void BallProjectile::setMaterial(const std::string& material)
46    {
47        this->materialBase_ = material;
48
49        BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_));
50    }
51
52    /**
53    @brief
54        Change the texture.
55    */
56    void BallProjectile::changeTexture()
57    {
58        this->textureIndex_++;
59        if (this->textureIndex_ > this->maxTextureIndex_)
60            this->textureIndex_ = 1;
61
62        this->setMaterial(this->materialBase_);
63    }
64
[12310]65
66
[12281]67    void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) {
68
69        Vector3 velocity = this->getVelocity();
[12310]70        Vector3 myPosition = otherObject->getPosition();
71        btVector3 positionOtherObject = contactPoint.getPositionWorldOnA();
[12281]72       
[12310]73            int distance_X = positionOtherObject.getX() - myPosition.x;
74            int distance_Z = positionOtherObject.getZ() - myPosition.z;
[12281]75
76            if (distance_X < 0)
[12310]77                distance_X = -distance_X;
[12281]78   
79
[12310]80            if (distance_Z < 0)
81                distance_Z = -distance_Z;
[12281]82
[12310]83            if (distance_X < distance_Z) {
84                velocity.z = -velocity.z;
85            }
86            if (distance_Z < distance_X) {
[12281]87                velocity.x = -velocity.x;
[12310]88            }
[12281]89            else {
90                velocity.x = -velocity.x;
[12310]91                velocity.z = -velocity.z;
[12281]92            }
[12310]93            this->setVelocity(velocity);
[12281]94    }
95
96   
97    bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs)
98    {
99        bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs);
[12371]100        if (result == true) {
101            if (otherObject->isA(Class(OrxoBloxStones))) {
102                Bounce(otherObject, contactPoint, cs);
[12393]103               
[12371]104            }
105        }
[12281]106        return result;
107    }
[12310]108
109
[12384]110   
[12368]111    OrxoBlox* BallProjectile::getOrxoBlox()
112    {
113        if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox)))
114        {
115            OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype());
116            return orxobloxGametype;
117        }
118        else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl;
119        return nullptr;
120    }
[12384]121   
[12310]122
123
124    void BallProjectile::tick(float dt)
125    {
126        SUPER(BallProjectile, tick, dt);
127
[12368]128               // Get the current position, velocity and acceleration of the ball.
[12388]129        bool suicidal = false;
[12310]130        Vector3 position = this->getPosition();
131        Vector3 velocity = this->getVelocity();
132        Vector3 acceleration = this->getAcceleration();
133
[12371]134        velocity.y = 0;
135        position.y = 0;
136
[12310]137        // 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).
[12371]138        if (position.z > this->fieldHeight_ || position.z < -this->fieldHeight_)
[12310]139        {
[12368]140
[12310]141            velocity.z = -velocity.z;
[12368]142            // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!!
[12371]143            if (position.z > this->fieldHeight_){
[12368]144                // Set the ball to be exactly at the boundary.
[12371]145                position.z = this-> fieldHeight_;
[12401]146                orxoblox_->count();
147
[12388]148                suicidal = true;
[12368]149               
150            }
[12371]151            if (position.z < -this->fieldHeight_){
152                position.z = -this->fieldHeight_;
[12368]153               
154            }
155
[12310]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.
[12371]161        if (position.x > this->fieldWidth_ || position.x < -this->fieldWidth_)
[12310]162        {
163            //Ball hits the right Wall
[12371]164            if (position.x > this->fieldWidth_)
[12310]165                {
166                    // Set the ball to be exactly at the boundary.
[12371]167                    position.x = this->fieldWidth_;
[12310]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
[12371]174            else if (position.x < -this->fieldWidth_)
[12310]175                {
176                        // Set the ball to be exactly at the boundary.
[12371]177                        position.x = -this->fieldWidth_;
[12310]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);
[12368]191        //this->Collides((this->orxoblox_->CheckForCollision(this)));
[12388]192        if (suicidal == true) {
193            this->destroy();
194        }
[12368]195
[12310]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
[12396]217
218    //This is an override to prevent getting killed by the program
[12378]219    void BallProjectile::destroyObject(void)
220    {
221        if(GameMode::isMaster()) {
222        }
223    }
[12310]224
[12378]225
226
[12281]227}
Note: See TracBrowser for help on using the repository browser.