| 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 |     } | 
|---|
| 55 |  | 
|---|
| 56 |     void SplitGunProjectile::setNumberOfSplits(int numberOfSplits) | 
|---|
| 57 |     { | 
|---|
| 58 |         if (numberOfSplits >= 0) | 
|---|
| 59 |         { | 
|---|
| 60 |             this->numberOfSplits_ = numberOfSplits; | 
|---|
| 61 |         } | 
|---|
| 62 |         else | 
|---|
| 63 |         { | 
|---|
| 64 |             this->numberOfSplits_ = 0; | 
|---|
| 65 |         } | 
|---|
| 66 |     } | 
|---|
| 67 |  | 
|---|
| 68 |     void SplitGunProjectile::setNumberOfChilds(int numberOfChilds) | 
|---|
| 69 |     { | 
|---|
| 70 |         if (numberOfChilds >= 0) | 
|---|
| 71 |         { | 
|---|
| 72 |             this->numberOfChilds_ = numberOfChilds; | 
|---|
| 73 |         } | 
|---|
| 74 |         else | 
|---|
| 75 |         { | 
|---|
| 76 |             this->numberOfChilds_ = 0; | 
|---|
| 77 |         } | 
|---|
| 78 |     } | 
|---|
| 79 |  | 
|---|
| 80 |     /** | 
|---|
| 81 |     @brief | 
|---|
| 82 |         This function starts a timer that will cause the projectile to split after a time defined by the argument @param splitTime.         | 
|---|
| 83 |     */ | 
|---|
| 84 |     void SplitGunProjectile::setSplitTime(float splitTime) | 
|---|
| 85 |     { | 
|---|
| 86 |         if (splitTime >= 0) | 
|---|
| 87 |         { | 
|---|
| 88 |             this->splitTime_ = splitTime; | 
|---|
| 89 |             this->splitTimer_.setTimer(this->splitTime_, false, createExecutor(createFunctor(&SplitGunProjectile::split, this))); | 
|---|
| 90 |         } | 
|---|
| 91 |         else | 
|---|
| 92 |         { | 
|---|
| 93 |             this->splitTime_ = 0; | 
|---|
| 94 |         } | 
|---|
| 95 |     } | 
|---|
| 96 |  | 
|---|
| 97 |     void SplitGunProjectile::setSpread(float spread) | 
|---|
| 98 |     { | 
|---|
| 99 |         this->spread_ = spread; | 
|---|
| 100 |     } | 
|---|
| 101 |  | 
|---|
| 102 |     /** | 
|---|
| 103 |     @brief | 
|---|
| 104 |         This is the setter function for the damageReduction_ variable. The value of the variable is bounded between 0 and 1. | 
|---|
| 105 |     */ | 
|---|
| 106 |     void SplitGunProjectile::setDamageReduction(float damageReduction) | 
|---|
| 107 |     { | 
|---|
| 108 |         this->damageReduction_ = (damageReduction >= 0.0f ? damageReduction : 1.0f); | 
|---|
| 109 |     } | 
|---|
| 110 |  | 
|---|
| 111 |     /** | 
|---|
| 112 |     @brief | 
|---|
| 113 |         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. | 
|---|
| 114 |     */ | 
|---|
| 115 |     void SplitGunProjectile::split() | 
|---|
| 116 |     { | 
|---|
| 117 |         if (numberOfSplits_ > 0) | 
|---|
| 118 |         { | 
|---|
| 119 |             -- numberOfSplits_; | 
|---|
| 120 |  | 
|---|
| 121 |             // Reduce damage of this projectile | 
|---|
| 122 |             this->setDamage(this->getDamage() * this->damageReduction_ / (this->numberOfChilds_+1)); | 
|---|
| 123 |  | 
|---|
| 124 |             // 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. | 
|---|
| 125 |             Vector3 velocityInitial = this->getVelocity(); | 
|---|
| 126 |             Vector3 velocityOffset = velocityInitial.perpendicular(); | 
|---|
| 127 |             velocityOffset.normalise(); | 
|---|
| 128 |             Degree angle = Degree(rnd(0,360)); | 
|---|
| 129 |             velocityOffset = Quaternion(angle, velocityInitial.normalisedCopy()) * velocityOffset; | 
|---|
| 130 |             velocityOffset.normalise(); | 
|---|
| 131 |  | 
|---|
| 132 |             // Create as many childs as defined by numberOfChilds_ | 
|---|
| 133 |             for (int i = 0; i < numberOfChilds_; ++i) | 
|---|
| 134 |             { | 
|---|
| 135 |                 // Every child projectile should fly uniform in different directions -> Rotate the velocityOffset in every iteration | 
|---|
| 136 |                 velocityOffset = Quaternion(Degree(360.0f/numberOfChilds_), velocityInitial.normalisedCopy()) * velocityOffset; | 
|---|
| 137 |                 velocityOffset.normalise(); | 
|---|
| 138 |  | 
|---|
| 139 |                 SplitGunProjectile* projectile = new SplitGunProjectile(this->getContext());                 | 
|---|
| 140 |  | 
|---|
| 141 |                 projectile->setMaterial(this->getMaterial()); | 
|---|
| 142 |                 projectile->setOrientation(this->getOrientation()); | 
|---|
| 143 |                 projectile->setPosition(this->getPosition()); | 
|---|
| 144 |                 projectile->setVelocity(velocityInitial + spread_*velocityOffset*velocityInitial.length()); | 
|---|
| 145 |  | 
|---|
| 146 |                 projectile->setNumberOfSplits(this->numberOfSplits_); | 
|---|
| 147 |                 projectile->setNumberOfChilds(this->numberOfChilds_); | 
|---|
| 148 |                 projectile->setSplitTime(this->splitTime_); | 
|---|
| 149 |                 projectile->setSpread(this->spread_); | 
|---|
| 150 |                 projectile->setDamageReduction(this->damageReduction_); | 
|---|
| 151 |  | 
|---|
| 152 |                 projectile->setShooter(this->getShooter()); | 
|---|
| 153 |                 projectile->setDamage(this->getDamage()); | 
|---|
| 154 |                 projectile->setShieldDamage(this->getShieldDamage()); | 
|---|
| 155 |                 projectile->setHealthDamage(this->getHealthDamage()); | 
|---|
| 156 |             } | 
|---|
| 157 |  | 
|---|
| 158 |             numberOfSplits_ = 0; | 
|---|
| 159 |         } | 
|---|
| 160 |     } | 
|---|
| 161 | } | 
|---|