Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/modules/weapons/projectiles/Rocket.cc @ 8703

Last change on this file since 8703 was 8701, checked in by jo, 14 years ago

First successful attempt, to make bots shoot rockets. Unfortunately they're shooting the wrong ones.

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