[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" |
---|
[8490] | 40 | #include "util/Math.h" |
---|
[2072] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[2662] | 44 | const float orientationGain = 100; |
---|
[2072] | 45 | CreateFactory(SpaceShip); |
---|
| 46 | |
---|
| 47 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) |
---|
| 48 | { |
---|
| 49 | RegisterObject(SpaceShip); |
---|
| 50 | |
---|
[2662] | 51 | this->primaryThrust_ = 100; |
---|
| 52 | this->auxilaryThrust_ = 30; |
---|
| 53 | this->rotationThrust_ = 10; |
---|
[2072] | 54 | |
---|
[2662] | 55 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 56 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 57 | this->bBoost_ = 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(); |
---|
[8574] | 80 | |
---|
[8575] | 81 | Camera* camera = CameraManager::getInstance().getActiveCamera(); |
---|
| 82 | this->cameraOriginalPosition_ = camera->getPosition(); |
---|
| 83 | this->cameraOriginalOrientation_ = camera->getOrientation(); |
---|
[8191] | 84 | |
---|
[8574] | 85 | this->shakeFrequency_ = 15; |
---|
| 86 | this->shakeAmplitude_ = 5; |
---|
| 87 | this->shakeDt_ = 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); |
---|
[8574] | 108 | XMLPortParamVariable(SpaceShip, "shakeFrequency", shakeFrequency_, xmlelement, mode); |
---|
[8577] | 109 | XMLPortParamVariable(SpaceShip, "shakeAmplitude", shakeAmplitude_, xmlelement, mode); |
---|
[2072] | 110 | } |
---|
| 111 | |
---|
| 112 | void SpaceShip::registerVariables() |
---|
| 113 | { |
---|
[3280] | 114 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
| 115 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
| 116 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
[8577] | 117 | // TODO: Synchronization of boost needed? |
---|
[8379] | 118 | registerVariable(this->boostPower_, VariableDirection::ToClient); |
---|
| 119 | registerVariable(this->boostPowerRate_, VariableDirection::ToClient); |
---|
| 120 | registerVariable(this->boostRate_, VariableDirection::ToClient); |
---|
| 121 | registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient); |
---|
[8574] | 122 | registerVariable(this->shakeFrequency_, VariableDirection::ToClient); |
---|
[8577] | 123 | registerVariable(this->shakeAmplitude_, VariableDirection::ToClient); |
---|
[2072] | 124 | } |
---|
| 125 | |
---|
| 126 | void SpaceShip::setConfigValues() |
---|
| 127 | { |
---|
| 128 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 129 | } |
---|
| 130 | |
---|
[2662] | 131 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2072] | 132 | { |
---|
[2662] | 133 | if (type != WorldEntity::Dynamic) |
---|
[2072] | 134 | { |
---|
[2662] | 135 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
| 136 | assert(false); // Only in debug mode |
---|
| 137 | return false; |
---|
[2072] | 138 | } |
---|
[2662] | 139 | else |
---|
| 140 | return true; |
---|
| 141 | } |
---|
[2072] | 142 | |
---|
[2662] | 143 | void SpaceShip::tick(float dt) |
---|
| 144 | { |
---|
[2809] | 145 | SUPER(SpaceShip, tick, dt); |
---|
[2072] | 146 | |
---|
[2662] | 147 | if (this->hasLocalController()) |
---|
[2072] | 148 | { |
---|
[8574] | 149 | |
---|
[2662] | 150 | /* |
---|
| 151 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); |
---|
| 152 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); |
---|
| 153 | if (this->localLinearAcceleration_.z() > 0) |
---|
| 154 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); |
---|
[2072] | 155 | else |
---|
[2662] | 156 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
| 157 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
| 158 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 159 | */ |
---|
| 160 | if (!this->isInMouseLook()) |
---|
| 161 | { |
---|
| 162 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 163 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 164 | } |
---|
[7860] | 165 | |
---|
| 166 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 167 | |
---|
[7801] | 168 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
| 169 | { |
---|
| 170 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
| 171 | } |
---|
[8191] | 172 | |
---|
[7801] | 173 | if(this->bBoost_) |
---|
| 174 | { |
---|
| 175 | this->boostPower_ -=this->boostRate_*dt; |
---|
| 176 | if(this->boostPower_ <= 0.0f) |
---|
| 177 | { |
---|
[8427] | 178 | this->boost(false); |
---|
[7801] | 179 | this->bBoostCooldown_ = true; |
---|
| 180 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
[8574] | 181 | |
---|
[7801] | 182 | } |
---|
[8574] | 183 | |
---|
[8575] | 184 | shakeCamera(dt); |
---|
[8490] | 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
[8191] | 188 | |
---|
[2072] | 189 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
| 190 | { |
---|
[2662] | 191 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
| 192 | this->steering_.z = -value.x; |
---|
[2072] | 193 | } |
---|
| 194 | |
---|
| 195 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
| 196 | { |
---|
[2662] | 197 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
| 198 | this->steering_.x = value.x; |
---|
[2072] | 199 | } |
---|
| 200 | |
---|
| 201 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
| 202 | { |
---|
[2662] | 203 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
| 204 | this->steering_.y = value.x; |
---|
[2072] | 205 | } |
---|
| 206 | |
---|
| 207 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
| 208 | { |
---|
[3039] | 209 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
[2662] | 210 | |
---|
| 211 | Pawn::rotateYaw(value); |
---|
[2072] | 212 | } |
---|
| 213 | |
---|
| 214 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
| 215 | { |
---|
[2662] | 216 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
| 217 | |
---|
| 218 | Pawn::rotatePitch(value); |
---|
[2072] | 219 | } |
---|
| 220 | |
---|
| 221 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
| 222 | { |
---|
[2662] | 223 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
| 224 | |
---|
| 225 | Pawn::rotateRoll(value); |
---|
[2072] | 226 | } |
---|
[7860] | 227 | |
---|
[2072] | 228 | void SpaceShip::fire() |
---|
| 229 | { |
---|
| 230 | } |
---|
[2662] | 231 | |
---|
[8379] | 232 | /** |
---|
| 233 | @brief |
---|
| 234 | Starts or stops boosting. |
---|
| 235 | @param bBoost |
---|
| 236 | Whether to start or stop boosting. |
---|
| 237 | */ |
---|
| 238 | void SpaceShip::boost(bool bBoost) |
---|
[8574] | 239 | { |
---|
[8379] | 240 | if(bBoost && !this->bBoostCooldown_) |
---|
[8574] | 241 | { |
---|
| 242 | //COUT(0) << "Boost startet!\n"; |
---|
[7801] | 243 | this->bBoost_ = true; |
---|
[8574] | 244 | } |
---|
[8379] | 245 | if(!bBoost) |
---|
[8574] | 246 | { |
---|
| 247 | //COUT(0) << "Boost stoppt\n"; |
---|
| 248 | this->resetCamera(); |
---|
[8379] | 249 | this->bBoost_ = false; |
---|
[8574] | 250 | } |
---|
[2662] | 251 | } |
---|
[8541] | 252 | |
---|
| 253 | void SpaceShip::boostCooledDown(void) |
---|
| 254 | { |
---|
[8574] | 255 | this->bBoostCooldown_ = false; |
---|
[8541] | 256 | } |
---|
| 257 | |
---|
| 258 | void SpaceShip::shakeCamera(float dt) |
---|
| 259 | { |
---|
[8574] | 260 | //make sure the ship is only shaking if it's moving |
---|
| 261 | if (this->getVelocity().squaredLength() > 80) |
---|
| 262 | { |
---|
| 263 | this->shakeDt_ += dt; |
---|
| 264 | |
---|
| 265 | int frequency = this->shakeFrequency_ * (this->getVelocity().squaredLength()); |
---|
| 266 | |
---|
| 267 | if (this->shakeDt_ >= 1 /(frequency)) |
---|
| 268 | { |
---|
| 269 | this->shakeDt_ -= 1/(frequency); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | Degree angle = Degree(sin(this->shakeDt_ * 2* math::pi * frequency) * this->shakeAmplitude_); |
---|
| 273 | |
---|
| 274 | //COUT(0) << "Angle: " << angle << std::endl; |
---|
| 275 | Camera* c = this->getCamera(); |
---|
[2662] | 276 | |
---|
[8574] | 277 | //Shaking Camera effect |
---|
| 278 | if (c != 0) |
---|
| 279 | { |
---|
| 280 | c->setOrientation(Vector3::UNIT_X, angle); |
---|
| 281 | } |
---|
| 282 | } |
---|
[8541] | 283 | } |
---|
| 284 | |
---|
| 285 | void SpaceShip::resetCamera() |
---|
| 286 | { |
---|
[8574] | 287 | |
---|
| 288 | //COUT(0) << "Resetting camera\n"; |
---|
| 289 | Camera *c = this->getCamera(); |
---|
| 290 | |
---|
| 291 | if (c == 0) |
---|
| 292 | { |
---|
| 293 | COUT(2) << "Failed to reset camera!"; |
---|
| 294 | return; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | shakeDt_ = 0; |
---|
| 298 | // |
---|
[8575] | 299 | c->setPosition(this->cameraOriginalPosition_); |
---|
| 300 | c->setOrientation(this->cameraOriginalOrientation_); |
---|
[8541] | 301 | } |
---|
| 302 | |
---|
[2662] | 303 | void SpaceShip::loadEngineTemplate() |
---|
| 304 | { |
---|
[6417] | 305 | if (!this->enginetemplate_.empty()) |
---|
[2662] | 306 | { |
---|
| 307 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
| 308 | |
---|
| 309 | if (temp) |
---|
| 310 | { |
---|
| 311 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
| 312 | |
---|
| 313 | if (identifier) |
---|
| 314 | { |
---|
| 315 | BaseObject* object = identifier->fabricate(this); |
---|
[3325] | 316 | this->engine_ = orxonox_cast<Engine*>(object); |
---|
[2662] | 317 | |
---|
| 318 | if (this->engine_) |
---|
| 319 | { |
---|
| 320 | this->engine_->addTemplate(temp); |
---|
| 321 | this->engine_->addToSpaceShip(this); |
---|
| 322 | } |
---|
| 323 | else |
---|
| 324 | { |
---|
[5929] | 325 | object->destroy(); |
---|
[2662] | 326 | } |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | void SpaceShip::setEngine(Engine* engine) |
---|
| 333 | { |
---|
| 334 | this->engine_ = engine; |
---|
| 335 | if (engine && engine->getShip() != this) |
---|
| 336 | engine->addToSpaceShip(this); |
---|
| 337 | } |
---|
[6709] | 338 | |
---|
[7547] | 339 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
[6709] | 340 | { |
---|
[6711] | 341 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
| 342 | list->push_back(this->engine_); |
---|
[6709] | 343 | return list; |
---|
| 344 | } |
---|
[8254] | 345 | |
---|
[8541] | 346 | |
---|
[2072] | 347 | } |
---|