Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/BigExplosion.cc @ 3186

Last change on this file since 3186 was 3186, checked in by rgrieder, 15 years ago

Last part of the cleanup: world entities.

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