Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9677 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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