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