| 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" | 
|---|
| 32 | #include "core/Executor.h" | 
|---|
| 33 | #include "core/GameMode.h" | 
|---|
| 34 | #include "util/Exception.h" | 
|---|
| 35 | #include "Scene.h" | 
|---|
| 36 | #include "tools/ParticleInterface.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     CreateFactory(ExplosionChunk); | 
|---|
| 41 |  | 
|---|
| 42 |     ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator) | 
|---|
| 43 |     { | 
|---|
| 44 |         RegisterObject(ExplosionChunk); | 
|---|
| 45 |  | 
|---|
| 46 |         if ( GameMode::showsGraphics() && ( !this->getScene() || !this->getScene()->getSceneManager() ) ) | 
|---|
| 47 |             ThrowException(AbortLoading, "Can't create ExplosionChunk, no scene or no scene manager given."); | 
|---|
| 48 |  | 
|---|
| 49 |         this->bStop_ = false; | 
|---|
| 50 |         this->LOD_ = LODParticle::Normal; | 
|---|
| 51 |  | 
|---|
| 52 |         if ( GameMode::showsGraphics() ) | 
|---|
| 53 |         { | 
|---|
| 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 |             } | 
|---|
| 61 |             catch (...) | 
|---|
| 62 |             { | 
|---|
| 63 |                 COUT(1) << "Error: Couln't load particle effect in ExplosionChunk: " << Exception::handleMessage() << std::endl; | 
|---|
| 64 |                 this->fire_ = 0; | 
|---|
| 65 |                 this->smoke_ = 0; | 
|---|
| 66 |             } | 
|---|
| 67 |         } | 
|---|
| 68 |         else | 
|---|
| 69 |         { | 
|---|
| 70 |             this->fire_ = 0; | 
|---|
| 71 |             this->smoke_ = 0; | 
|---|
| 72 |         } | 
|---|
| 73 |  | 
|---|
| 74 |         if (GameMode::isMaster()) | 
|---|
| 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); | 
|---|
| 80 |  | 
|---|
| 81 |             this->destroyTimer_.setTimer(rnd(1, 2), false, createExecutor(createFunctor(&ExplosionChunk::stop, this))); | 
|---|
| 82 |         } | 
|---|
| 83 |  | 
|---|
| 84 |         this->registerVariables(); | 
|---|
| 85 |     } | 
|---|
| 86 |  | 
|---|
| 87 |     ExplosionChunk::~ExplosionChunk() | 
|---|
| 88 |     { | 
|---|
| 89 |         if (this->isInitialized()) | 
|---|
| 90 |         { | 
|---|
| 91 |             if (this->fire_) | 
|---|
| 92 |             { | 
|---|
| 93 |                 this->detachOgreObject(this->fire_->getParticleSystem()); | 
|---|
| 94 |                 this->fire_->destroy(); | 
|---|
| 95 |             } | 
|---|
| 96 |             if (this->smoke_) | 
|---|
| 97 |             { | 
|---|
| 98 |                 this->detachOgreObject(this->smoke_->getParticleSystem()); | 
|---|
| 99 |                 this->smoke_->destroy(); | 
|---|
| 100 |             } | 
|---|
| 101 |         } | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     void ExplosionChunk::registerVariables() | 
|---|
| 105 |     { | 
|---|
| 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)); | 
|---|
| 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 |  | 
|---|
| 118 |     void ExplosionChunk::checkStop() | 
|---|
| 119 |     { | 
|---|
| 120 |         if (this->bStop_) | 
|---|
| 121 |             this->stop(); | 
|---|
| 122 |     } | 
|---|
| 123 |  | 
|---|
| 124 |     void ExplosionChunk::stop() | 
|---|
| 125 |     { | 
|---|
| 126 |         if (this->fire_) | 
|---|
| 127 |             this->fire_->setEnabled(false); | 
|---|
| 128 |         if (this->smoke_) | 
|---|
| 129 |             this->smoke_->setEnabled(false); | 
|---|
| 130 |  | 
|---|
| 131 |         if (GameMode::isMaster()) | 
|---|
| 132 |         { | 
|---|
| 133 |             this->bStop_ = true; | 
|---|
| 134 |             this->destroyTimer_.setTimer(1.0f, false, createExecutor(createFunctor(&ExplosionChunk::destroy, this))); | 
|---|
| 135 |         } | 
|---|
| 136 |     } | 
|---|
| 137 |  | 
|---|
| 138 |     void ExplosionChunk::tick(float dt) | 
|---|
| 139 |     { | 
|---|
| 140 |         static const unsigned int CHANGES_PER_SECOND = 5; | 
|---|
| 141 |  | 
|---|
| 142 |         if (GameMode::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.4f, 0.8f); | 
|---|
| 149 |             Vector3 velocity = this->getVelocity(); | 
|---|
| 150 |             velocity.normalise(); | 
|---|
| 151 |             velocity += change; | 
|---|
| 152 |             velocity.normalise(); | 
|---|
| 153 |             velocity *= length * rnd(0.8f, 1.0f); | 
|---|
| 154 |  | 
|---|
| 155 |             this->setVelocity(velocity); | 
|---|
| 156 |         } | 
|---|
| 157 |  | 
|---|
| 158 |         SUPER(ExplosionChunk, tick, dt); | 
|---|
| 159 |     } | 
|---|
| 160 | } | 
|---|