| 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 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |     CreateFactory(ExplosionChunk); | 
|---|
| 44 |  | 
|---|
| 45 |     ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator) | 
|---|
| 46 |     { | 
|---|
| 47 |         RegisterObject(ExplosionChunk); | 
|---|
| 48 |  | 
|---|
| 49 |         if (!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 |         try | 
|---|
| 56 |         { | 
|---|
| 57 |             this->fire_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_); | 
|---|
| 58 |             this->attachOgreObject(this->fire_->getParticleSystem()); | 
|---|
| 59 |             this->smoke_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_); | 
|---|
| 60 |             this->attachOgreObject(this->smoke_->getParticleSystem()); | 
|---|
| 61 |         } | 
|---|
| 62 |         catch (...) | 
|---|
| 63 |         { | 
|---|
| 64 |             COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl; | 
|---|
| 65 |             this->fire_ = 0; | 
|---|
| 66 |             this->smoke_ = 0; | 
|---|
| 67 |         } | 
|---|
| 68 |  | 
|---|
| 69 |         if (Core::isMaster()) | 
|---|
| 70 |         { | 
|---|
| 71 |             Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)); | 
|---|
| 72 |             velocity.normalise(); | 
|---|
| 73 |             velocity *= rnd(60, 80); | 
|---|
| 74 |             this->setVelocity(velocity); | 
|---|
| 75 |  | 
|---|
| 76 |             this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop))); | 
|---|
| 77 |         } | 
|---|
| 78 |  | 
|---|
| 79 |         this->registerVariables(); | 
|---|
| 80 |     } | 
|---|
| 81 |  | 
|---|
| 82 |     ExplosionChunk::~ExplosionChunk() | 
|---|
| 83 |     { | 
|---|
| 84 |         if (this->isInitialized()) | 
|---|
| 85 |         { | 
|---|
| 86 |             if (this->fire_) | 
|---|
| 87 |             { | 
|---|
| 88 |                 this->detachOgreObject(this->fire_->getParticleSystem()); | 
|---|
| 89 |                 delete this->fire_; | 
|---|
| 90 |             } | 
|---|
| 91 |             if (this->smoke_) | 
|---|
| 92 |             { | 
|---|
| 93 |                 this->detachOgreObject(this->smoke_->getParticleSystem()); | 
|---|
| 94 |                 delete this->smoke_; | 
|---|
| 95 |             } | 
|---|
| 96 |         } | 
|---|
| 97 |     } | 
|---|
| 98 |  | 
|---|
| 99 |     void ExplosionChunk::registerVariables() | 
|---|
| 100 |     { | 
|---|
| 101 |         registerVariable((int&)(this->LOD_), variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::LODchanged)); | 
|---|
| 102 |         registerVariable(this->bStop_,       variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::checkStop)); | 
|---|
| 103 |     } | 
|---|
| 104 |  | 
|---|
| 105 |     void ExplosionChunk::LODchanged() | 
|---|
| 106 |     { | 
|---|
| 107 |         if (this->fire_) | 
|---|
| 108 |             this->fire_->setDetailLevel(this->LOD_); | 
|---|
| 109 |         if (this->smoke_) | 
|---|
| 110 |             this->smoke_->setDetailLevel(this->LOD_); | 
|---|
| 111 |     } | 
|---|
| 112 |  | 
|---|
| 113 |     void ExplosionChunk::checkStop() | 
|---|
| 114 |     { | 
|---|
| 115 |         if (this->bStop_) | 
|---|
| 116 |             this->stop(); | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     void ExplosionChunk::stop() | 
|---|
| 120 |     { | 
|---|
| 121 |         if (this->fire_) | 
|---|
| 122 |             this->fire_->setEnabled(false); | 
|---|
| 123 |         if (this->smoke_) | 
|---|
| 124 |             this->smoke_->setEnabled(false); | 
|---|
| 125 |  | 
|---|
| 126 |         if (Core::isMaster()) | 
|---|
| 127 |         { | 
|---|
| 128 |             this->bStop_ = true; | 
|---|
| 129 |             this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy))); | 
|---|
| 130 |         } | 
|---|
| 131 |     } | 
|---|
| 132 |  | 
|---|
| 133 |     void ExplosionChunk::destroy() | 
|---|
| 134 |     { | 
|---|
| 135 |         delete this; | 
|---|
| 136 |     } | 
|---|
| 137 |  | 
|---|
| 138 |     void ExplosionChunk::tick(float dt) | 
|---|
| 139 |     { | 
|---|
| 140 |         static const unsigned int CHANGES_PER_SECOND = 5; | 
|---|
| 141 |  | 
|---|
| 142 |         if (Core::isMaster() && rnd() < dt*CHANGES_PER_SECOND) | 
|---|
| 143 |         { | 
|---|
| 144 |             float length = this->getVelocity().length(); | 
|---|
| 145 |  | 
|---|
| 146 |             Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)); | 
|---|
| 147 |             change.normalise(); | 
|---|
| 148 |             change *= rnd(0.4, 0.8); | 
|---|
| 149 |             Vector3 velocity = this->getVelocity(); | 
|---|
| 150 |             velocity.normalise(); | 
|---|
| 151 |             velocity += change; | 
|---|
| 152 |             velocity.normalise(); | 
|---|
| 153 |             velocity *= length * rnd(0.8, 1.0); | 
|---|
| 154 |  | 
|---|
| 155 |             this->setVelocity(velocity); | 
|---|
| 156 |         } | 
|---|
| 157 |  | 
|---|
| 158 |         SUPER(ExplosionChunk, tick, dt); | 
|---|
| 159 |     } | 
|---|
| 160 | } | 
|---|