Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

content/tools/ParticleUniverse: SampleImplementationRocket.cc

File SampleImplementationRocket.cc, 10.9 KB (added by gpregger, 9 years ago)
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 *      Oliver Scheuss
24 *   Co-authors:
25 *      simonmie
26 *
27 */
28
29/**
30    @file Rocket.h
31    @brief Implementation of the Rocket class.
32*/
33
34#include "Rocket.h"
35
36#include <BulletDynamics/Dynamics/btRigidBody.h>
37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40
41#include "Scene.h"
42#include "controllers/Controller.h"
43#include "graphics/Model.h"
44#include "graphics/ParticleSpawner.h"
45#include "infos/PlayerInfo.h"
46#include "objects/collisionshapes/ConeCollisionShape.h"
47#include "sound/WorldSound.h"
48#include "worldentities/CameraPosition.h"
49#include "worldentities/pawns/Pawn.h"
50#include "particleuniverse/include/ParticleUniverseSystemManager.h"
51
52namespace orxonox
53{
54    RegisterClass(Rocket);
55   
56    ParticleUniverse::ParticleSystem* pu_rocketFire;
57    ParticleUniverse::ParticleSystemManager* pManager;
58
59    /**
60    @brief
61        Constructor. Registers the object and initializes some default values.
62    */
63    Rocket::Rocket(Context* context)
64        : ControllableEntity(context)
65        , BasicProjectile()
66        , RadarViewable(this, static_cast<WorldEntity*>(this))
67    {
68        RegisterObject(Rocket);// Register the Rocket class to the core
69
70        this->localAngularVelocity_ = 0;
71        this->lifetime_ = 100.0f;
72
73        if (GameMode::isMaster())
74        {
75            this->setCollisionType(WorldEntity::Kinematic);
76            this->setVelocity(0,0,-100);
77
78            // Create rocket model
79            Model* model = new Model(this->getContext());
80            model->setMeshSource("rocket.mesh");
81            model->scale(0.7f);
82            this->attach(model);
83
84            // Add effects.
85            //ParticleEmitter* fire = new ParticleEmitter(this->getContext());
86            //this->attach(fire);
87            //fire->setOrientation(this->getOrientation());
88            //fire->setSource("Orxonox/rocketfire");
89           
90           
91           
92           
93            // Add Particle Universe Effects -> This works but the system needs to be destroyed! (See line 225 and the plugin manual)
94                pManager = ParticleUniverse::ParticleSystemManager::getSingletonPtr();
95                pu_rocketFire = pManager->createParticleSystem("pu_rocketFire", "flameSystem", this->getScene()->getSceneManager());
96                this->attachOgreObject(pu_rocketFire);
97                pu_rocketFire->start();
98               
99               
100               
101
102            this->enableCollisionCallback();
103            this->setCollisionResponse(false);
104            this->setCollisionType(Kinematic);
105
106            // Add collision shape
107            ConeCollisionShape* collisionShape = new ConeCollisionShape(this->getContext());
108            collisionShape->setRadius(3);
109            collisionShape->setHeight(500);
110            this->attachCollisionShape(collisionShape);
111
112            this->destroyTimer_.setTimer(this->lifetime_, false, createExecutor(createFunctor(&BasicProjectile::destroyObject, this)));
113
114            // Add sound
115            this->defSndWpnEngine_ = new WorldSound(this->getContext());
116            this->defSndWpnEngine_->setLooping(true);
117            this->defSndWpnEngine_->setSource("sounds/Rocket_engine.ogg");
118            this->defSndWpnEngine_->setVolume(1.0f);
119            this->attach(defSndWpnEngine_);
120
121            this->defSndWpnLaunch_ = new WorldSound(this->getContext());
122            this->defSndWpnLaunch_->setLooping(false);
123            this->defSndWpnLaunch_->setSource("sounds/Rocket_launch.ogg");
124            this->defSndWpnLaunch_->setVolume(1.0f);
125            this->attach(defSndWpnLaunch_);
126        }
127        else
128        {
129            this->defSndWpnEngine_ = 0;
130            this->defSndWpnLaunch_ = 0;
131        }
132
133        // Add camera
134        CameraPosition* camPosition = new CameraPosition(this->getContext());
135        camPosition->setPosition(0,4,15);
136        camPosition->setAllowMouseLook(true);
137        this->addCameraPosition(camPosition);
138
139        this->setRadarObjectColour(ColourValue(1.0, 0.5, 0.0)); // orange
140        this->setRadarObjectShape(RadarViewable::Triangle);
141        this->setRadarObjectScale(0.5f);
142    }
143
144    /**
145    @brief
146        Destructor. Destroys controller, if present and kills sounds, if playing.
147    */
148    Rocket::~Rocket()
149    {
150        if(this->isInitialized())
151        {
152            if (GameMode::isMaster())
153            {
154                this->destructionEffect();
155               
156                if (this->getPlayer() && this->getController())
157                    this->player_->stopTemporaryControl();
158            }
159
160            if ( this->defSndWpnEngine_ )
161                this->defSndWpnEngine_->destroy();
162
163            if ( this->defSndWpnLaunch_ )
164                this->defSndWpnLaunch_->destroy();
165        }
166    }
167
168    /**
169    @brief
170        Sets the entity that fired the Rocket.
171    @param shooter
172        A pointer to the Pawn that fired the Rocket.
173    */
174    void Rocket::setShooter(Pawn* shooter)
175    {
176        this->BasicProjectile::setShooter(shooter);
177       
178        this->player_ = this->getShooter()->getPlayer();
179        if(this->player_)
180            this->player_->startTemporaryControl(this);
181
182        if( GameMode::isMaster() )
183        {
184            this->defSndWpnEngine_->play();
185            this->defSndWpnLaunch_->play();
186        }
187    }
188
189    /**
190    @brief
191        Defines which actions the Rocket has to take in each tick.
192    @param dt
193        The length of the tick.
194    */
195    void Rocket::tick(float dt)
196    {
197        SUPER(Rocket, tick, dt);
198
199        if( this->hasLocalController() )
200        {
201            this->setAngularVelocity(this->getOrientation() * this->localAngularVelocity_);
202            this->setVelocity( this->getOrientation()*WorldEntity::FRONT*this->getVelocity().length() );
203            this->localAngularVelocity_ = 0;
204        }
205
206       this->destroyCheck();
207    }
208
209    bool Rocket::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* cs, btManifoldPoint& contactPoint)
210    {
211        return this->processCollision(otherObject, contactPoint, cs);
212    }
213
214    /**
215    @brief
216        Destroys the Rocket and stops the sound,
217    */
218    void Rocket::destroyObject(void)
219    {
220        if (GameMode::isMaster() && this->defSndWpnEngine_->isPlaying())
221            this->defSndWpnEngine_->stop();
222           
223           
224
225        // destruction of ParticleUniverse particle system
226        pu_rocketFire->stop();
227        this->detachOgreObject(pu_rocketFire);
228        pManager->destroyParticleSystem("pu_rocketFire", this->getScene()->getSceneManager());
229       
230       
231
232        this->BasicProjectile::destroyObject();
233    }
234
235    /**
236    @brief
237        Destroys the Rocket upon pressing "fire".
238    */
239    void Rocket::fired(unsigned int firemode)
240    {
241        this->destroyObject();
242    }
243
244    /**
245    @brief
246        The effects that are displayed, when the Rocket is destroyed.
247    */
248    void Rocket::destructionEffect()
249    {
250        ParticleSpawner *effect1, *effect2, *effect3, *effect4, *effect5;
251        if(this->getShooter())
252        {
253            effect1 = new ParticleSpawner(this->getShooter()->getContext());
254            effect2 = new ParticleSpawner(this->getShooter()->getContext());
255            effect3 = new ParticleSpawner(this->getShooter()->getContext());
256            effect4 = new ParticleSpawner(this->getShooter()->getContext());
257            effect5 = new ParticleSpawner(this->getShooter()->getContext());
258        }
259        else
260        {
261            effect1 = new ParticleSpawner(this->getContext());
262            effect2 = new ParticleSpawner(this->getContext());
263            effect3 = new ParticleSpawner(this->getContext());
264            effect4 = new ParticleSpawner(this->getContext());
265            effect5 = new ParticleSpawner(this->getContext());
266        }
267
268        effect1->setPosition(this->getPosition());
269        effect1->setOrientation(this->getOrientation());
270        effect1->setDestroyAfterLife(true);
271        effect1->setSource("orxonox/explosion_flash");
272        effect1->setLifetime(2.0f);
273
274        effect2->setPosition(this->getPosition());
275        effect2->setOrientation(this->getOrientation());
276        effect2->setDestroyAfterLife(true);
277        effect2->setSource("orxonox/explosion_flame");
278        effect2->setLifetime(3.0f);
279
280        effect3->setPosition(this->getPosition());
281        effect3->setOrientation(this->getOrientation());
282        effect3->setDestroyAfterLife(true);
283        effect3->setSource("orxonox/explosion_shockwave");
284        effect3->setLifetime(3.0f);
285
286        effect4->setPosition(this->getPosition());
287        effect4->setOrientation(this->getOrientation());
288        effect4->setDestroyAfterLife(true);
289        effect4->setSource("orxonox/explosion_sparks");
290        effect4->setLifetime(3.0f);
291
292        effect5->setPosition(this->getPosition());
293        effect5->setOrientation(this->getOrientation());
294        effect5->setDestroyAfterLife(true);
295        effect5->setSource("orxonox/explosion_streak1");
296        effect5->setLifetime(3.0f);
297    }
298
299    /**
300    @brief
301        Rotates the Rocket around the y-axis by the amount specified by the first component of the input 2-dim vector.
302    @param value
303        The vector determining the amount of the angular movement.
304    */
305    void Rocket::rotateYaw(const Vector2& value)
306    {
307        ControllableEntity::rotateYaw(value);
308
309        if( !this->isInMouseLook() )
310            this->localAngularVelocity_.y += value.x;
311    }
312
313    /**
314    @brief
315        Rotates the Rocket around the x-axis by the amount specified by the first component of the input 2-dim vector.
316    @param value
317        The vector determining the amount of the angular movement.
318    */
319    void Rocket::rotatePitch(const Vector2& value)
320    {
321        ControllableEntity::rotatePitch(value);
322
323        if( !this->isInMouseLook() )
324            this->localAngularVelocity_.x += value.x;
325    }
326
327    /**
328    @brief
329        Rotates the Rocket around the z-axis by the amount specified by the first component of the input 2-dim vector.
330    @param value
331        The vector determining the amount of the angular movement.
332    */
333    void Rocket::rotateRoll(const Vector2& value)
334    {
335        ControllableEntity::rotateRoll(value);
336
337        if( !this->isInMouseLook() )
338            this->localAngularVelocity_.z += value.x;
339    }
340
341}