[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" |
---|
[2072] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[2662] | 41 | const float orientationGain = 100; |
---|
[2072] | 42 | CreateFactory(SpaceShip); |
---|
| 43 | |
---|
| 44 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(SpaceShip); |
---|
| 47 | |
---|
[2662] | 48 | this->primaryThrust_ = 100; |
---|
| 49 | this->auxilaryThrust_ = 30; |
---|
| 50 | this->rotationThrust_ = 10; |
---|
[2072] | 51 | |
---|
[2662] | 52 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 53 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 54 | this->bBoost_ = false; |
---|
| 55 | this->steering_ = Vector3::ZERO; |
---|
| 56 | this->engine_ = 0; |
---|
[2072] | 57 | |
---|
[7801] | 58 | this->boostPower_ = 10.0f; |
---|
| 59 | this->initialBoostPower_ = 10.0f; |
---|
| 60 | this->boostRate_ = 5.0; |
---|
| 61 | this->boostPowerRate_ = 1.0; |
---|
| 62 | this->boostCooldownDuration_ = 5.0; |
---|
| 63 | this->bBoostCooldown_ = false; |
---|
[2072] | 64 | |
---|
| 65 | this->bInvertYAxis_ = false; |
---|
| 66 | |
---|
| 67 | this->setDestroyWhenPlayerLeft(true); |
---|
| 68 | |
---|
[2662] | 69 | // SpaceShip is always a physical object per default |
---|
| 70 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
| 71 | this->setCollisionType(WorldEntity::Dynamic); |
---|
| 72 | // Get notification about collisions |
---|
| 73 | this->enableCollisionCallback(); |
---|
| 74 | |
---|
[2072] | 75 | this->setConfigValues(); |
---|
| 76 | this->registerVariables(); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | SpaceShip::~SpaceShip() |
---|
| 80 | { |
---|
[2662] | 81 | if (this->isInitialized() && this->engine_) |
---|
[5929] | 82 | this->engine_->destroy(); |
---|
[2072] | 83 | } |
---|
| 84 | |
---|
| 85 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 86 | { |
---|
| 87 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
| 88 | |
---|
[2662] | 89 | XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
| 90 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
| 91 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
| 92 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
[7801] | 93 | XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode); |
---|
| 94 | XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode); |
---|
| 95 | XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); |
---|
| 96 | XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); |
---|
[2072] | 97 | } |
---|
| 98 | |
---|
| 99 | void SpaceShip::registerVariables() |
---|
| 100 | { |
---|
[3280] | 101 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
| 102 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
| 103 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
[8223] | 104 | registerVariable(this->boostPower_, VariableDirection::ToClient); |
---|
| 105 | registerVariable(this->boostPowerRate_, VariableDirection::ToClient); |
---|
| 106 | registerVariable(this->boostRate_, VariableDirection::ToClient); |
---|
| 107 | registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient); |
---|
[2072] | 108 | } |
---|
| 109 | |
---|
| 110 | void SpaceShip::setConfigValues() |
---|
| 111 | { |
---|
| 112 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 113 | } |
---|
| 114 | |
---|
[2662] | 115 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2072] | 116 | { |
---|
[2662] | 117 | if (type != WorldEntity::Dynamic) |
---|
[2072] | 118 | { |
---|
[2662] | 119 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
| 120 | assert(false); // Only in debug mode |
---|
| 121 | return false; |
---|
[2072] | 122 | } |
---|
[2662] | 123 | else |
---|
| 124 | return true; |
---|
| 125 | } |
---|
[2072] | 126 | |
---|
[2662] | 127 | void SpaceShip::tick(float dt) |
---|
| 128 | { |
---|
[2809] | 129 | SUPER(SpaceShip, tick, dt); |
---|
[2072] | 130 | |
---|
[2662] | 131 | if (this->hasLocalController()) |
---|
[2072] | 132 | { |
---|
[2662] | 133 | /* |
---|
| 134 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); |
---|
| 135 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); |
---|
| 136 | if (this->localLinearAcceleration_.z() > 0) |
---|
| 137 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); |
---|
[2072] | 138 | else |
---|
[2662] | 139 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
| 140 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
| 141 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 142 | */ |
---|
| 143 | if (!this->isInMouseLook()) |
---|
| 144 | { |
---|
| 145 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 146 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 147 | } |
---|
[7860] | 148 | |
---|
| 149 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 150 | |
---|
[7801] | 151 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
| 152 | { |
---|
| 153 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
| 154 | } |
---|
| 155 | if(this->bBoost_) |
---|
| 156 | { |
---|
| 157 | this->boostPower_ -=this->boostRate_*dt; |
---|
| 158 | if(this->boostPower_ <= 0.0f) |
---|
| 159 | { |
---|
| 160 | this->bBoost_ = false; |
---|
| 161 | this->bBoostCooldown_ = true; |
---|
| 162 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
| 163 | } |
---|
| 164 | } |
---|
[2072] | 165 | } |
---|
| 166 | } |
---|
[7860] | 167 | |
---|
[7801] | 168 | void SpaceShip::boostCooledDown(void) |
---|
| 169 | { |
---|
| 170 | this->bBoostCooldown_ = false; |
---|
| 171 | } |
---|
[2072] | 172 | |
---|
| 173 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
| 174 | { |
---|
[2662] | 175 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
| 176 | this->steering_.z = -value.x; |
---|
[2072] | 177 | } |
---|
| 178 | |
---|
| 179 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
| 180 | { |
---|
[2662] | 181 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
| 182 | this->steering_.x = value.x; |
---|
[2072] | 183 | } |
---|
| 184 | |
---|
| 185 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
| 186 | { |
---|
[2662] | 187 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
| 188 | this->steering_.y = value.x; |
---|
[2072] | 189 | } |
---|
| 190 | |
---|
| 191 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
| 192 | { |
---|
[3039] | 193 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
[2662] | 194 | |
---|
| 195 | Pawn::rotateYaw(value); |
---|
[2072] | 196 | } |
---|
| 197 | |
---|
| 198 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
| 199 | { |
---|
[2662] | 200 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
| 201 | |
---|
| 202 | Pawn::rotatePitch(value); |
---|
[2072] | 203 | } |
---|
| 204 | |
---|
| 205 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
| 206 | { |
---|
[2662] | 207 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
| 208 | |
---|
| 209 | Pawn::rotateRoll(value); |
---|
[2072] | 210 | } |
---|
[7860] | 211 | |
---|
[2072] | 212 | void SpaceShip::fire() |
---|
| 213 | { |
---|
| 214 | } |
---|
[2662] | 215 | |
---|
[8223] | 216 | /** |
---|
| 217 | @brief |
---|
| 218 | Starts or stops boosting. |
---|
| 219 | @param bBoost |
---|
| 220 | Whether to start or stop boosting. |
---|
| 221 | */ |
---|
| 222 | void SpaceShip::boost(bool bBoost) |
---|
[2662] | 223 | { |
---|
[8223] | 224 | if(bBoost && !this->bBoostCooldown_) |
---|
[7801] | 225 | this->bBoost_ = true; |
---|
[8223] | 226 | if(!bBoost) |
---|
| 227 | this->bBoost_ = false; |
---|
[2662] | 228 | } |
---|
| 229 | |
---|
| 230 | void SpaceShip::loadEngineTemplate() |
---|
| 231 | { |
---|
[6417] | 232 | if (!this->enginetemplate_.empty()) |
---|
[2662] | 233 | { |
---|
| 234 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
| 235 | |
---|
| 236 | if (temp) |
---|
| 237 | { |
---|
| 238 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
| 239 | |
---|
| 240 | if (identifier) |
---|
| 241 | { |
---|
| 242 | BaseObject* object = identifier->fabricate(this); |
---|
[3325] | 243 | this->engine_ = orxonox_cast<Engine*>(object); |
---|
[2662] | 244 | |
---|
| 245 | if (this->engine_) |
---|
| 246 | { |
---|
| 247 | this->engine_->addTemplate(temp); |
---|
| 248 | this->engine_->addToSpaceShip(this); |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | { |
---|
[5929] | 252 | object->destroy(); |
---|
[2662] | 253 | } |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | void SpaceShip::setEngine(Engine* engine) |
---|
| 260 | { |
---|
| 261 | this->engine_ = engine; |
---|
| 262 | if (engine && engine->getShip() != this) |
---|
| 263 | engine->addToSpaceShip(this); |
---|
| 264 | } |
---|
[6709] | 265 | |
---|
[7547] | 266 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
[6709] | 267 | { |
---|
[6711] | 268 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
| 269 | list->push_back(this->engine_); |
---|
[6709] | 270 | return list; |
---|
| 271 | } |
---|
[2072] | 272 | } |
---|