Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/BigExplosion.cc @ 3089

Last change on this file since 3089 was 3087, checked in by landauf, 15 years ago

merged particles branch back to trunk

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