Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/modules/weapons/projectiles/Rocket.cc @ 8852

Last change on this file since 8852 was 8733, checked in by jo, 13 years ago

Further narrowing down. The bug is triggered, when a bot calls fire(3) - whenever a bot launches a rocket.

  • Property svn:eol-style set to native
File size: 8.4 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 *      simonmie
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), BasicProjectile()
55    {
56        RegisterObject(Rocket);// - register the Rocket class to the core
57
58        this->localAngularVelocity_ = 0;
59        this->lifetime_ = 100;
60        this->bIsRocket_= true;
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            this->bIsRocket_= false;
120            if (GameMode::isMaster())
121            {
122                this->destructionEffect();
123
124                if (this->getPlayer() && this->getController())
125                    this->player_->stopTemporaryControl();
126            }
127
128            if ( this->defSndWpnEngine_ )
129                this->defSndWpnEngine_->destroy();
130
131            if ( this->defSndWpnLaunch_ )
132                this->defSndWpnLaunch_->destroy();
133        }
134    }
135
136    /**
137    @brief
138        Method for creating a Rocket through XML.
139    */
140    void Rocket::XMLPort(Element& xmlelement, XMLPort::Mode mode)
141    {
142        // this calls the XMLPort function of the parent class
143        SUPER(Rocket, XMLPort, xmlelement, mode);
144    }
145
146    void Rocket::setOwner(Pawn* owner)
147    {
148        this->owner_ = owner;
149        this->player_ = this->getOwner()->getPlayer();
150        this->getOwner()->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
176        if( GameMode::isMaster() )
177        {
178            if( this->getBDestroy() )
179                this->destroy();
180
181        }
182    }
183
184    /* Calls the collidesAgainst function of BasicProjectile
185     */
186    bool Rocket::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
187    {
188        return BasicProjectile::basicCollidesAgainst(otherObject,contactPoint,this->getOwner(),this);
189    }
190
191    void Rocket::destroyObject()
192    {
193        if (GameMode::isMaster())
194        {
195            if(this->defSndWpnEngine_->isPlaying())
196            {
197                this->defSndWpnEngine_->stop();
198            }
199            this->destroy();
200        }
201    }
202
203    void Rocket::fired(unsigned int firemode)
204    {
205        this->destroy();
206    }
207
208    void Rocket::destructionEffect()
209    {
210        ParticleSpawner *effect1, *effect2;
211        if( this->getOwner() )
212        {
213            effect1 = new ParticleSpawner(this->getOwner()->getCreator());
214            effect2 = new ParticleSpawner(this->getOwner()->getCreator());
215        }
216        else
217        {
218            effect1 = new ParticleSpawner(static_cast<BaseObject*>(this->getScene().get()));
219            effect2 = new ParticleSpawner(static_cast<BaseObject*>(this->getScene().get()));
220        }
221
222        effect1->setPosition(this->getPosition());
223        effect1->setOrientation(this->getOrientation());
224        effect1->setDestroyAfterLife(true);
225        effect1->setSource("Orxonox/explosion4");
226        effect1->setLifetime(2.0f);
227
228        effect2->setPosition(this->getPosition());
229        effect2->setOrientation(this->getOrientation());
230        effect2->setDestroyAfterLife(true);
231        effect2->setSource("Orxonox/smoke4");
232        effect2->setLifetime(3.0f);
233    }
234
235    /**
236    @brief
237        Rotates the Rocket around the y-axis by the amount specified by the first component of the input 2-dim vector.
238    @param value
239        The vector determining the amount of the angular movement.
240    */
241    void Rocket::rotateYaw(const Vector2& value)
242    {
243        ControllableEntity::rotateYaw(value);
244
245        if( !this->isInMouseLook() )
246            this->localAngularVelocity_.y += value.x;
247    }
248
249    /**
250    @brief
251        Rotates the Rocket around the x-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::rotatePitch(const Vector2& value)
256    {
257        ControllableEntity::rotatePitch(value);
258
259        if( !this->isInMouseLook() )
260            this->localAngularVelocity_.x += value.x;
261    }
262
263    /**
264    @brief
265        Rotates the Rocket around the z-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::rotateRoll(const Vector2& value)
270    {
271        ControllableEntity::rotateRoll(value);
272
273        if( !this->isInMouseLook() )
274            this->localAngularVelocity_.z += value.x;
275    }
276
277}
Note: See TracBrowser for help on using the repository browser.