[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "SpaceShip.h" |
---|
| 31 | |
---|
[2662] | 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 33 | |
---|
| 34 | #include "util/Math.h" |
---|
| 35 | #include "util/Exception.h" |
---|
[2072] | 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/ConfigValueIncludes.h" |
---|
[2662] | 38 | #include "core/Template.h" |
---|
[2072] | 39 | #include "core/XMLPort.h" |
---|
[2662] | 40 | #include "objects/items/Engine.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->bPermanentBoost_ = false; |
---|
| 59 | this->steering_ = Vector3::ZERO; |
---|
| 60 | this->engine_ = 0; |
---|
[2072] | 61 | |
---|
| 62 | |
---|
| 63 | this->bInvertYAxis_ = false; |
---|
| 64 | |
---|
| 65 | this->setDestroyWhenPlayerLeft(true); |
---|
| 66 | |
---|
[2662] | 67 | // SpaceShip is always a physical object per default |
---|
| 68 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
| 69 | this->setCollisionType(WorldEntity::Dynamic); |
---|
| 70 | // Get notification about collisions |
---|
| 71 | this->enableCollisionCallback(); |
---|
| 72 | |
---|
[2072] | 73 | this->setConfigValues(); |
---|
| 74 | this->registerVariables(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | SpaceShip::~SpaceShip() |
---|
| 78 | { |
---|
[2662] | 79 | if (this->isInitialized() && this->engine_) |
---|
| 80 | delete this->engine_; |
---|
[2072] | 81 | } |
---|
| 82 | |
---|
| 83 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 84 | { |
---|
| 85 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
| 86 | |
---|
[2662] | 87 | XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
| 88 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
| 89 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
| 90 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
[2072] | 91 | } |
---|
| 92 | |
---|
| 93 | void SpaceShip::registerVariables() |
---|
| 94 | { |
---|
[2662] | 95 | registerVariable(this->primaryThrust_, variableDirection::toclient); |
---|
| 96 | registerVariable(this->auxilaryThrust_, variableDirection::toclient); |
---|
| 97 | registerVariable(this->rotationThrust_, variableDirection::toclient); |
---|
[2072] | 98 | } |
---|
| 99 | |
---|
| 100 | void SpaceShip::setConfigValues() |
---|
| 101 | { |
---|
| 102 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 103 | } |
---|
| 104 | |
---|
[2662] | 105 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2072] | 106 | { |
---|
[2662] | 107 | if (type != WorldEntity::Dynamic) |
---|
[2072] | 108 | { |
---|
[2662] | 109 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
| 110 | assert(false); // Only in debug mode |
---|
| 111 | return false; |
---|
[2072] | 112 | } |
---|
[2662] | 113 | else |
---|
| 114 | return true; |
---|
| 115 | } |
---|
[2072] | 116 | |
---|
[2662] | 117 | void SpaceShip::tick(float dt) |
---|
| 118 | { |
---|
[2809] | 119 | SUPER(SpaceShip, tick, dt); |
---|
[2072] | 120 | |
---|
[2662] | 121 | if (this->hasLocalController()) |
---|
[2072] | 122 | { |
---|
[2662] | 123 | /* |
---|
| 124 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); |
---|
| 125 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); |
---|
| 126 | if (this->localLinearAcceleration_.z() > 0) |
---|
| 127 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); |
---|
[2072] | 128 | else |
---|
[2662] | 129 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
| 130 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
| 131 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 132 | */ |
---|
| 133 | if (!this->isInMouseLook()) |
---|
| 134 | { |
---|
| 135 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 136 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 137 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 138 | } |
---|
[2072] | 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
| 143 | { |
---|
[2662] | 144 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
| 145 | this->steering_.z = -value.x; |
---|
[2072] | 146 | } |
---|
| 147 | |
---|
| 148 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
| 149 | { |
---|
[2662] | 150 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
| 151 | this->steering_.x = value.x; |
---|
[2072] | 152 | } |
---|
| 153 | |
---|
| 154 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
| 155 | { |
---|
[2662] | 156 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
| 157 | this->steering_.y = value.x; |
---|
[2072] | 158 | } |
---|
| 159 | |
---|
| 160 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
| 161 | { |
---|
[2662] | 162 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x); |
---|
| 163 | |
---|
| 164 | Pawn::rotateYaw(value); |
---|
[2072] | 165 | } |
---|
| 166 | |
---|
| 167 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
| 168 | { |
---|
[2662] | 169 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
| 170 | |
---|
| 171 | Pawn::rotatePitch(value); |
---|
[2072] | 172 | } |
---|
| 173 | |
---|
| 174 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
| 175 | { |
---|
[2662] | 176 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
| 177 | |
---|
| 178 | Pawn::rotateRoll(value); |
---|
[2072] | 179 | } |
---|
| 180 | |
---|
| 181 | void SpaceShip::fire() |
---|
| 182 | { |
---|
| 183 | } |
---|
[2662] | 184 | |
---|
| 185 | void SpaceShip::boost() |
---|
| 186 | { |
---|
| 187 | this->bBoost_ = true; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void SpaceShip::loadEngineTemplate() |
---|
| 191 | { |
---|
| 192 | if (this->enginetemplate_ != "") |
---|
| 193 | { |
---|
| 194 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
| 195 | |
---|
| 196 | if (temp) |
---|
| 197 | { |
---|
| 198 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
| 199 | |
---|
| 200 | if (identifier) |
---|
| 201 | { |
---|
| 202 | BaseObject* object = identifier->fabricate(this); |
---|
| 203 | this->engine_ = dynamic_cast<Engine*>(object); |
---|
| 204 | |
---|
| 205 | if (this->engine_) |
---|
| 206 | { |
---|
| 207 | this->engine_->addTemplate(temp); |
---|
| 208 | this->engine_->addToSpaceShip(this); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | delete object; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void SpaceShip::setEngine(Engine* engine) |
---|
| 220 | { |
---|
| 221 | this->engine_ = engine; |
---|
| 222 | if (engine && engine->getShip() != this) |
---|
| 223 | engine->addToSpaceShip(this); |
---|
| 224 | } |
---|
[2072] | 225 | } |
---|