[10336] | 1 | /* |
---|
| 2 | * GravityBomb.cc |
---|
| 3 | * |
---|
| 4 | * Created on: Mar 26, 2015 |
---|
[10455] | 5 | * Author: Manuel Eggimann |
---|
[10336] | 6 | */ |
---|
| 7 | #include "GravityBomb.h" |
---|
[10369] | 8 | #include "graphics/Model.h" |
---|
[10336] | 9 | |
---|
| 10 | |
---|
| 11 | namespace orxonox{ |
---|
| 12 | RegisterClass(GravityBomb); |
---|
| 13 | |
---|
[10455] | 14 | const float GravityBomb::LIFETIME = 5; ///< The gravity bomb lifetime in seconds. |
---|
[10435] | 15 | |
---|
[10336] | 16 | GravityBomb::GravityBomb(Context* context): |
---|
[10341] | 17 | BasicProjectile(), |
---|
| 18 | MovableEntity(context), |
---|
| 19 | RadarViewable(this,static_cast<WorldEntity*>(this)) |
---|
[10336] | 20 | { |
---|
| 21 | RegisterObject(GravityBomb); |
---|
| 22 | |
---|
[10391] | 23 | this->setMass(10.0); |
---|
| 24 | this->isDetonated_ = false; |
---|
[10341] | 25 | if (GameMode::isMaster()) |
---|
| 26 | { |
---|
[10369] | 27 | //Define CollisionType of the bomb |
---|
[10435] | 28 | this->timeToLife_= LIFETIME; |
---|
[10369] | 29 | this->setCollisionResponse(false); |
---|
[10391] | 30 | this->setCollisionType(WorldEntity::Dynamic); |
---|
[10341] | 31 | this->enableCollisionCallback(); |
---|
[10336] | 32 | |
---|
[10341] | 33 | //Add Collision Shape |
---|
| 34 | SphereCollisionShape* collisionShape = new SphereCollisionShape(context); |
---|
[10369] | 35 | collisionShape->setRadius(1.0); |
---|
[10341] | 36 | this->attachCollisionShape(collisionShape); |
---|
| 37 | |
---|
| 38 | //Create Bomb Model |
---|
[10435] | 39 | Model* rocketModel = new Model(this->getContext()); |
---|
| 40 | rocketModel->setMeshSource("GravityBombRocket.mesh"); //Demo Model from SimpleRocket |
---|
| 41 | rocketModel->scale(3.0f); |
---|
| 42 | this->attach(rocketModel); |
---|
[10455] | 43 | //Add second model because the bomb consists of the bomb and attached rockets (2 separate models) |
---|
[10435] | 44 | Model* bombModel = new Model(this->getContext()); |
---|
| 45 | bombModel->setMeshSource("GravityBomb.mesh"); //Demo Model from SimpleRocket |
---|
| 46 | bombModel->scale(3.0f); |
---|
| 47 | this->attach(bombModel); |
---|
| 48 | |
---|
[10455] | 49 | //Add particle effect to the flying rockets. |
---|
| 50 | ParticleEmitter* fire = new ParticleEmitter(this->getContext()); |
---|
| 51 | fire->setOrientation(this->getOrientation()); |
---|
| 52 | fire->setSource("Orxonox/simplerocketfire"); |
---|
| 53 | this->attach(fire); |
---|
| 54 | |
---|
| 55 | //Add sound effect while the bomb is flying. |
---|
| 56 | WorldSound* bombSound = new WorldSound(context); |
---|
| 57 | bombSound->setSource("sounds/GravityBombFlight.ogg"); |
---|
| 58 | bombSound->setLooping(true); |
---|
| 59 | bombSound->setVolume(1.0); |
---|
| 60 | this->attach(bombSound); |
---|
| 61 | bombSound->play(); |
---|
[10341] | 62 | } |
---|
[10336] | 63 | } |
---|
| 64 | |
---|
| 65 | GravityBomb::~GravityBomb(){} |
---|
| 66 | |
---|
| 67 | void GravityBomb::tick(float dt) |
---|
| 68 | { |
---|
[10391] | 69 | timeToLife_ -= dt; |
---|
| 70 | if(timeToLife_ < 0) |
---|
[10341] | 71 | { |
---|
[10455] | 72 | //orxout(debug_output) << "bomb has stoped moving" <<endl; |
---|
| 73 | setVelocity(Vector3::ZERO); //Stop the bomb. |
---|
[10436] | 74 | isDetonated_ = true; |
---|
[10341] | 75 | } |
---|
[10369] | 76 | else |
---|
| 77 | { |
---|
[10455] | 78 | //orxout(debug_output)<< "Time to live:" << timeToLife_ <<endl; |
---|
| 79 | destroyCheck(); //Every BasicProjectil has to call this method in each tick. |
---|
[10369] | 80 | } |
---|
[10391] | 81 | if(isDetonated_) detonate(); |
---|
[10436] | 82 | else SUPER(GravityBomb, tick, dt); |
---|
[10336] | 83 | } |
---|
| 84 | |
---|
| 85 | bool GravityBomb::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* cs, btManifoldPoint& contactPoint) |
---|
| 86 | { |
---|
[10455] | 87 | if(otherObject != getShooter()) //Ensure that the bomb cannot collide with its shooter. |
---|
[10391] | 88 | { |
---|
| 89 | orxout(debug_output) << "collides" << endl; |
---|
| 90 | processCollision(otherObject, contactPoint,cs); |
---|
| 91 | isDetonated_ = true; |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | else{ |
---|
[10455] | 95 | //orxout(debug_output) << "collided with shooter. Has no effect..." << endl; |
---|
[10391] | 96 | return false; |
---|
| 97 | } |
---|
[10336] | 98 | } |
---|
[10341] | 99 | |
---|
| 100 | void GravityBomb::detonate() |
---|
| 101 | { |
---|
[10455] | 102 | //Create the GravityBombField and destroy the Projectil. |
---|
[10341] | 103 | GravityBombField* field = new GravityBombField(this->getContext()); |
---|
[10435] | 104 | field->setShooter(this->getShooter()); |
---|
[10391] | 105 | field->setPosition(getPosition()); |
---|
[10455] | 106 | //orxout(debug_output) << "detonating. Creating GravityBombField." <<endl; |
---|
| 107 | //orxout(debug_output) << "Field is at Position: " << getPosition() << endl; |
---|
[10369] | 108 | this->destroy(); |
---|
[10341] | 109 | } |
---|
[10336] | 110 | } |
---|
| 111 | |
---|
| 112 | |
---|