[3573] | 1 | |
---|
| 2 | |
---|
[4597] | 3 | /* |
---|
[3573] | 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
[4597] | 15 | co-programmer: |
---|
[3573] | 16 | */ |
---|
[5357] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
[3573] | 18 | |
---|
| 19 | #include "projectile.h" |
---|
[3618] | 20 | |
---|
[3573] | 21 | #include "world_entity.h" |
---|
[6434] | 22 | #include "world_entities/weapons/weapon.h" |
---|
[3678] | 23 | #include "model.h" |
---|
[9869] | 24 | #include "sound/resource_sound_buffer.h" |
---|
[3573] | 25 | |
---|
[9964] | 26 | #include <cmath> |
---|
| 27 | |
---|
[8362] | 28 | #include "debug.h" |
---|
[3573] | 29 | |
---|
[9869] | 30 | ObjectListDefinition(Projectile); |
---|
[3573] | 31 | |
---|
[3578] | 32 | /** |
---|
[4836] | 33 | * standard constructor |
---|
[3578] | 34 | */ |
---|
[4932] | 35 | Projectile::Projectile () : WorldEntity() |
---|
[3573] | 36 | { |
---|
[9869] | 37 | this->registerObject(this, Projectile::_objectList); |
---|
[4597] | 38 | |
---|
[4890] | 39 | this->lifeCycle = 0.0; |
---|
[5063] | 40 | this->lifeSpan = 1.0f; /* sec */ |
---|
[6078] | 41 | this->target = NULL; |
---|
[5769] | 42 | this->removeNode(); |
---|
[7084] | 43 | |
---|
[8190] | 44 | /* character attributes */ |
---|
| 45 | this->setHealth(1.0f); |
---|
[9235] | 46 | this->setDamage(1.0f); // default damage of a projectile set to 100.0 damage points |
---|
[8190] | 47 | |
---|
[9960] | 48 | this->physDamage = 0.0f; |
---|
| 49 | this->elecDamage = 0.0f; |
---|
[9656] | 50 | //this->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[3573] | 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
[3578] | 54 | /** |
---|
[4836] | 55 | * standard deconstructor |
---|
[3578] | 56 | */ |
---|
[4597] | 57 | Projectile::~Projectile () |
---|
[3573] | 58 | { |
---|
[4597] | 59 | /* |
---|
| 60 | do not delete the test projectModel, since it is pnode |
---|
| 61 | and will be cleaned out by world |
---|
[3629] | 62 | */ |
---|
| 63 | //delete this->projectileModel; |
---|
[3573] | 64 | } |
---|
| 65 | |
---|
[9960] | 66 | Projectile::Projectile (float pDamage, float eDamage, PNode* target) : WorldEntity() |
---|
| 67 | { |
---|
| 68 | this->registerObject(this, Projectile::_objectList); |
---|
[3578] | 69 | |
---|
[9960] | 70 | this->lifeCycle = 0.0; |
---|
| 71 | this->lifeSpan = 1.0f; /* sec */ |
---|
| 72 | this->removeNode(); |
---|
| 73 | |
---|
| 74 | /* character attributes */ |
---|
| 75 | this->setHealth(1.0f); |
---|
| 76 | this->setDamage(1.0f); // default damage of a projectile set to 100.0 damage points |
---|
| 77 | |
---|
| 78 | this->physDamage = pDamage; |
---|
| 79 | this->elecDamage = eDamage; |
---|
| 80 | this->target = target; |
---|
| 81 | |
---|
| 82 | //this->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void Projectile::initialize(float pDamage, float eDamage, PNode* target) |
---|
| 86 | { |
---|
| 87 | /* character attributes*/ |
---|
| 88 | this->physDamage = pDamage; |
---|
| 89 | this->elecDamage = eDamage; |
---|
| 90 | this->target = target; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
[7221] | 94 | void Projectile::loadExplosionSound(const std::string& explosionSound) |
---|
[7084] | 95 | { |
---|
[9869] | 96 | if (!explosionSound.empty()) |
---|
| 97 | this->explosionBuffer = OrxSound::ResourceSoundBuffer(explosionSound); |
---|
[7084] | 98 | else |
---|
[9869] | 99 | this->explosionBuffer = OrxSound::SoundBuffer(); |
---|
[7084] | 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
[7221] | 103 | void Projectile::loadEngineSound(const std::string& engineSound) |
---|
[7084] | 104 | { |
---|
[9869] | 105 | if (!engineSound.empty()) |
---|
| 106 | this->engineBuffer = OrxSound::ResourceSoundBuffer(engineSound); |
---|
[7084] | 107 | else |
---|
[9869] | 108 | this->engineBuffer = OrxSound::SoundBuffer(); |
---|
[7084] | 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
[6431] | 112 | void Projectile::setMinEnergy(float energyMin) |
---|
[4948] | 113 | { |
---|
| 114 | this->energyMin = energyMin; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
[3578] | 118 | /** |
---|
[4836] | 119 | * this sets the flight direction of the projectile |
---|
| 120 | * @param directin in which to flight |
---|
[3632] | 121 | |
---|
| 122 | this function will calculate a vector out of this to be used in the |
---|
| 123 | tick function |
---|
| 124 | */ |
---|
[4927] | 125 | void Projectile::setFlightDirection(const Quaternion& flightDirection) |
---|
[3632] | 126 | { |
---|
| 127 | Vector v(1, 0, 0); |
---|
[4890] | 128 | this->flightDirection = flightDirection.apply(v); |
---|
| 129 | this->flightDirection.normalize(); |
---|
[3632] | 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
[4836] | 133 | * sets the velocity vector to a spec speed |
---|
| 134 | * @param velocity: vector of the velocity |
---|
[4464] | 135 | */ |
---|
| 136 | void Projectile::setVelocity(const Vector &velocity) |
---|
| 137 | { |
---|
[4955] | 138 | //Vector offsetVel = |
---|
| 139 | this->velocity = velocity; |
---|
[9869] | 140 | // offsetVel.normalize(); |
---|
[4955] | 141 | //this->velocity += (offsetVel * 50.0); |
---|
[4464] | 142 | } |
---|
| 143 | |
---|
[5766] | 144 | |
---|
| 145 | |
---|
| 146 | void Projectile::setTarget(PNode* target) |
---|
| 147 | { |
---|
[6078] | 148 | if (this->target == NULL) |
---|
[9869] | 149 | this->target = new PNode(target, PNODE_REPARENT_ON_PARENTS_REMOVE | PNODE_REPARENT_TO_NULL | PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6078] | 150 | else |
---|
| 151 | this->target->setParent(target); |
---|
[5766] | 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
[9959] | 155 | void Projectile::collidesWith (SpaceShip* target, const Vector& location) |
---|
[9957] | 156 | { |
---|
[9959] | 157 | target->damage(this->physDamage, this->elecDamage); |
---|
| 158 | this->destroy(target); |
---|
[9957] | 159 | } |
---|
| 160 | |
---|
[9964] | 161 | |
---|
[4464] | 162 | /** |
---|
[9964] | 163 | * this function gets called by tick to calculate the new flight direction |
---|
| 164 | * @param curDirection direction vector |
---|
| 165 | * @param estTargetDir target vector, pointing to where the target will be on hit |
---|
| 166 | * @param angle = tick * turningSpeed |
---|
| 167 | * @return normalized (new) direction vector |
---|
| 168 | */ |
---|
[10035] | 169 | // Vector Projectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) |
---|
| 170 | // { |
---|
| 171 | // float tmp = angleDeg ( curDirection, estTargetDir); |
---|
| 172 | // if ( unlikely(tmp == 0) ) { return curDirection; } |
---|
| 173 | // |
---|
| 174 | // if( fabsf(angle) > fabsf(tmp) ) { angle = tmp; } |
---|
| 175 | // |
---|
| 176 | // Vector d = curDirection.cross(estTargetDir).cross(curDirection); |
---|
| 177 | // d.normalize(); |
---|
| 178 | // if( unlikely( angle == 90)) { return d; } |
---|
| 179 | // |
---|
| 180 | // Vector newDir = curDirection + d * curDirection.len() * tan (angle); |
---|
| 181 | // newDir.normalize(); |
---|
| 182 | // return newDir; |
---|
| 183 | // } |
---|
[9964] | 184 | |
---|
| 185 | |
---|
| 186 | /** |
---|
[4927] | 187 | * signal tick, time dependent things will be handled here |
---|
[6056] | 188 | * @param dt since last tick |
---|
[3578] | 189 | */ |
---|
[6056] | 190 | void Projectile::tick (float dt) |
---|
[3632] | 191 | { |
---|
[10035] | 192 | /* |
---|
| 193 | if (target != NULL){ |
---|
| 194 | float tti = this->target->getRelCoor().len() / this->getVelocity().len(); |
---|
| 195 | Vector estTargetDir = (this->target->getRelCoor() + this->target->getVelocity()) * tti; |
---|
| 196 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * dt ) * this->velocity.len(); |
---|
| 197 | Vector v = this->velocity * (dt); |
---|
| 198 | this->shiftCoor(v); |
---|
| 199 | }*/ |
---|
[3683] | 200 | |
---|
[6056] | 201 | if (this->tickLifeCycle(dt)) |
---|
[9235] | 202 | this->destroy( NULL ); |
---|
[3632] | 203 | } |
---|
[3573] | 204 | |
---|
| 205 | |
---|
[3578] | 206 | /** |
---|
[4836] | 207 | * the function gets called, when the projectile is destroyed |
---|
[3578] | 208 | */ |
---|
[9235] | 209 | void Projectile::destroy (WorldEntity* killer) |
---|
[7084] | 210 | { |
---|
[9869] | 211 | if (this->explosionBuffer.loaded()) |
---|
[7084] | 212 | this->soundSource.play(this->explosionBuffer); |
---|
| 213 | } |
---|
[9964] | 214 | |
---|