[2254] | 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 "Engine.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Math.h" |
---|
[2254] | 32 | #include "core/CoreIncludes.h" |
---|
[2478] | 33 | #include "core/ConfigValueIncludes.h" |
---|
[2254] | 34 | #include "core/XMLPort.h" |
---|
[5735] | 35 | #include "Scene.h" |
---|
| 36 | #include "worldentities/pawns/SpaceShip.h" |
---|
[2350] | 37 | #include "tools/Shader.h" |
---|
[2254] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | CreateFactory(Engine); |
---|
| 42 | |
---|
| 43 | Engine::Engine(BaseObject* creator) : Item(creator) |
---|
| 44 | { |
---|
| 45 | RegisterObject(Engine); |
---|
| 46 | |
---|
| 47 | this->ship_ = 0; |
---|
| 48 | this->shipID_ = OBJECTID_UNKNOWN; |
---|
| 49 | |
---|
| 50 | this->boostFactor_ = 1.5; |
---|
| 51 | this->speedFactor_ = 1.0; |
---|
| 52 | |
---|
| 53 | this->maxSpeedFront_ = 0.0; |
---|
| 54 | this->maxSpeedBack_ = 0.0; |
---|
| 55 | this->maxSpeedLeftRight_ = 0.0; |
---|
| 56 | this->maxSpeedUpDown_ = 0.0; |
---|
| 57 | |
---|
| 58 | this->accelerationFront_ = 0.0; |
---|
| 59 | this->accelerationBrake_ = 0.0; |
---|
| 60 | this->accelerationBack_ = 0.0; |
---|
| 61 | this->accelerationLeftRight_ = 0.0; |
---|
| 62 | this->accelerationUpDown_ = 0.0; |
---|
| 63 | |
---|
[2350] | 64 | this->boostBlur_ = 0; |
---|
| 65 | |
---|
[6709] | 66 | this->speedAdd_ = 0.0; |
---|
| 67 | this->speedMultiply_ = 1.0; |
---|
| 68 | |
---|
[2478] | 69 | this->setConfigValues(); |
---|
[2254] | 70 | this->registerVariables(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | Engine::~Engine() |
---|
| 74 | { |
---|
[2256] | 75 | if (this->isInitialized() && this->ship_) |
---|
[2350] | 76 | { |
---|
[2256] | 77 | this->ship_->setEngine(0); |
---|
[2350] | 78 | |
---|
| 79 | if (this->boostBlur_) |
---|
[5929] | 80 | this->boostBlur_->destroy(); |
---|
[2350] | 81 | } |
---|
[2254] | 82 | } |
---|
| 83 | |
---|
| 84 | void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 85 | { |
---|
| 86 | SUPER(Engine, XMLPort, xmlelement, mode); |
---|
| 87 | |
---|
| 88 | XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode); |
---|
| 89 | |
---|
| 90 | XMLPortParam(Engine, "speedfront", setMaxSpeedFront, setMaxSpeedFront, xmlelement, mode); |
---|
| 91 | XMLPortParam(Engine, "speedback", setMaxSpeedBack, setMaxSpeedBack, xmlelement, mode); |
---|
| 92 | XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode); |
---|
| 93 | XMLPortParam(Engine, "speedupdown", setMaxSpeedUpDown, setMaxSpeedUpDown, xmlelement, mode); |
---|
| 94 | |
---|
| 95 | XMLPortParam(Engine, "accelerationfront", setAccelerationFront, setAccelerationFront, xmlelement, mode); |
---|
| 96 | XMLPortParam(Engine, "accelerationbrake", setAccelerationBrake, setAccelerationBrake, xmlelement, mode); |
---|
| 97 | XMLPortParam(Engine, "accelerationback", setAccelerationBack, setAccelerationBack, xmlelement, mode); |
---|
| 98 | XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode); |
---|
| 99 | XMLPortParam(Engine, "accelerationupdown", setAccelerationUpDown, setAccelerationUpDown, xmlelement, mode); |
---|
| 100 | } |
---|
| 101 | |
---|
[2478] | 102 | void Engine::setConfigValues() |
---|
| 103 | { |
---|
[8079] | 104 | SetConfigValueExternal(bEnableMotionBlur_, "GraphicsSettings", "enableMotionBlur", true) |
---|
| 105 | .description("Enable or disable the motion blur effect when moving very fast") |
---|
| 106 | .callback(this, &Engine::changedEnableMotionBlur); |
---|
| 107 | SetConfigValueExternal(blurStrength_, "GraphicsSettings", "blurStrength", 3.0f) |
---|
| 108 | .description("Defines the strength of the motion blur effect"); |
---|
[2478] | 109 | } |
---|
| 110 | |
---|
[2254] | 111 | void Engine::registerVariables() |
---|
| 112 | { |
---|
[3280] | 113 | registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID)); |
---|
[2254] | 114 | |
---|
[3280] | 115 | registerVariable(this->speedFactor_, VariableDirection::ToClient); |
---|
| 116 | registerVariable(this->boostFactor_, VariableDirection::ToClient); |
---|
[2254] | 117 | |
---|
[3280] | 118 | registerVariable(this->maxSpeedFront_, VariableDirection::ToClient); |
---|
| 119 | registerVariable(this->maxSpeedBack_, VariableDirection::ToClient); |
---|
| 120 | registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient); |
---|
| 121 | registerVariable(this->maxSpeedUpDown_, VariableDirection::ToClient); |
---|
[2254] | 122 | |
---|
[3280] | 123 | registerVariable(this->accelerationFront_, VariableDirection::ToClient); |
---|
| 124 | registerVariable(this->accelerationBrake_, VariableDirection::ToClient); |
---|
| 125 | registerVariable(this->accelerationBack_, VariableDirection::ToClient); |
---|
| 126 | registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient); |
---|
| 127 | registerVariable(this->accelerationUpDown_, VariableDirection::ToClient); |
---|
[6709] | 128 | |
---|
| 129 | registerVariable(this->speedAdd_, VariableDirection::ToClient); |
---|
| 130 | registerVariable(this->speedMultiply_, VariableDirection::ToClient); |
---|
[2254] | 131 | } |
---|
| 132 | |
---|
| 133 | void Engine::networkcallback_shipID() |
---|
| 134 | { |
---|
| 135 | this->ship_ = 0; |
---|
| 136 | |
---|
| 137 | if (this->shipID_ != OBJECTID_UNKNOWN) |
---|
| 138 | { |
---|
| 139 | Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_); |
---|
| 140 | if (object) |
---|
[3325] | 141 | this->addToSpaceShip(orxonox_cast<SpaceShip*>(object)); |
---|
[2254] | 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void Engine::tick(float dt) |
---|
| 146 | { |
---|
| 147 | if (!this->ship_) |
---|
| 148 | { |
---|
| 149 | if (this->shipID_) |
---|
| 150 | { |
---|
| 151 | this->networkcallback_shipID(); |
---|
| 152 | |
---|
| 153 | if (!this->ship_) |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | return; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | if (!this->isActive()) |
---|
| 161 | return; |
---|
| 162 | |
---|
[2361] | 163 | SUPER(Engine, tick, dt); |
---|
| 164 | |
---|
[2254] | 165 | const Vector3& direction = this->getDirection(); |
---|
[2488] | 166 | Vector3 velocity = this->ship_->getLocalVelocity(); |
---|
[2254] | 167 | Vector3 acceleration = Vector3::ZERO; |
---|
| 168 | |
---|
| 169 | float factor = 1.0f / this->speedFactor_; |
---|
| 170 | velocity *= factor; |
---|
| 171 | |
---|
| 172 | if (direction.z < 0) |
---|
| 173 | { |
---|
| 174 | if (this->maxSpeedFront_ != 0) |
---|
| 175 | { |
---|
| 176 | float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f); |
---|
| 177 | acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | else if (direction.z > 0) |
---|
| 181 | { |
---|
| 182 | if (velocity.z < 0) |
---|
| 183 | acceleration.z = direction.z * this->accelerationBrake_; |
---|
| 184 | else if (this->maxSpeedBack_ != 0) |
---|
| 185 | acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (this->maxSpeedLeftRight_ != 0) |
---|
| 189 | { |
---|
| 190 | if (direction.x < 0) |
---|
| 191 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
| 192 | else if (direction.x > 0) |
---|
| 193 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | if (this->maxSpeedUpDown_ != 0) |
---|
| 197 | { |
---|
| 198 | if (direction.y < 0) |
---|
| 199 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
| 200 | else if (direction.y > 0) |
---|
| 201 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
| 202 | } |
---|
| 203 | |
---|
[6709] | 204 | this->ship_->setAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd()))); |
---|
[2254] | 205 | |
---|
| 206 | this->ship_->setSteeringDirection(Vector3::ZERO); |
---|
[2350] | 207 | |
---|
[8079] | 208 | if (this->bEnableMotionBlur_ && !this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController()) |
---|
[2350] | 209 | { |
---|
| 210 | this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager()); |
---|
[8079] | 211 | this->boostBlur_->setCompositorName("Radial Blur"); |
---|
[2350] | 212 | } |
---|
| 213 | |
---|
| 214 | if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) |
---|
[8079] | 215 | { |
---|
| 216 | float blur = this->blurStrength_ * clamp((-velocity.z - this->maxSpeedFront_) / ((this->boostFactor_ - 1) * this->maxSpeedFront_), 0.0f, 1.0f); |
---|
| 217 | |
---|
| 218 | this->boostBlur_->setVisible(blur > 0); |
---|
| 219 | this->boostBlur_->setParameter(0, 0, "sampleStrength", blur); |
---|
| 220 | } |
---|
[2254] | 221 | } |
---|
| 222 | |
---|
[2350] | 223 | void Engine::changedActivity() |
---|
| 224 | { |
---|
| 225 | SUPER(Engine, changedActivity); |
---|
| 226 | |
---|
| 227 | if (this->boostBlur_) |
---|
| 228 | this->boostBlur_->setVisible(this->isVisible()); |
---|
| 229 | } |
---|
| 230 | |
---|
[2254] | 231 | void Engine::addToSpaceShip(SpaceShip* ship) |
---|
| 232 | { |
---|
| 233 | this->ship_ = ship; |
---|
[3060] | 234 | |
---|
[2254] | 235 | if (ship) |
---|
[2256] | 236 | { |
---|
[2254] | 237 | this->shipID_ = ship->getObjectID(); |
---|
[2256] | 238 | if (ship->getEngine() != this) |
---|
| 239 | ship->setEngine(this); |
---|
[2350] | 240 | |
---|
| 241 | if (this->boostBlur_) |
---|
| 242 | { |
---|
[5929] | 243 | this->boostBlur_->destroy(); |
---|
[2350] | 244 | this->boostBlur_ = 0; |
---|
| 245 | } |
---|
[2256] | 246 | } |
---|
[2254] | 247 | } |
---|
| 248 | |
---|
| 249 | const Vector3& Engine::getDirection() const |
---|
| 250 | { |
---|
| 251 | if (this->ship_) |
---|
| 252 | return this->ship_->getSteeringDirection(); |
---|
| 253 | else |
---|
| 254 | return Vector3::ZERO; |
---|
| 255 | } |
---|
[6709] | 256 | |
---|
[7547] | 257 | PickupCarrier* Engine::getCarrierParent(void) const |
---|
[6709] | 258 | { |
---|
| 259 | return this->ship_; |
---|
| 260 | } |
---|
| 261 | |
---|
[7547] | 262 | const Vector3& Engine::getCarrierPosition(void) const |
---|
[6709] | 263 | { |
---|
| 264 | return this->ship_->getWorldPosition(); |
---|
| 265 | } |
---|
[8079] | 266 | |
---|
| 267 | void Engine::changedEnableMotionBlur() |
---|
| 268 | { |
---|
| 269 | if (!this->bEnableMotionBlur_) |
---|
| 270 | { |
---|
| 271 | this->boostBlur_->destroy(); |
---|
| 272 | this->boostBlur_ = 0; |
---|
| 273 | } |
---|
| 274 | } |
---|
[2254] | 275 | } |
---|