Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/weapons/projectiles/Rocket.cc @ 8124

Last change on this file since 8124 was 7848, checked in by landauf, 14 years ago

fixed "sound volume out of range" warning when firing a rocked. max volume is 1.0f.

  • Property svn:eol-style set to native
File size: 9.5 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 *      Oliver Scheuss
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Rocket.h"
30
31#include <BulletDynamics/Dynamics/btRigidBody.h>
32
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "worldentities/CameraPosition.h"
36#include "worldentities/pawns/Pawn.h"
37#include "graphics/ParticleSpawner.h"
38#include "graphics/Model.h"
39#include "objects/collisionshapes/ConeCollisionShape.h"
40#include "infos/PlayerInfo.h"
41#include "controllers/Controller.h"
42#include "sound/WorldSound.h"
43#include "Scene.h"
44
45namespace orxonox
46{
47    CreateFactory(Rocket);
48    // create the factory for the Rocket
49
50    /**
51    @brief
52        Constructor. Registers the object and initializes some default values.
53    */
54    Rocket::Rocket(BaseObject* creator) : ControllableEntity(creator)
55    {
56        RegisterObject(Rocket);// - register the Rocket class to the core
57
58        this->localAngularVelocity_ = 0;
59        this->bDestroy_ = false;
60        this->lifetime_ = 100;
61
62        if (GameMode::isMaster())
63        {
64            this->setCollisionType(WorldEntity::Kinematic);
65            this->setVelocity(0,0,-100);
66
67            Model* model = new Model(this);
68            model->setMeshSource("rocket.mesh");
69            model->scale(0.7f);
70            this->attach(model);
71            ParticleEmitter* fire = new ParticleEmitter(this);
72            this->attach(fire);
73            fire->setOrientation(this->getOrientation());
74            fire->setSource("Orxonox/rocketfire");
75
76            this->enableCollisionCallback();
77            this->setCollisionResponse(false);
78            this->setCollisionType(Kinematic);
79
80            ConeCollisionShape* collisionShape = new ConeCollisionShape(this);
81            collisionShape->setRadius(3);
82            collisionShape->setHeight(500);
83            this->attachCollisionShape(collisionShape);
84
85            this->destroyTimer_.setTimer(this->lifetime_, false, createExecutor(createFunctor(&Rocket::destroyObject, this)));
86
87            this->defSndWpnEngine_ = new WorldSound(this);
88            this->defSndWpnEngine_->setLooping(true);
89            this->defSndWpnEngine_->setSource("sounds/Rocket_engine.ogg");
90            this->defSndWpnEngine_->setVolume(1.0f);
91            this->attach(defSndWpnEngine_);
92
93            this->defSndWpnLaunch_ = new WorldSound(this);
94            this->defSndWpnLaunch_->setLooping(false);
95            this->defSndWpnLaunch_->setSource("sounds/Rocket_launch.ogg");
96            this->defSndWpnLaunch_->setVolume(1.0f);
97            this->attach(defSndWpnLaunch_);
98        }
99        else
100        {
101            this->defSndWpnEngine_ = 0;
102            this->defSndWpnLaunch_ = 0;
103        }
104
105        CameraPosition* camPosition = new CameraPosition(this);
106        camPosition->setPosition(0,4,15);
107        camPosition->setAllowMouseLook(true);
108        this->addCameraPosition(camPosition);
109    }
110
111    /**
112    @brief
113        Destructor. Destroys controller, if present and kills sounds, if playing.
114    */
115    Rocket::~Rocket()
116    {
117        if(this->isInitialized())
118        {
119            if (GameMode::isMaster())
120            {
121                this->destructionEffect();
122
123                if (this->getPlayer() && this->getController())
124                    this->player_->stopTemporaryControl();
125            }
126
127            if ( this->defSndWpnEngine_ )
128                this->defSndWpnEngine_->destroy();
129
130            if ( this->defSndWpnLaunch_ )
131                this->defSndWpnLaunch_->destroy();
132        }
133    }
134
135    /**
136    @brief
137        Method for creating a Rocket through XML.
138    */
139    void Rocket::XMLPort(Element& xmlelement, XMLPort::Mode mode)
140    {
141        // this calls the XMLPort function of the parent class
142        SUPER(Rocket, XMLPort, xmlelement, mode);
143    }
144
145    void Rocket::setOwner(Pawn* owner)
146    {
147        this->owner_ = owner;
148        this->player_ = this->owner_->getPlayer();
149        this->owner_->getPlayer()->startTemporaryControl(this);
150
151        if( GameMode::isMaster() )
152        {
153            this->defSndWpnEngine_->play();
154            this->defSndWpnLaunch_->play();
155        }
156    }
157
158    /**
159    @brief
160        Defines which actions the Rocket has to take in each tick.
161    @param dt
162        The length of the tick.
163    */
164    void Rocket::tick(float dt)
165    {
166        SUPER(Rocket, tick, dt);
167
168        if( this->hasLocalController() )
169        {
170            this->setAngularVelocity(this->getOrientation() * this->localAngularVelocity_);
171            this->setVelocity( this->getOrientation()*WorldEntity::FRONT*this->getVelocity().length() );
172            this->localAngularVelocity_ = 0;
173        }
174
175        if( GameMode::isMaster() )
176        {
177            if( this->bDestroy_ )
178                this->destroy();
179
180        }
181    }
182
183    bool Rocket::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
184    {
185        if (!this->bDestroy_ && GameMode::isMaster())
186        {
187            if (otherObject == this->owner_)
188                return false;
189
190            this->bDestroy_ = true;
191
192            if (this->owner_)
193            {
194                {
195                    ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator());
196                    effect->setPosition(this->getPosition());
197                    effect->setOrientation(this->getOrientation());
198                    effect->setDestroyAfterLife(true);
199                    effect->setSource("Orxonox/explosion4");
200                    effect->setLifetime(2.0f);
201                }
202
203                {
204                    ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator());
205                    effect->setPosition(this->getPosition());
206                    effect->setOrientation(this->getOrientation());
207                    effect->setDestroyAfterLife(true);
208                    effect->setSource("Orxonox/smoke4");
209                    effect->setLifetime(3.0f);
210                }
211            }
212
213            Pawn* victim = orxonox_cast<Pawn*>(otherObject);
214            if (victim)
215                victim->hit(this->owner_, contactPoint, this->damage_);
216//             this->destroy();
217        }
218        return false;
219    }
220
221    void Rocket::destroyObject()
222    {
223        if (GameMode::isMaster())
224        {
225            if(this->defSndWpnEngine_->isPlaying())
226            {
227                this->defSndWpnEngine_->stop();
228            }
229            this->destroy();
230        }
231    }
232
233    void Rocket::fired(unsigned int firemode)
234    {
235//         if (this->owner_)
236//         {
237            this->destroy();
238//         }
239    }
240
241    void Rocket::destructionEffect()
242    {
243        ParticleSpawner *effect1, *effect2;
244        if( this->owner_ )
245        {
246            effect1 = new ParticleSpawner(this->owner_->getCreator());
247            effect2 = new ParticleSpawner(this->owner_->getCreator());
248        }
249        else
250        {
251            effect1 = new ParticleSpawner(static_cast<BaseObject*>(this->getScene().get()));
252            effect2 = new ParticleSpawner(static_cast<BaseObject*>(this->getScene().get()));
253        }
254
255        effect1->setPosition(this->getPosition());
256        effect1->setOrientation(this->getOrientation());
257        effect1->setDestroyAfterLife(true);
258        effect1->setSource("Orxonox/explosion4");
259        effect1->setLifetime(2.0f);
260
261        effect2->setPosition(this->getPosition());
262        effect2->setOrientation(this->getOrientation());
263        effect2->setDestroyAfterLife(true);
264        effect2->setSource("Orxonox/smoke4");
265        effect2->setLifetime(3.0f);
266    }
267
268    /**
269    @brief
270        Rotates the Rocket around the y-axis by the amount specified by the first component of the input 2-dim vector.
271    @param value
272        The vector determining the amount of the angular movement.
273    */
274    void Rocket::rotateYaw(const Vector2& value)
275    {
276        ControllableEntity::rotateYaw(value);
277
278        if( !this->isInMouseLook() )
279            this->localAngularVelocity_.y += value.x;
280    }
281
282    /**
283    @brief
284        Rotates the Rocket around the x-axis by the amount specified by the first component of the input 2-dim vector.
285    @param value
286        The vector determining the amount of the angular movement.
287    */
288    void Rocket::rotatePitch(const Vector2& value)
289    {
290        ControllableEntity::rotatePitch(value);
291
292        if( !this->isInMouseLook() )
293            this->localAngularVelocity_.x += value.x;
294    }
295
296    /**
297    @brief
298        Rotates the Rocket around the z-axis by the amount specified by the first component of the input 2-dim vector.
299    @param value
300        The vector determining the amount of the angular movement.
301    */
302    void Rocket::rotateRoll(const Vector2& value)
303    {
304        ControllableEntity::rotateRoll(value);
305
306        if( !this->isInMouseLook() )
307            this->localAngularVelocity_.z += value.x;
308    }
309
310}
Note: See TracBrowser for help on using the repository browser.