[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" |
---|
| 32 | #include "core/Executor.h" |
---|
[3196] | 33 | #include "core/GameMode.h" |
---|
| 34 | #include "util/Exception.h" |
---|
[5735] | 35 | #include "Scene.h" |
---|
[2414] | 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 | |
---|
[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 | } |
---|
| 61 | catch (...) |
---|
| 62 | { |
---|
| 63 | COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl; |
---|
| 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 | |
---|
[2422] | 81 | this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop))); |
---|
| 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()); |
---|
[2414] | 94 | delete this->fire_; |
---|
[2485] | 95 | } |
---|
[2414] | 96 | if (this->smoke_) |
---|
[2485] | 97 | { |
---|
| 98 | this->detachOgreObject(this->smoke_->getParticleSystem()); |
---|
[2414] | 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; |
---|
| 134 | this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy))); |
---|
| 135 | } |
---|
[2414] | 136 | } |
---|
| 137 | |
---|
| 138 | void ExplosionChunk::destroy() |
---|
| 139 | { |
---|
| 140 | delete this; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void ExplosionChunk::tick(float dt) |
---|
| 144 | { |
---|
| 145 | static const unsigned int CHANGES_PER_SECOND = 5; |
---|
| 146 | |
---|
[2896] | 147 | if (GameMode::isMaster() && rnd() < dt*CHANGES_PER_SECOND) |
---|
[2414] | 148 | { |
---|
| 149 | float length = this->getVelocity().length(); |
---|
| 150 | |
---|
| 151 | Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)); |
---|
| 152 | change.normalise(); |
---|
[3196] | 153 | change *= rnd(0.4f, 0.8f); |
---|
[2414] | 154 | Vector3 velocity = this->getVelocity(); |
---|
| 155 | velocity.normalise(); |
---|
| 156 | velocity += change; |
---|
| 157 | velocity.normalise(); |
---|
[3196] | 158 | velocity *= length * rnd(0.8f, 1.0f); |
---|
[2414] | 159 | |
---|
| 160 | this->setVelocity(velocity); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | SUPER(ExplosionChunk, tick, dt); |
---|
| 164 | } |
---|
| 165 | } |
---|