Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/rocket2/src/modules/weapons/projectiles/Rocket.cc @ 6969

Last change on this file since 6969 was 6969, checked in by gnadler, 14 years ago
  • Property svn:eol-style set to native
File size: 9.2 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.7f);
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->defSndWpnEngine_->setVolume(100);
90            this->attach(defSndWpnEngine_);
91
92            this->defSndWpnLaunch_ = new WorldSound(this);
93            this->defSndWpnLaunch_->setLooping(false);
94            this->defSndWpnLaunch_->setSource("sounds/Rocket_launch.ogg");
95            this->defSndWpnLaunch_->setVolume(100);
96            this->attach(defSndWpnLaunch_);
97        }
98        else
99        {
100            this->defSndWpnEngine_ = 0;
101            this->defSndWpnLaunch_ = 0;
102        }
103
104        CameraPosition* camPosition = new CameraPosition(this);
105        camPosition->setPosition(0,4,15);
106        camPosition->setAllowMouseLook(true);
107        this->addCameraPosition(camPosition);
108    }
109
110    /**
111    @brief
112        Destructor. Destroys controller, if present and kills sounds, if playing.
113    */
114    Rocket::~Rocket()
115    {
116        if(this->isInitialized())
117        {
118            if (GameMode::isMaster() && this->player_)
119                this->player_->stopTemporaryControl();
120
121            if ( this->defSndWpnEngine_ )
122                this->defSndWpnEngine_->destroy();
123
124            if ( this->defSndWpnLaunch_ )
125                this->defSndWpnLaunch_->destroy();
126        }
127    }
128
129    /**
130    @brief
131        Method for creating a Rocket through XML.
132    */
133    void Rocket::XMLPort(Element& xmlelement, XMLPort::Mode mode)
134    {
135        // this calls the XMLPort function of the parent class
136        SUPER(Rocket, XMLPort, xmlelement, mode);
137    }
138
139    void Rocket::setOwner(Pawn* owner)
140    {
141        this->owner_ = owner;
142        this->originalControllableEntity_ = this->owner_->getPlayer()->getControllableEntity();
143        this->player_ = this->owner_->getPlayer();
144        this->owner_->getPlayer()->startTemporaryControl(this);
145
146        if( GameMode::isMaster() )
147        {
148            this->defSndWpnEngine_->play();
149            this->defSndWpnLaunch_->play();
150        }
151    }
152
153    /**
154    @brief
155        Defines which actions the Rocket has to take in each tick.
156    @param dt
157        The length of the tick.
158    */
159    void Rocket::tick(float dt)
160    {
161        SUPER(Rocket, tick, dt);
162
163        if( this->hasLocalController() )
164        {
165            this->setAngularVelocity(this->getOrientation() * this->localAngularVelocity_);
166            this->setVelocity( this->getOrientation()*WorldEntity::FRONT*this->getVelocity().length() );
167            this->localAngularVelocity_ = 0;
168
169            if( this->bDestroy_ )
170                this->destroy();
171        }
172    }
173
174    bool Rocket::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
175    {
176        if (!this->bDestroy_ && GameMode::isMaster())
177        {
178            if (otherObject == this->owner_)
179                return false;
180
181            this->bDestroy_ = true;
182
183            if (this->owner_)
184            {
185                {
186                    ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator());
187                    effect->setPosition(this->getPosition());
188                    effect->setOrientation(this->getOrientation());
189                    effect->setDestroyAfterLife(true);
190                    effect->setSource("Orxonox/explosion4");
191                    effect->setLifetime(2.0f);
192                }
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/smoke4");
200                    effect->setLifetime(3.0f);
201                }
202            }
203
204            Pawn* victim = orxonox_cast<Pawn*>(otherObject);
205            if (victim)
206                victim->hit(this->owner_, contactPoint, this->damage_);
207//             this->destroy();
208        }
209        return false;
210    }
211
212    void Rocket::destroyObject()
213    {
214        if (GameMode::isMaster())
215        {
216            if(this->defSndWpnEngine_->isPlaying())
217            {
218                this->defSndWpnEngine_->stop();
219            }
220            this->destroy();
221        }
222    }
223
224    void Rocket::fired(unsigned int firemode)
225    {
226        if (this->owner_)
227        {
228            {
229                ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator());
230                effect->setPosition(this->getPosition());
231                effect->setOrientation(this->getOrientation());
232                effect->setDestroyAfterLife(true);
233                effect->setSource("Orxonox/explosion4");
234                effect->setLifetime(2.0f);
235            }
236
237            {
238                ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator());
239                effect->setPosition(this->getPosition());
240                effect->setOrientation(this->getOrientation());
241                effect->setDestroyAfterLife(true);
242                effect->setSource("Orxonox/smoke4");
243                effect->setLifetime(3.0f);
244            }
245            this->destroy();
246        }
247    }
248
249    /**
250    @brief
251        Rotates the Rocket around the y-axis by the amount specified by the first component of the input 2-dim vector.
252    @param value
253        The vector determining the amount of the angular movement.
254    */
255    void Rocket::rotateYaw(const Vector2& value)
256    {
257        ControllableEntity::rotateYaw(value);
258
259        if( !this->isInMouseLook() )
260            this->localAngularVelocity_.y += value.x;
261    }
262
263    /**
264    @brief
265        Rotates the Rocket around the x-axis by the amount specified by the first component of the input 2-dim vector.
266    @param value
267        The vector determining the amount of the angular movement.
268    */
269    void Rocket::rotatePitch(const Vector2& value)
270    {
271        ControllableEntity::rotatePitch(value);
272
273        if( !this->isInMouseLook() )
274            this->localAngularVelocity_.x += value.x;
275    }
276
277    /**
278    @brief
279        Rotates the Rocket around the z-axis by the amount specified by the first component of the input 2-dim vector.
280    @param value
281        The vector determining the amount of the angular movement.
282    */
283    void Rocket::rotateRoll(const Vector2& value)
284    {
285        ControllableEntity::rotateRoll(value);
286
287        if( !this->isInMouseLook() )
288            this->localAngularVelocity_.z += value.x;
289    }
290
291}
Note: See TracBrowser for help on using the repository browser.