Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles/src/orxonox/objects/worldentities/BigExplosion.cc @ 3054

Last change on this file since 3054 was 3054, checked in by decapitb, 15 years ago

everything

File size: 14.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 *      Benjamin de Capitani
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "BigExplosion.h"
31#include "MovableEntity.h"
32#include "Model.h"
33
34#include <OgreParticleSystem.h>
35#include <OgreSceneNode.h>
36
37#include "core/Core.h"
38#include "core/CoreIncludes.h"
39#include "core/Executor.h"
40#include "objects/Scene.h"
41#include "tools/ParticleInterface.h"
42#include "objects/worldentities/ParticleSpawner.h"
43#include "util/Exception.h"
44
45namespace orxonox
46{
47    CreateFactory(BigExplosion);
48
49    BigExplosion::BigExplosion(BaseObject* creator) : MovableEntity(creator)
50    {
51        RegisterObject(BigExplosion);
52
53        if ( Core::showsGraphics() && ( !this->getScene() || !this->getScene()->getSceneManager() ) )
54            ThrowException(AbortLoading, "Can't create BigExplosion, no scene or no scene manager given.");
55
56        this->bStop_ = false;
57        this->LOD_ = LODParticle::normal;
58
59        if ( Core::showsGraphics() )
60        {
61            try
62            {
63                this->init();
64            }
65            catch (...)
66            {
67                COUT(1) << "Error: Couln't load particle effect in BigExplosion." << std::endl;
68                this->initZero();
69            }
70        }
71        else
72        {
73            this->initZero();
74        }
75
76        if (Core::isMaster())
77        {
78            Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
79            velocity.normalise();
80            velocity *= rnd(20, 30);
81            this->setVelocity(velocity);
82
83            this->destroyTimer_.setTimer(rnd(4, 7), false, this, createExecutor(createFunctor(&BigExplosion::stop)));
84        }
85        this->registerVariables();
86    }
87
88    void BigExplosion::init()
89    {
90        Identifier* idDE1 = Class(MovableEntity);
91        BaseObject* oDE1 = idDE1->fabricate(this);
92        this->debrisEntity1_ = dynamic_cast<MovableEntity*>(oDE1);
93
94        Identifier* idDE2 = Class(MovableEntity);
95        BaseObject* oDE2 = idDE2->fabricate(this);
96        this->debrisEntity2_ = dynamic_cast<MovableEntity*>(oDE2);
97
98        Identifier* idDE3 = Class(MovableEntity);
99        BaseObject* oDE3 = idDE3 ->fabricate(this);
100        this->debrisEntity3_ = dynamic_cast<MovableEntity*>(oDE3);
101
102        Identifier* idDE4 = Class(MovableEntity);
103        BaseObject* oDE4 = idDE4->fabricate(this);
104        this->debrisEntity4_ = dynamic_cast<MovableEntity*>(oDE4);
105
106        Identifier* idD1 = Class(Model);
107        BaseObject* oD1 = idD1->fabricate(this);
108        this->debris1_ = dynamic_cast<Model*>(oD1);
109
110        Identifier* idD2 = Class(Model);
111        BaseObject* oD2 = idD2->fabricate(this);
112        this->debris2_ = dynamic_cast<Model*>(oD2);
113
114        Identifier* idD3 = Class(Model);
115        BaseObject* oD3 = idD3->fabricate(this);
116        this->debris3_ = dynamic_cast<Model*>(oD3);
117
118        Identifier* idD4 = Class(Model);
119        BaseObject* oD4 = idD4->fabricate(this);
120        this->debris4_ = dynamic_cast<Model*>(oD4);
121
122        Identifier* id6 = Class(StaticEntity);
123        BaseObject* object4 = id6->fabricate(this);
124        this->explosion_ = dynamic_cast<StaticEntity*>(object4);
125
126        this->debrisSmoke1_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
127        this->debrisSmoke2_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
128        this->debrisSmoke3_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
129        this->debrisSmoke4_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
130
131        this->debrisFire1_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
132        this->debrisFire2_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
133        this->debrisFire3_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
134        this->debrisFire4_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
135
136        this->debris1_->attachOgreObject(this->debrisSmoke1_->getParticleSystem());
137        this->debris1_->attachOgreObject(this->debrisFire1_->getParticleSystem());
138        this->debris2_->attachOgreObject(this->debrisSmoke2_->getParticleSystem());
139        this->debris2_->attachOgreObject(this->debrisFire2_->getParticleSystem());
140        this->debris3_->attachOgreObject(this->debrisSmoke3_->getParticleSystem());
141        this->debris3_->attachOgreObject(this->debrisFire3_->getParticleSystem());
142        this->debris4_->attachOgreObject(this->debrisSmoke4_->getParticleSystem());
143        this->debris4_->attachOgreObject(this->debrisFire4_->getParticleSystem());
144
145        this->debris1_->setMeshSource("CockpitDebris.mesh");
146        this->debris2_->setMeshSource("WingDebris1.mesh");
147        this->debris3_->setMeshSource("BodyDebris1.mesh");
148        this->debris4_->setMeshSource("WingDebris1.mesh");
149
150        this->debrisEntity1_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
151        this->debrisEntity1_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
152        this->debrisEntity1_->setScale(4);
153
154        this->debrisEntity2_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
155        this->debrisEntity2_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
156        this->debrisEntity2_->setScale(4);
157
158        this->debrisEntity3_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
159        this->debrisEntity3_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
160        this->debrisEntity3_->setScale(4);
161
162        this->debrisEntity4_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
163        this->debrisEntity4_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
164        this->debrisEntity4_->setScale(4);
165
166        this->debrisEntity1_->attach(debris1_);
167        this->debrisEntity2_->attach(debris2_);
168        this->debrisEntity3_->attach(debris3_);
169        this->debrisEntity4_->attach(debris4_);
170
171        ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
172        effect->setDestroyAfterLife(true);
173        effect->setSource("Orxonox/explosion2b");
174        effect->setLifetime(4.0f);
175
176        ParticleSpawner* effect2 = new ParticleSpawner(this->getCreator());
177        effect2->setDestroyAfterLife(true);
178        effect2->setSource("Orxonox/smoke6");
179        effect2->setLifetime(4.0f);
180
181        this->explosion_->attach(effect);
182        this->explosion_->attach(effect2);
183
184        this->attach(explosion_);
185        this->attach(debrisEntity1_);
186        this->attach(debrisEntity2_);
187        this->attach(debrisEntity3_);
188        this->attach(debrisEntity4_);
189
190
191        for(int i=0;i<50;i++)
192        {
193            Identifier* idf1 = Class(Model);
194            BaseObject* obj1 = idf1->fabricate(this);
195            Model* part1 = dynamic_cast<Model*>(obj1);
196
197
198            Identifier* idf2 = Class(Model);
199            BaseObject* obj2 = idf2->fabricate(this);
200            Model* part2 = dynamic_cast<Model*>(obj2);
201
202            Identifier* idf3 = Class(MovableEntity);
203            BaseObject* obj3 = idf3->fabricate(this);
204            MovableEntity* partEntity1 = dynamic_cast<MovableEntity*>(obj3);
205
206            Identifier* idf4 = Class(MovableEntity);
207            BaseObject* obj4 = idf4->fabricate(this);
208            MovableEntity* partEntity2 = dynamic_cast<MovableEntity*>(obj4);
209
210            partEntity1->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10,100));
211            partEntity1->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
212            partEntity1->setScale(2);
213
214            partEntity2->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10, 100));
215            partEntity2->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(1000).valueRadians());
216            partEntity2->setScale(2);
217
218            part1->setMeshSource("SmallPart1.mesh");
219            part2->setMeshSource("SmallPart2.mesh");
220
221            partEntity1->attach(part1);
222            partEntity2->attach(part2);
223
224            this->attach(partEntity1);
225            this->attach(partEntity2);
226        }
227    }
228
229    void BigExplosion::initZero()
230    {
231        this->debrisFire1_ = 0;
232        this->debrisFire2_ = 0;
233        this->debrisFire3_ = 0;
234        this->debrisFire4_ = 0;
235
236        this->debrisSmoke1_ = 0;
237        this->debrisSmoke2_ = 0;
238        this->debrisSmoke3_ = 0;
239        this->debrisSmoke4_ = 0;
240
241        this->explosionSmoke_=0;
242        this->explosionFire_=0;
243    }
244
245    BigExplosion::~BigExplosion()
246    {
247        if (this->isInitialized())
248        {
249            if (this->debrisFire1_)
250            {
251                this->debris1_->detachOgreObject(this->debrisFire1_->getParticleSystem());
252                delete this->debrisFire1_;
253            }
254            if (this->debrisSmoke1_)
255            {
256                this->debris1_->detachOgreObject(this->debrisSmoke1_->getParticleSystem());
257                delete this->debrisSmoke1_;
258            }
259
260            if (this->debrisFire2_)
261            {
262                this->debris2_->detachOgreObject(this->debrisFire2_->getParticleSystem());
263                delete this->debrisFire2_;
264            }
265            if (this->debrisSmoke2_)
266            {
267                this->debris2_->detachOgreObject(this->debrisSmoke2_->getParticleSystem());
268                delete this->debrisSmoke2_;
269            }
270
271            if (this->debrisFire3_)
272            {
273                this->debris3_->detachOgreObject(this->debrisFire3_->getParticleSystem());
274                delete this->debrisFire3_;
275            }
276            if (this->debrisSmoke3_)
277            {
278                this->debris3_->detachOgreObject(this->debrisSmoke3_->getParticleSystem());
279                delete this->debrisSmoke3_;
280            }
281
282            if (this->debrisFire4_)
283            {
284                this->debris4_->detachOgreObject(this->debrisFire4_->getParticleSystem());
285                delete this->debrisFire4_;
286            }
287            if (this->debrisSmoke4_)
288            {
289                this->debris4_->detachOgreObject(this->debrisSmoke4_->getParticleSystem());
290                delete this->debrisSmoke4_;
291            }
292        }
293    }
294
295    void BigExplosion::registerVariables()
296    {
297        registerVariable((int&)(this->LOD_), variableDirection::toclient, new NetworkCallback<BigExplosion>(this, &BigExplosion::LODchanged));
298        registerVariable(this->bStop_,       variableDirection::toclient, new NetworkCallback<BigExplosion>(this, &BigExplosion::checkStop));
299    }
300
301    void BigExplosion::LODchanged()
302    {
303        if (this->debrisFire1_)
304            this->debrisFire1_->setDetailLevel(this->LOD_);
305        if (this->debrisSmoke1_)
306            this->debrisSmoke1_->setDetailLevel(this->LOD_);
307
308        if (this->debrisFire2_)
309            this->debrisFire2_->setDetailLevel(this->LOD_);
310        if (this->debrisSmoke2_)
311            this->debrisSmoke2_->setDetailLevel(this->LOD_);
312
313        if (this->debrisFire3_)
314            this->debrisFire3_->setDetailLevel(this->LOD_);
315        if (this->debrisSmoke3_)
316            this->debrisSmoke3_->setDetailLevel(this->LOD_);
317
318        if (this->debrisFire4_)
319            this->debrisFire4_->setDetailLevel(this->LOD_);
320        if (this->debrisSmoke4_)
321            this->debrisSmoke4_->setDetailLevel(this->LOD_);
322    }
323
324    void BigExplosion::checkStop()
325    {
326        if (this->bStop_)
327            this->stop();
328    }
329
330    void BigExplosion::stop()
331    {
332        if (this->debrisFire1_)
333            this->debrisFire1_->setEnabled(false);
334        if (this->debrisSmoke1_)
335            this->debrisSmoke1_->setEnabled(false);
336
337        if (this->debrisFire2_)
338            this->debrisFire2_->setEnabled(false);
339        if (this->debrisSmoke2_)
340            this->debrisSmoke2_->setEnabled(false);
341
342        if (this->debrisFire3_)
343            this->debrisFire3_->setEnabled(false);
344        if (this->debrisSmoke3_)
345            this->debrisSmoke3_->setEnabled(false);
346
347        if (this->debrisFire4_)
348            this->debrisFire4_->setEnabled(false);
349        if (this->debrisSmoke4_)
350            this->debrisSmoke4_->setEnabled(false);
351
352        if (Core::isMaster())
353        {
354            this->bStop_ = true;
355            this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&BigExplosion::destroy)));
356        }
357    }
358
359    void BigExplosion::destroy()
360    {
361        delete this;
362    }
363
364     void BigExplosion::tick(float dt)
365    {
366
367
368//        static const unsigned int CHANGES_PER_SECOND = 5;
369//
370//        if (Core::isMaster() && rnd() < dt*CHANGES_PER_SECOND)
371//        {
372//            Vector3 velocity = this->object2_->getVelocity();
373//            velocity /= 1.5;
374//            this->object2_->setVelocity(velocity);
375//        }
376
377        SUPER(BigExplosion, tick, dt);
378    }
379}
Note: See TracBrowser for help on using the repository browser.