Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/modules/weapons/projectiles/Rocket.cc @ 6378

Last change on this file since 6378 was 6378, checked in by rgrieder, 14 years ago

Added missing includes (revealed only with PCH and compilations).

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