| 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 | *      Fabien Vultier | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file SplitGunProjectile.h | 
|---|
| 31 | @brief Implementation of the SplitGunProjectile class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "SplitGunProjectile.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 | #include "core/command/Executor.h" | 
|---|
| 38 | #include "util/Convert.h" | 
|---|
| 39 | #include "util/Math.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 | RegisterClass(SplitGunProjectile); | 
|---|
| 44 |  | 
|---|
| 45 | SplitGunProjectile::SplitGunProjectile(Context* context) : BillboardProjectile(context) | 
|---|
| 46 | { | 
|---|
| 47 | RegisterObject(SplitGunProjectile); | 
|---|
| 48 |  | 
|---|
| 49 | this->numberOfSplits_ = 0; | 
|---|
| 50 | this->numberOfChilds_ = 0; | 
|---|
| 51 | this->splitTime_ = 1.0f; | 
|---|
| 52 | this->spread_ = 0.2f; | 
|---|
| 53 | this->damageReduction_ = 1.0f; | 
|---|
| 54 | this->splitSound_ = nullptr; | 
|---|
| 55 |  | 
|---|
| 56 | this->setSplitSound("sounds/Weapon_SplitGun.ogg", 0.8); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | SplitGunProjectile::~SplitGunProjectile() | 
|---|
| 60 | { | 
|---|
| 61 | if (this->isInitialized()) | 
|---|
| 62 | { | 
|---|
| 63 | if (splitSound_) | 
|---|
| 64 | { | 
|---|
| 65 | splitSound_->destroy(); | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void SplitGunProjectile::setNumberOfSplits(int numberOfSplits) | 
|---|
| 71 | { | 
|---|
| 72 | if (numberOfSplits >= 0) | 
|---|
| 73 | { | 
|---|
| 74 | this->numberOfSplits_ = numberOfSplits; | 
|---|
| 75 | } | 
|---|
| 76 | else | 
|---|
| 77 | { | 
|---|
| 78 | this->numberOfSplits_ = 0; | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | void SplitGunProjectile::setNumberOfChilds(int numberOfChilds) | 
|---|
| 83 | { | 
|---|
| 84 | if (numberOfChilds >= 0) | 
|---|
| 85 | { | 
|---|
| 86 | this->numberOfChilds_ = numberOfChilds; | 
|---|
| 87 | } | 
|---|
| 88 | else | 
|---|
| 89 | { | 
|---|
| 90 | this->numberOfChilds_ = 0; | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 | @brief | 
|---|
| 96 | This function starts a timer that will cause the projectile to split after a time defined by the argument @p splitTime. | 
|---|
| 97 | @param splitTime The time | 
|---|
| 98 | */ | 
|---|
| 99 | void SplitGunProjectile::setSplitTime(float splitTime) | 
|---|
| 100 | { | 
|---|
| 101 | if (splitTime >= 0) | 
|---|
| 102 | { | 
|---|
| 103 | this->splitTime_ = splitTime; | 
|---|
| 104 | this->splitTimer_.setTimer(this->splitTime_, false, createExecutor(createFunctor(&SplitGunProjectile::split, this))); | 
|---|
| 105 | } | 
|---|
| 106 | else | 
|---|
| 107 | { | 
|---|
| 108 | this->splitTime_ = 0; | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void SplitGunProjectile::setSpread(float spread) | 
|---|
| 113 | { | 
|---|
| 114 | this->spread_ = spread; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /** | 
|---|
| 118 | @brief | 
|---|
| 119 | This is the setter function for the damageReduction_ variable. The value of the variable is bounded between 0 and 1. | 
|---|
| 120 | */ | 
|---|
| 121 | void SplitGunProjectile::setDamageReduction(float damageReduction) | 
|---|
| 122 | { | 
|---|
| 123 | this->damageReduction_ = (damageReduction >= 0.0f ? damageReduction : 1.0f); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | /** | 
|---|
| 127 | @brief | 
|---|
| 128 | If this function is called the projectile splits up into many child projectiles. The original projectiles does not get destroyed but it will never split up again. | 
|---|
| 129 | */ | 
|---|
| 130 | void SplitGunProjectile::split() | 
|---|
| 131 | { | 
|---|
| 132 | if (numberOfSplits_ > 0) | 
|---|
| 133 | { | 
|---|
| 134 | -- numberOfSplits_; | 
|---|
| 135 |  | 
|---|
| 136 | // Reduce damage of this projectile | 
|---|
| 137 | this->setDamage(this->getDamage() * this->damageReduction_ / (this->numberOfChilds_+1)); | 
|---|
| 138 |  | 
|---|
| 139 | // Calculate a normalized vector (velocityOffset) that is perpendicluar to the velocity of this projectile. Since there are infinitly many perpendicular vectors a random one is chosen. | 
|---|
| 140 | Vector3 velocityInitial = this->getVelocity(); | 
|---|
| 141 | Vector3 velocityOffset = velocityInitial.perpendicular(); | 
|---|
| 142 | velocityOffset.normalise(); | 
|---|
| 143 | Degree angle = Degree(rnd(0,360)); | 
|---|
| 144 | velocityOffset = Quaternion(angle, velocityInitial.normalisedCopy()) * velocityOffset; | 
|---|
| 145 | velocityOffset.normalise(); | 
|---|
| 146 |  | 
|---|
| 147 | // Create as many childs as defined by numberOfChilds_ | 
|---|
| 148 | for (int i = 0; i < numberOfChilds_; ++i) | 
|---|
| 149 | { | 
|---|
| 150 | // Every child projectile should fly uniform in different directions -> Rotate the velocityOffset in every iteration | 
|---|
| 151 | velocityOffset = Quaternion(Degree(360.0f/numberOfChilds_), velocityInitial.normalisedCopy()) * velocityOffset; | 
|---|
| 152 | velocityOffset.normalise(); | 
|---|
| 153 |  | 
|---|
| 154 | SplitGunProjectile* projectile = new SplitGunProjectile(this->getContext()); | 
|---|
| 155 |  | 
|---|
| 156 | projectile->setMaterial(this->getMaterial()); | 
|---|
| 157 | projectile->setOrientation(this->getOrientation()); | 
|---|
| 158 | projectile->setPosition(this->getPosition()); | 
|---|
| 159 | projectile->setVelocity(velocityInitial + spread_*velocityOffset*velocityInitial.length()); | 
|---|
| 160 |  | 
|---|
| 161 | projectile->setNumberOfSplits(this->numberOfSplits_); | 
|---|
| 162 | projectile->setNumberOfChilds(this->numberOfChilds_); | 
|---|
| 163 | projectile->setSplitTime(this->splitTime_); | 
|---|
| 164 | projectile->setSpread(this->spread_); | 
|---|
| 165 | projectile->setDamageReduction(this->damageReduction_); | 
|---|
| 166 |  | 
|---|
| 167 | projectile->setShooter(this->getShooter()); | 
|---|
| 168 | projectile->setDamage(this->getDamage()); | 
|---|
| 169 | projectile->setShieldDamage(this->getShieldDamage()); | 
|---|
| 170 | projectile->setHealthDamage(this->getHealthDamage()); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | numberOfSplits_ = 0; | 
|---|
| 174 |  | 
|---|
| 175 | if (splitSound_) | 
|---|
| 176 | { | 
|---|
| 177 | splitSound_->play(); | 
|---|
| 178 | } | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | void SplitGunProjectile::setSplitSound(const std::string& soundPath, const float soundVolume) | 
|---|
| 183 | { | 
|---|
| 184 | if (!splitSound_) | 
|---|
| 185 | { | 
|---|
| 186 | this->splitSound_ = new WorldSound(this->getContext()); | 
|---|
| 187 | this->splitSound_->setLooping(false); | 
|---|
| 188 | this->attach(splitSound_); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | this->splitSound_->setSource(soundPath); | 
|---|
| 192 | this->splitSound_->setVolume(soundVolume); | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|