[2072] | 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 "SpaceShip.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <BulletDynamics/Dynamics/btRigidBody.h> |
---|
[2662] | 32 | |
---|
[2072] | 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/ConfigValueIncludes.h" |
---|
[2662] | 35 | #include "core/Template.h" |
---|
[2072] | 36 | #include "core/XMLPort.h" |
---|
[5735] | 37 | #include "items/Engine.h" |
---|
[8138] | 38 | #include "graphics/Camera.h" |
---|
| 39 | #include "CameraManager.h" |
---|
[2072] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[2662] | 43 | const float orientationGain = 100; |
---|
[2072] | 44 | CreateFactory(SpaceShip); |
---|
| 45 | |
---|
| 46 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) |
---|
| 47 | { |
---|
| 48 | RegisterObject(SpaceShip); |
---|
| 49 | |
---|
[2662] | 50 | this->primaryThrust_ = 100; |
---|
| 51 | this->auxilaryThrust_ = 30; |
---|
| 52 | this->rotationThrust_ = 10; |
---|
[2072] | 53 | |
---|
[2662] | 54 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 55 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 56 | this->bBoost_ = false; |
---|
| 57 | this->bPermanentBoost_ = false; |
---|
| 58 | this->steering_ = Vector3::ZERO; |
---|
| 59 | this->engine_ = 0; |
---|
[2072] | 60 | |
---|
[7801] | 61 | this->boostPower_ = 10.0f; |
---|
| 62 | this->initialBoostPower_ = 10.0f; |
---|
| 63 | this->boostRate_ = 5.0; |
---|
| 64 | this->boostPowerRate_ = 1.0; |
---|
| 65 | this->boostCooldownDuration_ = 5.0; |
---|
| 66 | this->bBoostCooldown_ = false; |
---|
[2072] | 67 | |
---|
| 68 | this->bInvertYAxis_ = false; |
---|
| 69 | |
---|
| 70 | this->setDestroyWhenPlayerLeft(true); |
---|
| 71 | |
---|
[2662] | 72 | // SpaceShip is always a physical object per default |
---|
| 73 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
| 74 | this->setCollisionType(WorldEntity::Dynamic); |
---|
| 75 | // Get notification about collisions |
---|
| 76 | this->enableCollisionCallback(); |
---|
| 77 | |
---|
[2072] | 78 | this->setConfigValues(); |
---|
| 79 | this->registerVariables(); |
---|
[8138] | 80 | |
---|
| 81 | Camera* c = CameraManager::getInstance().getActiveCamera(); |
---|
| 82 | this->cameraOriginalPosition = c->getPosition(); |
---|
| 83 | this->cameraOriginalOrientation = c->getOrientation(); |
---|
[8191] | 84 | |
---|
| 85 | this->shakeFrequency_ = 100; |
---|
| 86 | this->shakeAmplitude_ = 40; |
---|
| 87 | this->shakeDeltaTime_ = 0; |
---|
[2072] | 88 | } |
---|
| 89 | |
---|
| 90 | SpaceShip::~SpaceShip() |
---|
| 91 | { |
---|
[2662] | 92 | if (this->isInitialized() && this->engine_) |
---|
[5929] | 93 | this->engine_->destroy(); |
---|
[2072] | 94 | } |
---|
| 95 | |
---|
| 96 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 97 | { |
---|
| 98 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
| 99 | |
---|
[2662] | 100 | XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
| 101 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
| 102 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
| 103 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
[7801] | 104 | XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode); |
---|
| 105 | XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode); |
---|
| 106 | XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); |
---|
| 107 | XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); |
---|
[2072] | 108 | } |
---|
| 109 | |
---|
| 110 | void SpaceShip::registerVariables() |
---|
| 111 | { |
---|
[3280] | 112 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
| 113 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
| 114 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
[2072] | 115 | } |
---|
| 116 | |
---|
| 117 | void SpaceShip::setConfigValues() |
---|
| 118 | { |
---|
| 119 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 120 | } |
---|
| 121 | |
---|
[2662] | 122 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2072] | 123 | { |
---|
[2662] | 124 | if (type != WorldEntity::Dynamic) |
---|
[2072] | 125 | { |
---|
[2662] | 126 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
| 127 | assert(false); // Only in debug mode |
---|
| 128 | return false; |
---|
[2072] | 129 | } |
---|
[2662] | 130 | else |
---|
| 131 | return true; |
---|
| 132 | } |
---|
[2072] | 133 | |
---|
[2662] | 134 | void SpaceShip::tick(float dt) |
---|
| 135 | { |
---|
[2809] | 136 | SUPER(SpaceShip, tick, dt); |
---|
[2072] | 137 | |
---|
[2662] | 138 | if (this->hasLocalController()) |
---|
[2072] | 139 | { |
---|
[2662] | 140 | /* |
---|
| 141 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); |
---|
| 142 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); |
---|
| 143 | if (this->localLinearAcceleration_.z() > 0) |
---|
| 144 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); |
---|
[2072] | 145 | else |
---|
[2662] | 146 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
| 147 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
| 148 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 149 | */ |
---|
| 150 | if (!this->isInMouseLook()) |
---|
| 151 | { |
---|
| 152 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 153 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 154 | } |
---|
[7860] | 155 | |
---|
| 156 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 157 | |
---|
[7801] | 158 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
| 159 | { |
---|
| 160 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
| 161 | } |
---|
[8191] | 162 | |
---|
| 163 | Camera* c = CameraManager::getInstance().getActiveCamera(); |
---|
[7801] | 164 | if(this->bBoost_) |
---|
| 165 | { |
---|
| 166 | this->boostPower_ -=this->boostRate_*dt; |
---|
| 167 | if(this->boostPower_ <= 0.0f) |
---|
| 168 | { |
---|
| 169 | this->bBoost_ = false; |
---|
| 170 | this->bBoostCooldown_ = true; |
---|
| 171 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
[8138] | 172 | |
---|
[7801] | 173 | } |
---|
[8138] | 174 | else |
---|
| 175 | { |
---|
[8191] | 176 | this->shakeDeltaTime_ += dt; |
---|
| 177 | |
---|
| 178 | //Shaking Camera effect |
---|
[8138] | 179 | if (c != 0) |
---|
| 180 | { |
---|
[8191] | 181 | |
---|
| 182 | if (c->getAngularVelocity() == Vector3(0,0,0)) |
---|
| 183 | { |
---|
| 184 | c->setAngularVelocity(Vector3(2,0,0)); |
---|
| 185 | this->shakeDeltaTime_ = 0; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | //toggle the rotation |
---|
| 190 | if (1/(this->shakeFrequency_) <= shakeDeltaTime_) |
---|
| 191 | { |
---|
| 192 | c->setAngularVelocity(-c->getAngularVelocity()); |
---|
| 193 | shakeDeltaTime_ = 0; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | /* |
---|
| 198 | COUT(1) << "Time since change: " << shakeDeltaTime_ << std::endl; |
---|
| 199 | COUT(1) << "Rotation Rate: " << c->getRotationRate() << std::endl; |
---|
| 200 | COUT(1) << "Angular Velocity: " << c->getAngularVelocity().length(); |
---|
| 201 | COUT(1) << std::endl; |
---|
| 202 | */ |
---|
[8138] | 203 | } |
---|
| 204 | } |
---|
[7801] | 205 | } |
---|
[8191] | 206 | else |
---|
| 207 | { |
---|
| 208 | //reset the camera, if the boost is not active |
---|
| 209 | //TODO: don't call this every tick |
---|
| 210 | c->setAngularVelocity(Vector3(0,0,0)); |
---|
| 211 | c->setPosition(this->cameraOriginalPosition); |
---|
| 212 | c->setOrientation(this->cameraOriginalOrientation); |
---|
| 213 | } |
---|
[2072] | 214 | } |
---|
| 215 | } |
---|
[7860] | 216 | |
---|
[7801] | 217 | void SpaceShip::boostCooledDown(void) |
---|
| 218 | { |
---|
| 219 | this->bBoostCooldown_ = false; |
---|
| 220 | } |
---|
[2072] | 221 | |
---|
| 222 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
| 223 | { |
---|
[2662] | 224 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
| 225 | this->steering_.z = -value.x; |
---|
[2072] | 226 | } |
---|
| 227 | |
---|
| 228 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
| 229 | { |
---|
[2662] | 230 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
| 231 | this->steering_.x = value.x; |
---|
[2072] | 232 | } |
---|
| 233 | |
---|
| 234 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
| 235 | { |
---|
[2662] | 236 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
| 237 | this->steering_.y = value.x; |
---|
[2072] | 238 | } |
---|
| 239 | |
---|
| 240 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
| 241 | { |
---|
[3039] | 242 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
[2662] | 243 | |
---|
| 244 | Pawn::rotateYaw(value); |
---|
[2072] | 245 | } |
---|
| 246 | |
---|
| 247 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
| 248 | { |
---|
[2662] | 249 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
| 250 | |
---|
| 251 | Pawn::rotatePitch(value); |
---|
[2072] | 252 | } |
---|
| 253 | |
---|
| 254 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
| 255 | { |
---|
[2662] | 256 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
| 257 | |
---|
| 258 | Pawn::rotateRoll(value); |
---|
[2072] | 259 | } |
---|
[7860] | 260 | |
---|
[7801] | 261 | // TODO: something seems to call this function every tick, could probably handled a little more efficiently! |
---|
| 262 | void SpaceShip::setBoost(bool bBoost) |
---|
| 263 | { |
---|
| 264 | if(bBoost == this->bBoost_) |
---|
| 265 | return; |
---|
[7860] | 266 | |
---|
[7801] | 267 | if(bBoost) |
---|
| 268 | this->boost(); |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | this->bBoost_ = false; |
---|
| 272 | } |
---|
| 273 | } |
---|
[2072] | 274 | |
---|
| 275 | void SpaceShip::fire() |
---|
| 276 | { |
---|
| 277 | } |
---|
[2662] | 278 | |
---|
| 279 | void SpaceShip::boost() |
---|
| 280 | { |
---|
[7801] | 281 | if(!this->bBoostCooldown_) |
---|
| 282 | this->bBoost_ = true; |
---|
[2662] | 283 | } |
---|
| 284 | |
---|
| 285 | void SpaceShip::loadEngineTemplate() |
---|
| 286 | { |
---|
[6417] | 287 | if (!this->enginetemplate_.empty()) |
---|
[2662] | 288 | { |
---|
| 289 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
| 290 | |
---|
| 291 | if (temp) |
---|
| 292 | { |
---|
| 293 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
| 294 | |
---|
| 295 | if (identifier) |
---|
| 296 | { |
---|
| 297 | BaseObject* object = identifier->fabricate(this); |
---|
[3325] | 298 | this->engine_ = orxonox_cast<Engine*>(object); |
---|
[2662] | 299 | |
---|
| 300 | if (this->engine_) |
---|
| 301 | { |
---|
| 302 | this->engine_->addTemplate(temp); |
---|
| 303 | this->engine_->addToSpaceShip(this); |
---|
| 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
[5929] | 307 | object->destroy(); |
---|
[2662] | 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | void SpaceShip::setEngine(Engine* engine) |
---|
| 315 | { |
---|
| 316 | this->engine_ = engine; |
---|
| 317 | if (engine && engine->getShip() != this) |
---|
| 318 | engine->addToSpaceShip(this); |
---|
| 319 | } |
---|
[6709] | 320 | |
---|
[7547] | 321 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
[6709] | 322 | { |
---|
[6711] | 323 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
| 324 | list->push_back(this->engine_); |
---|
[6709] | 325 | return list; |
---|
| 326 | } |
---|
[2072] | 327 | } |
---|