[8492] | 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 | * simonmie |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[8855] | 29 | /** |
---|
| 30 | @file BasicProjectile.h |
---|
| 31 | @brief Implementation of the BasicProjectile class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[8492] | 34 | #include "BasicProjectile.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/GameMode.h" |
---|
| 38 | #include "core/command/Executor.h" |
---|
[8855] | 39 | |
---|
[8492] | 40 | #include "graphics/ParticleSpawner.h" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[9667] | 44 | RegisterClassNoArgs(BasicProjectile); |
---|
| 45 | |
---|
[8492] | 46 | /** |
---|
| 47 | @brief |
---|
| 48 | Constructor. Registers the object and initializes some default values. |
---|
| 49 | */ |
---|
[9667] | 50 | BasicProjectile::BasicProjectile() |
---|
[8492] | 51 | { |
---|
[9667] | 52 | RegisterObject(BasicProjectile);// Register the BasicProjectile class to the core |
---|
[8492] | 53 | |
---|
| 54 | this->bDestroy_ = false; |
---|
| 55 | |
---|
[11108] | 56 | // Default is destroying the projectile after collision |
---|
| 57 | this->destroyAfterCollision_ = true; |
---|
| 58 | |
---|
[8550] | 59 | // Default damage must be zero, otherwise it would be above zero if no settings are made in the weaponsettings xml file. |
---|
| 60 | // same thing for all weaponmodes files |
---|
[8767] | 61 | this->damage_ = 0.0f; |
---|
| 62 | this->healthdamage_ = 0.0f; |
---|
| 63 | this->shielddamage_ = 0.0f; |
---|
[8492] | 64 | } |
---|
| 65 | |
---|
| 66 | BasicProjectile::~BasicProjectile() |
---|
| 67 | { |
---|
| 68 | } |
---|
| 69 | |
---|
[8855] | 70 | /** |
---|
| 71 | @brief |
---|
| 72 | The function called when a projectile hits another thing. |
---|
[11052] | 73 | Calls the hit-function, starts the shield recharge countdown, displays visual hit effects defined in Pawn. |
---|
[8855] | 74 | Needs to be called in the collidesAgainst() function by every Class directly inheriting from BasicProjectile. |
---|
| 75 | @param otherObject |
---|
| 76 | A pointer to the object the Projectile has collided against. |
---|
| 77 | @param contactPoint |
---|
| 78 | A btManifoldPoint indicating the point of contact/impact. |
---|
[11099] | 79 | @param cs |
---|
| 80 | The btCollisionShape of the other object |
---|
[8855] | 81 | @return |
---|
| 82 | Returns true if the collision resulted in a successful hit. |
---|
| 83 | @see Pawn.h |
---|
| 84 | */ |
---|
[10216] | 85 | bool BasicProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) |
---|
[8492] | 86 | { |
---|
[12291] | 87 | orxout() << "OMG A COLLISION" << endl; |
---|
[8855] | 88 | if (!this->bDestroy_ && GameMode::isMaster()) |
---|
[8492] | 89 | { |
---|
[10293] | 90 | if (this->isObjectRelatedToShooter(otherObject) || otherObject->isA(Class(BasicProjectile))) // Prevents you from shooting yourself or other projectiles |
---|
[8492] | 91 | return false; |
---|
| 92 | |
---|
[11108] | 93 | if (getDestroyAfterCollision()) |
---|
| 94 | { |
---|
| 95 | this->bDestroy_ = true; // If something is hit, the object is destroyed and can't hit something else. |
---|
| 96 | // The projectile is destroyed by its tick()-function (in the following tick). |
---|
| 97 | // TODO: Use destroyLater() for this |
---|
| 98 | } |
---|
[8492] | 99 | |
---|
[11071] | 100 | Pawn* victim = orxonox_cast<Pawn*>(otherObject); // If otherObject isn't a Pawn, then victim is nullptr |
---|
[8492] | 101 | |
---|
[8855] | 102 | WorldEntity* entity = orxonox_cast<WorldEntity*>(this); |
---|
| 103 | assert(entity); // The projectile must not be a WorldEntity. |
---|
[8492] | 104 | |
---|
[8855] | 105 | // If visual effects after destruction cause problems, put this block below the effects code block |
---|
[8492] | 106 | if (victim) |
---|
| 107 | { |
---|
[10216] | 108 | victim->hit(this->getShooter(), contactPoint, cs, this->getDamage(), this->getHealthDamage(), this->getShieldDamage()); |
---|
[11052] | 109 | victim->startShieldRechargeCountdown(); |
---|
[8492] | 110 | } |
---|
| 111 | |
---|
[8855] | 112 | // Visual effects for being hit, depending on whether the shield is hit or not |
---|
| 113 | if (this->getShooter()) // If the owner does not exist (anymore?), no effects are displayed. |
---|
[8492] | 114 | { |
---|
[8855] | 115 | // Damping and explosion effect is only played if the victim is no Pawn (see cast above) |
---|
| 116 | // or if the victim is a Pawn, has no shield left, is still alive and any damage goes to the health |
---|
| 117 | if (!victim || (victim && !victim->hasShield() && victim->getHealth() > 0.0f && (this->getDamage() > 0.0f || this->getHealthDamage() > 0.0f))) |
---|
[8492] | 118 | { |
---|
| 119 | { |
---|
[9667] | 120 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
[8492] | 121 | effect->setPosition(entity->getPosition()); |
---|
| 122 | effect->setOrientation(entity->getOrientation()); |
---|
| 123 | effect->setDestroyAfterLife(true); |
---|
| 124 | effect->setSource("Orxonox/explosion3"); |
---|
| 125 | effect->setLifetime(2.0f); |
---|
| 126 | } |
---|
[8855] | 127 | // Second effect with same condition |
---|
[8492] | 128 | { |
---|
[9667] | 129 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
[8492] | 130 | effect->setPosition(entity->getPosition()); |
---|
| 131 | effect->setOrientation(entity->getOrientation()); |
---|
| 132 | effect->setDestroyAfterLife(true); |
---|
| 133 | effect->setSource("Orxonox/smoke4"); |
---|
| 134 | effect->setLifetime(3.0f); |
---|
| 135 | } |
---|
| 136 | } |
---|
[8542] | 137 | |
---|
| 138 | // victim->isAlive() is not false until the next tick, so getHealth() > 0 is used instead |
---|
[8855] | 139 | if (victim && victim->hasShield() && (this->getDamage() > 0.0f || this->getShieldDamage() > 0.0f) && victim->getHealth() > 0.0f) |
---|
[8492] | 140 | { |
---|
[9667] | 141 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
[8492] | 142 | effect->setDestroyAfterLife(true); |
---|
| 143 | effect->setSource("Orxonox/Shield"); |
---|
[8645] | 144 | effect->setLifetime(0.5f); |
---|
[8635] | 145 | victim->attach(effect); |
---|
[8492] | 146 | } |
---|
| 147 | } |
---|
[8855] | 148 | return true; |
---|
[8492] | 149 | } |
---|
| 150 | return false; |
---|
| 151 | } |
---|
[8855] | 152 | |
---|
[10293] | 153 | /** |
---|
| 154 | * Returns true if otherObject is equal to the shooter or if one of otherObject's parents is equal to the shooter or if one of the shooter's parent is equal to otherObject. |
---|
| 155 | */ |
---|
| 156 | bool BasicProjectile::isObjectRelatedToShooter(WorldEntity* otherObject) |
---|
| 157 | { |
---|
[11071] | 158 | for (WorldEntity* shooter = this->getShooter(); shooter != nullptr; shooter = shooter->getParent()) |
---|
[10293] | 159 | if (otherObject == shooter) |
---|
| 160 | return true; |
---|
[11071] | 161 | for (WorldEntity* object = otherObject; object != nullptr; object = object->getParent()) |
---|
[10293] | 162 | if (otherObject == this->getShooter()) |
---|
| 163 | return true; |
---|
[10216] | 164 | |
---|
[10293] | 165 | return false; |
---|
| 166 | } |
---|
| 167 | |
---|
[8855] | 168 | /** |
---|
| 169 | @brief |
---|
| 170 | Check whether the projectile needs to be destroyed and destroys it if so. |
---|
| 171 | Needs to be called in the tick() by every Class directly inheriting from BasicProjectile, to make sure the projectile is destroyed after it has hit something. |
---|
| 172 | */ |
---|
| 173 | void BasicProjectile::destroyCheck(void) |
---|
| 174 | { |
---|
| 175 | if(GameMode::isMaster() && this->bDestroy_) |
---|
| 176 | this->destroy(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | @brief |
---|
| 181 | Destroys the object. |
---|
| 182 | */ |
---|
| 183 | void BasicProjectile::destroyObject(void) |
---|
| 184 | { |
---|
| 185 | if(GameMode::isMaster()) |
---|
| 186 | this->destroy(); |
---|
| 187 | } |
---|
[8492] | 188 | } |
---|