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