Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/ExplosionChunk.cc @ 2802

Last change on this file since 2802 was 2759, checked in by scheusso, 16 years ago

merged network branch (windows,multiplayer fixes) back to trunk

  • Property svn:eol-style set to native
File size: 5.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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "ExplosionChunk.h"
31
32#include <OgreParticleSystem.h>
33
34#include "core/Core.h"
35#include "core/CoreIncludes.h"
36#include "core/Executor.h"
37#include "objects/Scene.h"
38#include "tools/ParticleInterface.h"
39#include "util/Exception.h"
40
41namespace orxonox
42{
43    CreateFactory(ExplosionChunk);
44
45    ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator)
46    {
47        RegisterObject(ExplosionChunk);
48
49        if ( Core::showsGraphics() && ( !this->getScene() || !this->getScene()->getSceneManager() ) )
50            ThrowException(AbortLoading, "Can't create ExplosionChunk, no scene or no scene manager given.");
51
52        this->bStop_ = false;
53        this->LOD_ = LODParticle::normal;
54
55        if ( Core::showsGraphics() )
56        {
57            try
58            {
59                this->fire_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
60                this->attachOgreObject(this->fire_->getParticleSystem());
61                this->smoke_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
62                this->attachOgreObject(this->smoke_->getParticleSystem());
63            }
64            catch (...)
65            {
66                COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl;
67                this->fire_ = 0;
68                this->smoke_ = 0;
69            }
70        }
71        else
72        {
73            this->fire_ = 0;
74            this->smoke_ = 0;
75        }
76
77        if (Core::isMaster())
78        {
79            Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
80            velocity.normalise();
81            velocity *= rnd(60, 80);
82            this->setVelocity(velocity);
83
84            this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop)));
85        }
86
87        this->registerVariables();
88    }
89
90    ExplosionChunk::~ExplosionChunk()
91    {
92        if (this->isInitialized())
93        {
94            if (this->fire_)
95            {
96                this->detachOgreObject(this->fire_->getParticleSystem());
97                delete this->fire_;
98            }
99            if (this->smoke_)
100            {
101                this->detachOgreObject(this->smoke_->getParticleSystem());
102                delete this->smoke_;
103            }
104        }
105    }
106
107    void ExplosionChunk::registerVariables()
108    {
109        registerVariable((int&)(this->LOD_), variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::LODchanged));
110        registerVariable(this->bStop_,       variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::checkStop));
111    }
112
113    void ExplosionChunk::LODchanged()
114    {
115        if (this->fire_)
116            this->fire_->setDetailLevel(this->LOD_);
117        if (this->smoke_)
118            this->smoke_->setDetailLevel(this->LOD_);
119    }
120
121    void ExplosionChunk::checkStop()
122    {
123        if (this->bStop_)
124            this->stop();
125    }
126
127    void ExplosionChunk::stop()
128    {
129        if (this->fire_)
130            this->fire_->setEnabled(false);
131        if (this->smoke_)
132            this->smoke_->setEnabled(false);
133
134        if (Core::isMaster())
135        {
136            this->bStop_ = true;
137            this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy)));
138        }
139    }
140
141    void ExplosionChunk::destroy()
142    {
143        delete this;
144    }
145
146    void ExplosionChunk::tick(float dt)
147    {
148        static const unsigned int CHANGES_PER_SECOND = 5;
149
150        if (Core::isMaster() && rnd() < dt*CHANGES_PER_SECOND)
151        {
152            float length = this->getVelocity().length();
153
154            Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
155            change.normalise();
156            change *= rnd(0.4, 0.8);
157            Vector3 velocity = this->getVelocity();
158            velocity.normalise();
159            velocity += change;
160            velocity.normalise();
161            velocity *= length * rnd(0.8, 1.0);
162
163            this->setVelocity(velocity);
164        }
165
166        SUPER(ExplosionChunk, tick, dt);
167    }
168}
Note: See TracBrowser for help on using the repository browser.