Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/weapons/projectiles/GravityBombField.cc @ 11787

Last change on this file since 11787 was 11103, checked in by landauf, 9 years ago

fixed warnings (release build).
fixed error in GravityBombField

  • Property svn:eol-style set to native
File size: 7.5 KB
RevLine 
[10341]1/*
2 * GravityBombField.cc
3 *
4 *  Created on: Apr 2, 2015
[10455]5 *      Author: Manuel Eggimann
[10341]6 */
7
8#include "GravityBombField.h"
[10391]9#include "graphics/Model.h"
[10341]10
11namespace orxonox{
[10601]12    RegisterClass(GravityBombField);
[10341]13
[10601]14    //Change these constants to alter the behaviour of the field.
15   
16    const float GravityBombField::FORCE_FIELD_LIFETIME = 15;
17    const float GravityBombField::FORCE_SPHERE_START_RADIUS = 250;
18    const float GravityBombField::FORCE_SPHERE_START_STRENGTH = -700;
19    const float GravityBombField::PEAK_EXPLOSION_FORCE = 5e4;
20    const float GravityBombField::FORCE_FIELD_EXPLOSION_DAMMAGE = 100;
21    const float GravityBombField::EXPLOSION_DURATION = 1;
22    const float GravityBombField::EXPLOSION_RADIUS = 600;
23    const float GravityBombField::PEAK_ANGULAR_VELOCITY = 20;
24    const float GravityBombField::CENTRE_MODEL_END_SIZE = 1.5;
[10341]25
[10601]26    GravityBombField::GravityBombField(Context* context) : ForceField(context),RadarViewable(this, static_cast<WorldEntity*>(this))
27    {
28        RegisterObject(GravityBombField);
29        //Initialize variable with their initial values.
30        lifetime_=FORCE_FIELD_LIFETIME;
31        forceStrength_ = FORCE_SPHERE_START_STRENGTH;
32        forceSphereRadius_ = FORCE_SPHERE_START_RADIUS;
33        modelScaling_ = 1;
34        fieldExploded_ = false;
[10409]35
[11083]36        setFieldVelocity(FORCE_SPHERE_START_STRENGTH);
[10601]37        setDiameter(2*FORCE_SPHERE_START_RADIUS);
38        setMode(modeSphere_s);
39        setCollisionResponse(false);
[10391]40
[10601]41        //Make the Field visible on Radar and minimap.
42        this->setRadarObjectColour(ColourValue(1.0, 0.0, 0.2,1)); // Red
[11071]43        this->setRadarObjectShape(RadarViewable::Shape::Dot);
[10601]44        this->setRadarObjectScale(1.0f);
45       
[10409]46
[10601]47        //Attach Model
48        Model* model = new Model(this->getContext());
49        model->setMeshSource("GravityBomb.mesh"); //Demo Model from SimpleRocket
50        model->scale(2.5f);
51        bombModel_ = new MovableEntity(context);
52        bombModel_->attach(model);
53        this->attach(bombModel_);
[10409]54
[10601]55        //Add a Backlight to the centre.
56        centreLight_ = new Backlight(context);
57        centreLight_->setColour(ColourValue(0.2,0.9,0.2,1));
58        centreLight_->setScale(0.3);
59        centreLight_->setTrailMaterial("Trail/backlighttrail");
60        centreLight_->setMaterial("Examples/Flare");
61        centreLight_->setLifetime(20);
62        bombModel_->attach(centreLight_);
[10435]63
[10601]64        //Let the Bomb Modell in the centre rotate in a random direction.
65        Vector3 randomRotation;
[10621]66        randomRotation.x = rnd();
67        randomRotation.y = rnd();
[11103]68        randomRotation.z = rnd();
[10601]69        randomRotation.normalise();
70        bombModel_->setAngularAcceleration(randomRotation*(PEAK_ANGULAR_VELOCITY/FORCE_FIELD_LIFETIME));
[10435]71
[10601]72        //Add Collision Shape
73        SphereCollisionShape* collisionShape = new SphereCollisionShape(context);
74        collisionShape->setRadius(10.0);
75        this->attachCollisionShape(collisionShape);
[10435]76
[10601]77        //Add particle effect to visualize the force field.
78        this->particleSphere_ = new ParticleEmitter(this->getContext());
79        this->attach(this->particleSphere_);
80        particleSphere_->setSource("Orxonox/GravityBombField");
[10455]81
[10601]82        //Add a sound effect to the field.
83        WorldSound* fieldSound = new WorldSound(context);
84        fieldSound->setSource("sounds/GravityField.ogg");
85        fieldSound->setLooping(true);
86        fieldSound->setVolume(1.0);
87        this->attach(fieldSound);
88        fieldSound->play();
89    }
[10341]90
[10601]91    GravityBombField::~GravityBombField(){}
[10341]92
[10601]93   
94    void GravityBombField::tick(float dt)
95    {
96        SUPER(GravityBombField,tick,dt);
97        lifetime_-=dt;
[10435]98
[10601]99        if(lifetime_ > EXPLOSION_DURATION)//If field is still alive, make it smaller and stronger.
100        {
101            modelScaling_ += ((CENTRE_MODEL_END_SIZE-1) / FORCE_FIELD_LIFETIME)*dt;
102            forceStrength_ *= (1+dt/10);
103            forceSphereRadius_ = FORCE_SPHERE_START_RADIUS*(1-((FORCE_FIELD_LIFETIME-lifetime_)/FORCE_FIELD_LIFETIME)*((FORCE_FIELD_LIFETIME-lifetime_)/FORCE_FIELD_LIFETIME)*((FORCE_FIELD_LIFETIME-lifetime_)/FORCE_FIELD_LIFETIME));
104        }
105        else if(lifetime_ > 0)
106        {
107            if (!fieldExploded_) // Start the field explosion if it has not been started yet.
108            {
109                forceStrength_ = pow((EXPLOSION_DURATION + lifetime_),4)/EXPLOSION_DURATION * PEAK_EXPLOSION_FORCE;
110                fieldExploded_ = true;
[10409]111
[10601]112                //Add particle effect to visualize explosion
113                explosionCross_ = new ParticleEmitter(this->getContext());
114                explosionCross_->setSource("Orxonox/FieldExplosion");
[10621]115                explosionCross_->setOrientation(rnd(), rnd(), rnd(), rnd());
[10601]116                explosionCross_->setScale(0.7);
117                this->attach(explosionCross_);
[10455]118
[10601]119                //Add explosion sound effect.
120                explosionSound_ = new WorldSound(getContext());
121                explosionSound_->setSource("sounds/GravityFieldExplosion.ogg");
122                explosionSound_->setVolume(1.0);
123                explosionSound_->play();
124            }
[10435]125
[10601]126            //Check if any pawn is inside the shockwave and hit it with dammage proportional to the distance between explosion centre and pawn. Make sure, the same pawn is damaged only once.
[11071]127            for (Pawn* pawn : ObjectList<Pawn>())
[10601]128            {
[11071]129                Vector3 distanceVector = pawn->getWorldPosition()-this->getWorldPosition();
[10601]130                 //orxout(debug_output) << "Found Pawn:" << it->getWorldPosition() << endl;
131                if(distanceVector.length()< forceSphereRadius_)
132                {
133                    //orxout(debug_output) << "Force sphere radius is: " << forceSphereRadius_ << " Distance to Pawn is: " << distanceVector.length();
[11071]134                    if (std::find(victimsAlreadyDamaged_.begin(),victimsAlreadyDamaged_.end(),pawn) == victimsAlreadyDamaged_.end())
[10601]135                    {
136                        //orxout(debug_output) << "Found Pawn to damage: " << it->getWorldPosition() << endl;
137                        float damage = FORCE_FIELD_EXPLOSION_DAMMAGE*(1-distanceVector.length()/EXPLOSION_RADIUS);
138                        //orxout(debug_output) << "Damage: " << damage << endl;
[11071]139                        pawn->hit(shooter_, pawn->getWorldPosition(), nullptr, damage, 0,0);
140                        victimsAlreadyDamaged_.push_back(pawn);
[10601]141                    }
142                }
143            }
[10435]144
[10601]145            forceSphereRadius_ = EXPLOSION_RADIUS*(1-lifetime_/EXPLOSION_DURATION);
146            explosionCross_->setScale(forceSphereRadius_/FORCE_SPHERE_START_RADIUS);
147        }
148        else if (lifetime_ > -6) //The field has to exist for 6 more seconds for the particles of the particle effect to vanish smoothly.
149        {
150            //Make the bomb model invisible, let the strength of the field be zero and remove all particle emitters so the particle effect will slowly vanish.
151            bombModel_->setVisible(false);
152            this->setRadarVisibility(false);
153            forceStrength_ = 0;
154            forceSphereRadius_ = 0.00001;
155            particleSphere_->getParticleInterface()->removeAllEmitters();
156            explosionCross_->getParticleInterface()->removeAllEmitters();
157        }
158       
159        setDiameter(forceSphereRadius_*2);
[11083]160        setFieldVelocity(forceStrength_);
[10601]161        if(lifetime_>0) particleSphere_->setScale(forceSphereRadius_/FORCE_SPHERE_START_RADIUS);
162        bombModel_->setScale(modelScaling_);
[10409]163
[10601]164        if (lifetime_ <= -4)
165        {
166            this->destroy();
167        }
168    }
[10341]169
[10601]170    void GravityBombField::destroy()
171    {
172        ForceField::destroy();
173    }
[10341]174
175}
Note: See TracBrowser for help on using the repository browser.