Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particleEffectsHS15/src/modules/weapons/projectiles/MineProjectile.cc @ 10908

Last change on this file since 10908 was 10908, checked in by fvultier, 8 years ago

Mine damages pawns within range.

File size: 8.9 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 *      Jannis Holzer
24 *      Fabien Vultier
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30/**
31    @file MineProjectile.h
32    @brief Implementation of the MineProjectile class.
33*/
34
35#include "MineProjectile.h"
36
37#include "core/CoreIncludes.h"
38#include "graphics/Model.h"
39#include "core/command/Executor.h"
40#include "graphics/ParticleSpawner.h"
41#include "worldentities/pawns/Pawn.h"
42#include "core/EventIncludes.h"
43#include "sound/WorldSound.h"
44
45namespace orxonox
46{
47    RegisterClass(MineProjectile);
48
49    const float MineProjectile::collisionShapeRadius_ = 15.0f;
50    const float MineProjectile::damageRadius_ = 200.0f;
51    const float MineProjectile::triggerRadius_ = 100.0f;
52
53    MineProjectile::MineProjectile(Context* context) : MovableEntity(context), BasicProjectile()
54    {
55        RegisterObject(MineProjectile);
56
57        this->bAllowExplosion_ = false;
58        this->maxTimeUntilExplosion_ = 10.0f;
59        this->timeUntilActivation_ = 1.0f;
60
61        //Create movable entities
62        rings1_ = new MovableEntity(this->getContext());
63        this->attach(rings1_);
64        rings1_->setPosition(Vector3(0.0,0.0,0.0));
65        rings1_->setAngularVelocity(Vector3(0.0,5.0,0.0));
66
67        rings2_ = new MovableEntity(this->getContext());
68        this->attach(rings2_);
69        rings2_->setPosition(Vector3(0.0,0.0,0.0));
70        rings2_->setAngularVelocity(Vector3(0.0,0.0,5.0));
71
72        core_ = new MovableEntity(this->getContext());
73        this->attach(core_);
74        core_->setPosition(Vector3(0.0,0.0,0.0));
75        core_->setAngularVelocity(Vector3(2.5,2.5,0.0));
76
77        //Create Models
78        //Core
79        modelCore_ = new Model(this->getContext());
80        modelCore_->setMeshSource("Mine_Core.mesh");
81        modelCore_->setScale(15.0);
82        core_->attach(modelCore_);
83        modelCore_->setPosition(Vector3(0,0,0));
84
85        //Ring 1
86        modelRing1_ = new Model(this->getContext());
87        modelRing1_->setMeshSource("Mine_Ring.mesh");
88        modelRing1_->setScale(15.0);
89        rings1_->attach(modelRing1_);
90        modelRing1_->setPosition(Vector3(0,0,0));
91        modelRing1_->yaw(Degree(0));
92        //Ring 2
93        modelRing2_ = new Model(this->getContext());
94        modelRing2_->setMeshSource("Mine_Ring.mesh");
95        modelRing2_->setScale(15.0);
96        rings1_->attach(modelRing2_);
97        modelRing2_->setPosition(Vector3(0,0,0));
98        modelRing2_->yaw(Degree(180));
99        //Ring 3
100        modelRing3_ = new Model(this->getContext());
101        modelRing3_->setMeshSource("Mine_Ring.mesh");
102        modelRing3_->setScale(15.0);
103        rings2_->attach(modelRing3_);
104        modelRing3_->setPosition(Vector3(0,0,0));
105        modelRing3_->yaw(Degree(90));
106        //Ring 4
107        modelRing4_ = new Model(this->getContext());
108        modelRing4_->setMeshSource("Mine_Ring.mesh");
109        modelRing4_->setScale(15.0);
110        rings2_->attach(modelRing4_);
111        modelRing4_->setPosition(Vector3(0,0,0));
112        modelRing4_->yaw(Degree(270));
113
114        emitter_ = NULL;
115
116        if (GameMode::isMaster())
117        {
118            this->setMass(10.0f);
119            this->setFriction(100.0f);
120            this->enableCollisionCallback();
121            this->setCollisionResponse(false);
122            this->setCollisionType(Dynamic);
123
124            // Create a sphere collision shape and attach it to the projectile.
125            collisionShape_ = new SphereCollisionShape(this->getContext());
126            collisionShape_->setRadius(collisionShapeRadius_);
127            this->attachCollisionShape(collisionShape_);
128            collisionShape_->setPosition(Vector3(0,0,0));
129
130            // Create a distance trigger and attach it to the projectile.
131            distanceTrigger_ = new DistanceTrigger(this->getContext());
132            this->attach(distanceTrigger_);
133            distanceTrigger_->setPosition(Vector3(0,0,0));
134            distanceTrigger_->setDistance(triggerRadius_);
135            distanceTrigger_->addTarget("Pawn");
136            distanceTrigger_->setStayActive(false);
137
138            this->addEventSource(distanceTrigger_, "explode");
139        }
140    }
141
142    MineProjectile::~MineProjectile()
143    {
144        if (this->isInitialized())
145        {
146            modelCore_->destroy();
147            modelRing1_->destroy();
148            modelRing2_->destroy();
149            modelRing3_->destroy();
150            modelRing4_->destroy();
151
152            distanceTrigger_->destroy();
153
154            if (emitter_)
155            {
156                emitter_->destroy();
157            }
158        }
159    }
160
161    /**
162    @brief
163        TODO
164    */
165    void MineProjectile::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
166    {
167        SUPER(MineProjectile, XMLEventPort, xmlelement, mode);
168        XMLPortEventState(MineProjectile, BaseObject, "explode", explode, xmlelement, mode);
169    }
170
171    /**
172    @brief
173        TODO
174    */
175    void MineProjectile::setMaxTimeUntilExplosion(float maxTimeUntilExplosion)
176    {
177        if (maxTimeUntilExplosion >= 0)
178        {
179            this->maxTimeUntilExplosion_ = maxTimeUntilExplosion;
180            if (GameMode::isMaster())
181            {
182                this->explodeTimer_.setTimer(this->maxTimeUntilExplosion_, false, createExecutor(createFunctor(&MineProjectile::explode, this)));
183            }
184        }
185        else
186        {
187            this->maxTimeUntilExplosion_ = 0;
188        }
189    }
190
191    /**
192    @brief
193        TODO
194    */
195    void MineProjectile::setTimeUntilActivation(float timeUntilActivation)
196    {
197        timeUntilActivation_ = timeUntilActivation;
198
199        if (GameMode::isMaster())
200        {
201            this->activationTimer_.setTimer(this->timeUntilActivation_, false, createExecutor(createFunctor(&MineProjectile::allowExplosion, this)));
202        }
203    }
204
205    /**
206    @brief
207        TODO
208    */
209    void MineProjectile::explode()
210    {
211        if (bAllowExplosion_)
212        {
213            if (GameMode::isMaster())
214            {
215                // Damage all pawns within the damage radius
216                for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
217                {
218                    Vector3 distanceVector = it->getWorldPosition()-this->getWorldPosition();
219                    if(distanceVector.length()< damageRadius_)
220                    {
221                        it->hit(this->getShooter(), it->getWorldPosition(), NULL, this->getDamage(), this->getHealthDamage(), this->getShieldDamage());
222                    }
223                }
224            }
225
226            this->destructionEffect();
227            this->destroyLater();
228        }
229    }
230
231    /**
232    @brief
233        TODO
234    */
235    void MineProjectile::allowExplosion()
236    {
237        // Allow explosion
238        bAllowExplosion_ = true;
239        // Add particle effect
240        emitter_ = new ParticleEmitter(this->getContext());
241        this->attach(emitter_);
242        emitter_->setOrientation(this->getOrientation());
243        emitter_->setSource("Orxonox/mineparticle"); 
244    }
245
246    /**
247    @brief
248        TODO
249    */
250    void MineProjectile::destructionEffect()
251    {
252        ParticleSpawner *effect1, *effect2, *effect3;
253       
254        effect1 = new ParticleSpawner(this->getContext());
255        effect2 = new ParticleSpawner(this->getContext());
256        effect3 = new ParticleSpawner(this->getContext());
257
258        effect1->setPosition(this->getPosition());
259        effect1->setOrientation(this->getOrientation());
260        effect1->setDestroyAfterLife(true);
261        effect1->setSource("Orxonox/MineExpl");
262        effect1->setLifetime(2.5f);
263
264        effect2->setPosition(this->getPosition());
265        effect2->setOrientation(this->getOrientation());
266        effect2->setDestroyAfterLife(true);
267        effect2->setSource("Orxonox/MineExpl1");
268        effect2->setLifetime(2.5f);
269
270        effect3->setPosition(this->getPosition());
271        effect3->setOrientation(this->getOrientation());
272        effect3->setDestroyAfterLife(true);
273        effect3->setSource("Orxonox/MineExpl2");
274        effect3->setLifetime(2.5f);
275
276        // Explosion sound effect.
277        WeakPtr<WorldSound> explosionSound_ = new WorldSound(getContext());
278        explosionSound_->setSource("sounds/GravityFieldExplosion.ogg");
279        explosionSound_->setVolume(1.0);
280        explosionSound_->play();
281    }
282}
Note: See TracBrowser for help on using the repository browser.