| 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 "Projectile.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/ConfigValueIncludes.h" | 
|---|
| 33 | #include "core/GameMode.h" | 
|---|
| 34 | #include "core/command/Executor.h" | 
|---|
| 35 | #include "objects/collisionshapes/SphereCollisionShape.h" | 
|---|
| 36 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 37 | #include "graphics/ParticleSpawner.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 | CreateFactory(Projectile); | 
|---|
| 42 |  | 
|---|
| 43 | Projectile::Projectile(BaseObject* creator) : MovableEntity(creator) | 
|---|
| 44 | { | 
|---|
| 45 | RegisterObject(Projectile); | 
|---|
| 46 |  | 
|---|
| 47 | this->setConfigValues(); | 
|---|
| 48 | this->bDestroy_ = false; | 
|---|
| 49 | this->owner_ = 0; | 
|---|
| 50 | this->damage_ = 15; | 
|---|
| 51 |  | 
|---|
| 52 | // Get notification about collisions | 
|---|
| 53 |  | 
|---|
| 54 | if (GameMode::isMaster()) | 
|---|
| 55 | { | 
|---|
| 56 | this->setMass(1.0); | 
|---|
| 57 | this->enableCollisionCallback(); | 
|---|
| 58 | this->setCollisionResponse(false); | 
|---|
| 59 | this->setCollisionType(Kinematic); | 
|---|
| 60 |  | 
|---|
| 61 | SphereCollisionShape* shape = new SphereCollisionShape(this); | 
|---|
| 62 | shape->setRadius(20); | 
|---|
| 63 | this->attachCollisionShape(shape); | 
|---|
| 64 |  | 
|---|
| 65 | this->destroyTimer_.setTimer(this->lifetime_, false, createExecutor(createFunctor(&Projectile::destroyObject, this))); | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | Projectile::~Projectile() | 
|---|
| 70 | { | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void Projectile::setConfigValues() | 
|---|
| 74 | { | 
|---|
| 75 | SetConfigValue(lifetime_, 4.0).description("The time in seconds a projectile stays alive"); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | void Projectile::tick(float dt) | 
|---|
| 80 | { | 
|---|
| 81 | SUPER(Projectile, tick, dt); | 
|---|
| 82 |  | 
|---|
| 83 | if (!this->isActive()) | 
|---|
| 84 | return; | 
|---|
| 85 |  | 
|---|
| 86 | if (this->bDestroy_) | 
|---|
| 87 | this->destroy(); // TODO: use a scheduler instead of deleting the object right here in tick() | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void Projectile::destroyObject() | 
|---|
| 91 | { | 
|---|
| 92 | if (GameMode::isMaster()) | 
|---|
| 93 | this->destroy(); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | bool Projectile::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) | 
|---|
| 97 | { | 
|---|
| 98 | if (!this->bDestroy_ && GameMode::isMaster()) | 
|---|
| 99 | { | 
|---|
| 100 | if (otherObject == this->owner_) | 
|---|
| 101 | return false; | 
|---|
| 102 |  | 
|---|
| 103 | this->bDestroy_ = true; | 
|---|
| 104 |  | 
|---|
| 105 | if (this->owner_) | 
|---|
| 106 | { | 
|---|
| 107 | { | 
|---|
| 108 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); | 
|---|
| 109 | effect->setPosition(this->getPosition()); | 
|---|
| 110 | effect->setOrientation(this->getOrientation()); | 
|---|
| 111 | effect->setDestroyAfterLife(true); | 
|---|
| 112 | effect->setSource("Orxonox/explosion3"); | 
|---|
| 113 | effect->setLifetime(2.0f); | 
|---|
| 114 | } | 
|---|
| 115 | { | 
|---|
| 116 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); | 
|---|
| 117 | effect->setPosition(this->getPosition()); | 
|---|
| 118 | effect->setOrientation(this->getOrientation()); | 
|---|
| 119 | effect->setDestroyAfterLife(true); | 
|---|
| 120 | effect->setSource("Orxonox/smoke4"); | 
|---|
| 121 | effect->setLifetime(3.0f); | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | Pawn* victim = orxonox_cast<Pawn*>(otherObject); | 
|---|
| 126 | if (victim) | 
|---|
| 127 | victim->hit(this->owner_, contactPoint, this->damage_); | 
|---|
| 128 | } | 
|---|
| 129 | return false; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void Projectile::setOwner(Pawn* owner) | 
|---|
| 133 | { | 
|---|
| 134 | this->owner_ = owner; | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|