Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3129 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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