| 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 | * Oliver Scheuss |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "Rocket.h" |
|---|
| 30 | |
|---|
| 31 | #include "core/XMLPort.h" |
|---|
| 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
|---|
| 33 | #include "worldentities/pawns/Pawn.h" |
|---|
| 34 | #include "graphics/ParticleSpawner.h" |
|---|
| 35 | #include "graphics/Model.h" |
|---|
| 36 | #include "objects/collisionshapes/ConeCollisionShape.h" |
|---|
| 37 | #include "infos/PlayerInfo.h" |
|---|
| 38 | #include "controllers/Controller.h" |
|---|
| 39 | #include "worldentities/CameraPosition.h" |
|---|
| 40 | |
|---|
| 41 | #include "sound/WorldSound.h" |
|---|
| 42 | |
|---|
| 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| 45 | CreateFactory(Rocket); |
|---|
| 46 | // create the factory for the Rocket |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | @brief |
|---|
| 50 | Constructor. Registers the object and initializes some default values. |
|---|
| 51 | */ |
|---|
| 52 | Rocket::Rocket(BaseObject* creator) : ControllableEntity(creator) |
|---|
| 53 | { |
|---|
| 54 | RegisterObject(Rocket);// - register the Rocket class to the core |
|---|
| 55 | |
|---|
| 56 | this->localAngularVelocity_ = 0; |
|---|
| 57 | this->bDestroy_ = false; |
|---|
| 58 | this->lifetime_ = 100; |
|---|
| 59 | |
|---|
| 60 | if (GameMode::isMaster()) |
|---|
| 61 | { |
|---|
| 62 | this->setCollisionType(WorldEntity::Kinematic); |
|---|
| 63 | this->setVelocity(0,0,-100); |
|---|
| 64 | |
|---|
| 65 | Model* model = new Model(this); |
|---|
| 66 | model->setMeshSource("rocket.mesh"); |
|---|
| 67 | model->scale(0.7); |
|---|
| 68 | this->attach(model); |
|---|
| 69 | ParticleEmitter* fire = new ParticleEmitter(this); |
|---|
| 70 | this->attach(fire); |
|---|
| 71 | fire->setOrientation(this->getOrientation()); |
|---|
| 72 | fire->setSource("Orxonox/rocketfire"); |
|---|
| 73 | |
|---|
| 74 | this->enableCollisionCallback(); |
|---|
| 75 | this->setCollisionResponse(false); |
|---|
| 76 | this->setCollisionType(Kinematic); |
|---|
| 77 | |
|---|
| 78 | ConeCollisionShape* collisionShape = new ConeCollisionShape(this); |
|---|
| 79 | collisionShape->setRadius(3); |
|---|
| 80 | collisionShape->setHeight(500); |
|---|
| 81 | this->attachCollisionShape(collisionShape); |
|---|
| 82 | |
|---|
| 83 | this->destroyTimer_.setTimer(this->lifetime_, false, createExecutor(createFunctor(&Rocket::destroyObject, this))); |
|---|
| 84 | |
|---|
| 85 | this->defSndWpnEngine_ = new WorldSound(this); |
|---|
| 86 | this->defSndWpnEngine_->setLooping(true); |
|---|
| 87 | this->defSndWpnEngine_->setSource("sounds/Rocket_engine.ogg"); |
|---|
| 88 | this->attach(defSndWpnEngine_); |
|---|
| 89 | |
|---|
| 90 | this->defSndWpnLaunch_ = new WorldSound(this); |
|---|
| 91 | this->defSndWpnLaunch_->setLooping(false); |
|---|
| 92 | this->defSndWpnLaunch_->setSource("sounds/Rocket_launch.ogg"); |
|---|
| 93 | this->attach(defSndWpnLaunch_); |
|---|
| 94 | } |
|---|
| 95 | else |
|---|
| 96 | { |
|---|
| 97 | this->defSndWpnEngine_ = 0; |
|---|
| 98 | this->defSndWpnLaunch_ = 0; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | CameraPosition* camPosition = new CameraPosition(this); |
|---|
| 102 | camPosition->setPosition(0,4,15); |
|---|
| 103 | camPosition->setAllowMouseLook(true); |
|---|
| 104 | this->addCameraPosition(camPosition); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | @brief |
|---|
| 109 | Destructor. Destroys controller, if present and kills sounds, if playing. |
|---|
| 110 | */ |
|---|
| 111 | Rocket::~Rocket() |
|---|
| 112 | { |
|---|
| 113 | if(this->isInitialized()) |
|---|
| 114 | { |
|---|
| 115 | if (GameMode::isMaster() && this->player_) |
|---|
| 116 | { |
|---|
| 117 | this->player_->stopTemporaryControl(); |
|---|
| 118 | } |
|---|
| 119 | if ( this->defSndWpnEngine_ ) |
|---|
| 120 | { |
|---|
| 121 | if ( this->defSndWpnEngine_->isPlaying() ) |
|---|
| 122 | this->defSndWpnEngine_->stop(); |
|---|
| 123 | delete this->defSndWpnEngine_; |
|---|
| 124 | } |
|---|
| 125 | if ( this->defSndWpnLaunch_ ) |
|---|
| 126 | { |
|---|
| 127 | if ( this->defSndWpnLaunch_->isPlaying()) |
|---|
| 128 | this->defSndWpnLaunch_->stop(); |
|---|
| 129 | delete this->defSndWpnLaunch_; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | @brief |
|---|
| 136 | Method for creating a Rocket through XML. |
|---|
| 137 | */ |
|---|
| 138 | void Rocket::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 139 | { |
|---|
| 140 | // this calls the XMLPort function of the parent class |
|---|
| 141 | SUPER(Rocket, XMLPort, xmlelement, mode); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | void Rocket::setOwner(Pawn* owner) |
|---|
| 145 | { |
|---|
| 146 | this->owner_ = owner; |
|---|
| 147 | this->originalControllableEntity_ = this->owner_->getPlayer()->getControllableEntity(); |
|---|
| 148 | this->player_ = this->owner_->getPlayer(); |
|---|
| 149 | this->owner_->getPlayer()->startTemporaryControl(this); |
|---|
| 150 | |
|---|
| 151 | if( GameMode::isMaster() ) |
|---|
| 152 | { |
|---|
| 153 | this->defSndWpnEngine_->play(); |
|---|
| 154 | this->defSndWpnLaunch_->play(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | @brief |
|---|
| 160 | Defines which actions the Rocket has to take in each tick. |
|---|
| 161 | @param dt |
|---|
| 162 | The length of the tick. |
|---|
| 163 | */ |
|---|
| 164 | void Rocket::tick(float dt) |
|---|
| 165 | { |
|---|
| 166 | SUPER(Rocket, tick, dt); |
|---|
| 167 | |
|---|
| 168 | if( this->hasLocalController() ) |
|---|
| 169 | { |
|---|
| 170 | this->setAngularVelocity(this->getOrientation() * this->localAngularVelocity_); |
|---|
| 171 | this->setVelocity( this->getOrientation()*WorldEntity::FRONT*this->getVelocity().length() ); |
|---|
| 172 | this->localAngularVelocity_ = 0; |
|---|
| 173 | |
|---|
| 174 | if( this->bDestroy_ ) |
|---|
| 175 | this->destroy(); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | bool Rocket::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) |
|---|
| 180 | { |
|---|
| 181 | if (!this->bDestroy_ && GameMode::isMaster()) |
|---|
| 182 | { |
|---|
| 183 | if (otherObject == this->owner_) |
|---|
| 184 | return false; |
|---|
| 185 | |
|---|
| 186 | this->bDestroy_ = true; |
|---|
| 187 | |
|---|
| 188 | if (this->owner_) |
|---|
| 189 | { |
|---|
| 190 | { |
|---|
| 191 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); |
|---|
| 192 | effect->setPosition(this->getPosition()); |
|---|
| 193 | effect->setOrientation(this->getOrientation()); |
|---|
| 194 | effect->setDestroyAfterLife(true); |
|---|
| 195 | effect->setSource("Orxonox/explosion4"); |
|---|
| 196 | effect->setLifetime(2.0f); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | { |
|---|
| 200 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); |
|---|
| 201 | effect->setPosition(this->getPosition()); |
|---|
| 202 | effect->setOrientation(this->getOrientation()); |
|---|
| 203 | effect->setDestroyAfterLife(true); |
|---|
| 204 | effect->setSource("Orxonox/smoke4"); |
|---|
| 205 | effect->setLifetime(3.0f); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | float dmg = this->damage_; |
|---|
| 210 | if (this->owner_) |
|---|
| 211 | dmg = this->owner_->getPickups().processModifiers(ModifierType::Damage, dmg, false); |
|---|
| 212 | |
|---|
| 213 | Pawn* victim = orxonox_cast<Pawn*>(otherObject); |
|---|
| 214 | if (victim) |
|---|
| 215 | victim->hit(this->owner_, contactPoint, dmg); |
|---|
| 216 | // this->destroy(); |
|---|
| 217 | } |
|---|
| 218 | return false; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | void Rocket::destroyObject() |
|---|
| 222 | { |
|---|
| 223 | if (GameMode::isMaster()) |
|---|
| 224 | { |
|---|
| 225 | if(this->defSndWpnEngine_->isPlaying()) |
|---|
| 226 | { |
|---|
| 227 | this->defSndWpnEngine_->stop(); |
|---|
| 228 | } |
|---|
| 229 | this->destroy(); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | void Rocket::fired(unsigned int firemode) |
|---|
| 234 | { |
|---|
| 235 | if (this->owner_) |
|---|
| 236 | { |
|---|
| 237 | { |
|---|
| 238 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); |
|---|
| 239 | effect->setPosition(this->getPosition()); |
|---|
| 240 | effect->setOrientation(this->getOrientation()); |
|---|
| 241 | effect->setDestroyAfterLife(true); |
|---|
| 242 | effect->setSource("Orxonox/explosion4"); |
|---|
| 243 | effect->setLifetime(2.0f); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | { |
|---|
| 247 | ParticleSpawner* effect = new ParticleSpawner(this->owner_->getCreator()); |
|---|
| 248 | effect->setPosition(this->getPosition()); |
|---|
| 249 | effect->setOrientation(this->getOrientation()); |
|---|
| 250 | effect->setDestroyAfterLife(true); |
|---|
| 251 | effect->setSource("Orxonox/smoke4"); |
|---|
| 252 | effect->setLifetime(3.0f); |
|---|
| 253 | } |
|---|
| 254 | this->destroy(); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | @brief |
|---|
| 260 | Rotates the Rocket around the y-axis by the amount specified by the first component of the input 2-dim vector. |
|---|
| 261 | @param value |
|---|
| 262 | The vector determining the amount of the angular movement. |
|---|
| 263 | */ |
|---|
| 264 | void Rocket::rotateYaw(const Vector2& value) |
|---|
| 265 | { |
|---|
| 266 | ControllableEntity::rotateYaw(value); |
|---|
| 267 | |
|---|
| 268 | if( !this->isInMouseLook() ) |
|---|
| 269 | this->localAngularVelocity_.y += value.x; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | @brief |
|---|
| 274 | Rotates the Rocket around the x-axis by the amount specified by the first component of the input 2-dim vector. |
|---|
| 275 | @param value |
|---|
| 276 | The vector determining the amount of the angular movement. |
|---|
| 277 | */ |
|---|
| 278 | void Rocket::rotatePitch(const Vector2& value) |
|---|
| 279 | { |
|---|
| 280 | ControllableEntity::rotatePitch(value); |
|---|
| 281 | |
|---|
| 282 | if( !this->isInMouseLook() ) |
|---|
| 283 | this->localAngularVelocity_.x += value.x; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | /** |
|---|
| 287 | @brief |
|---|
| 288 | Rotates the Rocket around the z-axis by the amount specified by the first component of the input 2-dim vector. |
|---|
| 289 | @param value |
|---|
| 290 | The vector determining the amount of the angular movement. |
|---|
| 291 | */ |
|---|
| 292 | void Rocket::rotateRoll(const Vector2& value) |
|---|
| 293 | { |
|---|
| 294 | ControllableEntity::rotateRoll(value); |
|---|
| 295 | |
|---|
| 296 | if( !this->isInMouseLook() ) |
|---|
| 297 | this->localAngularVelocity_.z += value.x; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | } |
|---|