Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/invaders/src/orxonox/worldentities/BigExplosion.cc @ 9869

Last change on this file since 9869 was 9869, checked in by zifloria, 10 years ago

tried to make particle effects dynamic. Didn't work….

  • Property svn:eol-style set to native
File size: 13.0 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 "util/Exception.h"
32#include "core/CoreIncludes.h"
33#include "core/GameMode.h"
34#include "core/command/Executor.h"
35#include "tools/ParticleInterface.h"
36#include "Scene.h"
37#include "graphics/ParticleSpawner.h"
38#include "graphics/Model.h"
39#include "MovableEntity.h"
40
41namespace orxonox
42{
43    RegisterClass(BigExplosion);
44
45    BigExplosion::BigExplosion(Context* context) : MobileEntity(context)
46    {
47        RegisterObject(BigExplosion);
48
49        if ( GameMode::showsGraphics() && ( !this->getScene() || !this->getScene()->getSceneManager() ) )
50            ThrowException(AbortLoading, "Can't create BigExplosion, no scene or no scene manager given.");
51
52        this->bStop_ = false;
53        this->LOD_ = LODParticle::Normal;
54
55        if ( GameMode::showsGraphics() )
56        {
57            try
58            {
59                this->init();
60            }
61            catch (const std::exception& ex)
62            {
63                orxout(internal_error) << "Couldn't load particle effect in BigExplosion: " << ex.what() << endl;
64                this->initZero();
65            }
66        }
67        else
68        {
69            this->initZero();
70        }
71
72        if (GameMode::isMaster())
73        {
74            this->destroyTimer_.setTimer(rnd(2, 4), false, createExecutor(createFunctor(&BigExplosion::stop, this)));
75        }
76
77        this->registerVariables();
78    }
79
80    void BigExplosion::init()
81    {
82        this->debrisEntity1_ = new MovableEntity(this->getContext());
83        this->debrisEntity2_ = new MovableEntity(this->getContext());
84        this->debrisEntity3_ = new MovableEntity(this->getContext());
85        this->debrisEntity4_ = new MovableEntity(this->getContext());
86
87        this->debrisEntity1_->setSyncMode(0);
88        this->debrisEntity2_->setSyncMode(0);
89        this->debrisEntity3_->setSyncMode(0);
90        this->debrisEntity4_->setSyncMode(0);
91
92        this->debris1_ = new Model(this->getContext());
93        this->debris2_ = new Model(this->getContext());
94        this->debris3_ = new Model(this->getContext());
95        this->debris4_ = new Model(this->getContext());
96
97        this->debris1_->setSyncMode(0);
98        this->debris2_->setSyncMode(0);
99        this->debris3_->setSyncMode(0);
100        this->debris4_->setSyncMode(0);
101
102        this->explosion_ = new MobileEntity(this->getContext());
103        this->explosion_->setSyncMode(0);
104
105        this->debrisSmoke1_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
106        this->debrisSmoke2_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
107        this->debrisSmoke3_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
108        this->debrisSmoke4_ =  new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
109
110        this->debrisFire1_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
111        this->debrisFire2_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
112        this->debrisFire3_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
113        this->debrisFire4_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
114
115        this->debris1_->attachOgreObject(this->debrisSmoke1_->getParticleSystem());
116        this->debris1_->attachOgreObject(this->debrisFire1_->getParticleSystem());
117        this->debris2_->attachOgreObject(this->debrisSmoke2_->getParticleSystem());
118        this->debris2_->attachOgreObject(this->debrisFire2_->getParticleSystem());
119        this->debris3_->attachOgreObject(this->debrisSmoke3_->getParticleSystem());
120        this->debris3_->attachOgreObject(this->debrisFire3_->getParticleSystem());
121        this->debris4_->attachOgreObject(this->debrisSmoke4_->getParticleSystem());
122        this->debris4_->attachOgreObject(this->debrisFire4_->getParticleSystem());
123
124        this->debris1_->setMeshSource("CockpitDebris.mesh");
125        this->debris2_->setMeshSource("WingDebris1.mesh");
126        this->debris3_->setMeshSource("BodyDebris1.mesh");
127        this->debris4_->setMeshSource("WingDebris2.mesh");
128
129        this->debrisEntity1_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
130        this->debrisEntity1_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
131        this->debrisEntity1_->setScale(4);
132
133        this->debrisEntity2_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
134        this->debrisEntity2_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
135        this->debrisEntity2_->setScale(4);
136
137        this->debrisEntity3_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
138        this->debrisEntity3_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
139        this->debrisEntity3_->setScale(4);
140
141        this->debrisEntity4_->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(50,100));
142        this->debrisEntity4_->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
143        this->debrisEntity4_->setScale(4);
144
145        this->debrisEntity1_->attach(debris1_);
146        this->debrisEntity2_->attach(debris2_);
147        this->debrisEntity3_->attach(debris3_);
148        this->debrisEntity4_->attach(debris4_);
149
150        // ///////////////////////
151        // TODO: particleSpawner is a static entity. It should probably be dynamic, for better explosions.
152        //
153        ParticleSpawner* effect = new ParticleSpawner(this->getContext());
154        // orxout() << "vel " << getVelocity() << endl;
155        // effect->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10, 200));
156        effect->setDestroyAfterLife(true);
157        effect->setSource("Orxonox/explosion2b");
158        effect->setLifetime(4.0f);
159        effect->setSyncMode(0);
160
161        ParticleSpawner* effect2 = new ParticleSpawner(this->getContext());
162        // effect2->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10, 200));
163        effect2->setDestroyAfterLife(true);
164        effect2->setSource("Orxonox/smoke6");
165        effect2->setLifetime(4.0f);
166        effect2->setSyncMode(0);
167
168        this->explosion_->attach(effect);
169        this->explosion_->attach(effect2);
170
171        this->attach(explosion_);
172        this->attach(debrisEntity1_);
173        this->attach(debrisEntity2_);
174        this->attach(debrisEntity3_);
175        this->attach(debrisEntity4_);
176
177
178        for(int i=0;i<10;i++)
179        {
180            Model* part1 = new Model(this->getContext());
181            Model* part2 = new Model(this->getContext());
182
183            MovableEntity* partEntity1 = new MovableEntity(this->getContext());
184            MovableEntity* partEntity2 = new MovableEntity(this->getContext());
185
186            part1->setSyncMode(0);
187            part2->setSyncMode(0);
188            partEntity1->setSyncMode(0);
189            partEntity2->setSyncMode(0);
190
191            partEntity1->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10,100));
192            partEntity1->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
193            partEntity1->setScale(rnd(1, 3));
194
195            partEntity2->setVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1))*rnd(10, 100));
196            partEntity2->setAngularVelocity(Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)).normalisedCopy() * Degree(400).valueRadians());
197            partEntity2->setScale(rnd(1, 3));
198
199            part1->setMeshSource("SmallPart1.mesh");
200            part2->setMeshSource("SmallPart2.mesh");
201
202            partEntity1->attach(part1);
203            partEntity2->attach(part2);
204
205            this->attach(partEntity1);
206            this->attach(partEntity2);
207        }
208    }
209
210    void BigExplosion::initZero()
211    {
212        this->debrisFire1_ = 0;
213        this->debrisFire2_ = 0;
214        this->debrisFire3_ = 0;
215        this->debrisFire4_ = 0;
216
217        this->debrisSmoke1_ = 0;
218        this->debrisSmoke2_ = 0;
219        this->debrisSmoke3_ = 0;
220        this->debrisSmoke4_ = 0;
221
222        this->explosionSmoke_=0;
223        this->explosionFire_=0;
224    }
225
226    BigExplosion::~BigExplosion()
227    {
228        if (this->isInitialized())
229        {
230            if (this->debrisFire1_)
231            {
232                this->debris1_->detachOgreObject(this->debrisFire1_->getParticleSystem());
233                delete this->debrisFire1_;
234            }
235            if (this->debrisSmoke1_)
236            {
237                this->debris1_->detachOgreObject(this->debrisSmoke1_->getParticleSystem());
238                delete this->debrisSmoke1_;
239            }
240
241            if (this->debrisFire2_)
242            {
243                this->debris2_->detachOgreObject(this->debrisFire2_->getParticleSystem());
244                delete this->debrisFire2_;
245            }
246            if (this->debrisSmoke2_)
247            {
248                this->debris2_->detachOgreObject(this->debrisSmoke2_->getParticleSystem());
249                delete this->debrisSmoke2_;
250            }
251
252            if (this->debrisFire3_)
253            {
254                this->debris3_->detachOgreObject(this->debrisFire3_->getParticleSystem());
255                delete this->debrisFire3_;
256            }
257            if (this->debrisSmoke3_)
258            {
259                this->debris3_->detachOgreObject(this->debrisSmoke3_->getParticleSystem());
260                delete this->debrisSmoke3_;
261            }
262
263            if (this->debrisFire4_)
264            {
265                this->debris4_->detachOgreObject(this->debrisFire4_->getParticleSystem());
266                delete this->debrisFire4_;
267            }
268            if (this->debrisSmoke4_)
269            {
270                this->debris4_->detachOgreObject(this->debrisSmoke4_->getParticleSystem());
271                delete this->debrisSmoke4_;
272            }
273        }
274    }
275
276    void BigExplosion::registerVariables()
277    {
278        registerVariable((int&)(this->LOD_), VariableDirection::ToClient, new NetworkCallback<BigExplosion>(this, &BigExplosion::LODchanged));
279        registerVariable(this->bStop_,       VariableDirection::ToClient, new NetworkCallback<BigExplosion>(this, &BigExplosion::checkStop));
280    }
281
282    void BigExplosion::LODchanged()
283    {
284        if (this->debrisFire1_)
285            this->debrisFire1_->setDetailLevel(this->LOD_);
286        if (this->debrisSmoke1_)
287            this->debrisSmoke1_->setDetailLevel(this->LOD_);
288
289        if (this->debrisFire2_)
290            this->debrisFire2_->setDetailLevel(this->LOD_);
291        if (this->debrisSmoke2_)
292            this->debrisSmoke2_->setDetailLevel(this->LOD_);
293
294        if (this->debrisFire3_)
295            this->debrisFire3_->setDetailLevel(this->LOD_);
296        if (this->debrisSmoke3_)
297            this->debrisSmoke3_->setDetailLevel(this->LOD_);
298
299        if (this->debrisFire4_)
300            this->debrisFire4_->setDetailLevel(this->LOD_);
301        if (this->debrisSmoke4_)
302            this->debrisSmoke4_->setDetailLevel(this->LOD_);
303    }
304
305    void BigExplosion::checkStop()
306    {
307        if (this->bStop_)
308            this->stop();
309    }
310
311    void BigExplosion::stop()
312    {
313        if (this->debrisFire1_)
314            this->debrisFire1_->setEnabled(false);
315        if (this->debrisSmoke1_)
316            this->debrisSmoke1_->setEnabled(false);
317
318        if (this->debrisFire2_)
319            this->debrisFire2_->setEnabled(false);
320        if (this->debrisSmoke2_)
321            this->debrisSmoke2_->setEnabled(false);
322
323        if (this->debrisFire3_)
324            this->debrisFire3_->setEnabled(false);
325        if (this->debrisSmoke3_)
326            this->debrisSmoke3_->setEnabled(false);
327
328        if (this->debrisFire4_)
329            this->debrisFire4_->setEnabled(false);
330        if (this->debrisSmoke4_)
331            this->debrisSmoke4_->setEnabled(false);
332
333        if (GameMode::isMaster())
334        {
335            this->bStop_ = true;
336            this->destroyTimer_.setTimer(1.0f, false, createExecutor(createFunctor(&BigExplosion::destroy, this)));
337        }
338    }
339
340/* TODO
341
342    void BigExplosion::setDebrisMeshes()
343    {
344
345    }
346    void BigExplosion::getDebrisMeshes()
347    {
348
349    }
350*/
351}
Note: See TracBrowser for help on using the repository browser.