[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 |
---|
[6187] | 24 | * Reto Grieder |
---|
[2254] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "MultiStateEngine.h" |
---|
| 31 | |
---|
[6187] | 32 | extern "C" { |
---|
| 33 | #include <lua.h> |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | #include "util/Convert.h" |
---|
[3196] | 37 | #include "core/CoreIncludes.h" |
---|
[2896] | 38 | #include "core/GameMode.h" |
---|
[6187] | 39 | #include "core/LuaState.h" |
---|
[2254] | 40 | #include "core/XMLPort.h" |
---|
[6187] | 41 | #include "worldentities/EffectContainer.h" |
---|
[5735] | 42 | #include "worldentities/pawns/SpaceShip.h" |
---|
[6202] | 43 | #include "sound/WorldSound.h" |
---|
[2254] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20; |
---|
[6202] | 48 | static const float MAX_VELOCITY_NORMAL = 111; |
---|
| 49 | static const float MAX_VELOCITY_BOOST = 221; |
---|
[2254] | 50 | |
---|
| 51 | CreateFactory(MultiStateEngine); |
---|
| 52 | |
---|
| 53 | MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) |
---|
| 54 | { |
---|
| 55 | RegisterObject(MultiStateEngine); |
---|
| 56 | |
---|
[6202] | 57 | defEngineSndNormal_ = new WorldSound(this); |
---|
| 58 | defEngineSndNormal_->setLooping(true); |
---|
| 59 | |
---|
[6187] | 60 | this->lua_ = new LuaState(); |
---|
[2254] | 61 | this->state_ = 0; |
---|
| 62 | |
---|
| 63 | this->registerVariables(); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | MultiStateEngine::~MultiStateEngine() |
---|
| 67 | { |
---|
| 68 | if (this->isInitialized() && !this->getShip()) |
---|
| 69 | { |
---|
| 70 | // We have no ship, so the effects are not attached and won't be destroyed automatically |
---|
[6187] | 71 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 72 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) |
---|
| 73 | (*it2)->destroy(); |
---|
[6202] | 74 | delete this->defEngineSndNormal_; |
---|
[6187] | 75 | delete this->lua_; |
---|
[2254] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 80 | { |
---|
| 81 | SUPER(MultiStateEngine, XMLPort, xmlelement, mode); |
---|
[6187] | 82 | XMLPortObject(MultiStateEngine, EffectContainer, "", addEffectContainer, getEffectContainer, xmlelement, mode); |
---|
[6202] | 83 | XMLPortParam(MultiStateEngine, "defEngineSndNormal", setDefEngSndNormal, getDefEngSndNormal, xmlelement, mode); |
---|
[2254] | 84 | } |
---|
| 85 | |
---|
| 86 | void MultiStateEngine::registerVariables() |
---|
| 87 | { |
---|
[3280] | 88 | registerVariable(this->state_, VariableDirection::ToServer); |
---|
[2254] | 89 | } |
---|
| 90 | |
---|
| 91 | void MultiStateEngine::tick(float dt) |
---|
| 92 | { |
---|
| 93 | if (this->getShip()) |
---|
| 94 | { |
---|
| 95 | if (this->getShip()->hasLocalController()) |
---|
| 96 | { |
---|
[5929] | 97 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2254] | 98 | |
---|
| 99 | const Vector3& direction = this->getDirection(); |
---|
[2488] | 100 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
[2254] | 101 | |
---|
[6202] | 102 | float pitch = velocity.length(); |
---|
[2254] | 103 | bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
| 104 | |
---|
[6187] | 105 | int newState = 0; |
---|
| 106 | if (this->getShip()->getBoost() && forward) |
---|
[6202] | 107 | { |
---|
[6187] | 108 | newState = Boost; |
---|
[6202] | 109 | pitch = pitch/MAX_VELOCITY_BOOST + 1; |
---|
| 110 | pitch = pitch > 2 ? 2 : pitch; |
---|
| 111 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
| 112 | defEngineSndNormal_->setPitch(pitch); |
---|
| 113 | } |
---|
[6187] | 114 | else if (forward && !newState) // newState == Boost |
---|
[6202] | 115 | { |
---|
[6187] | 116 | newState = Normal; |
---|
[6202] | 117 | pitch = pitch/MAX_VELOCITY_NORMAL + 1; |
---|
| 118 | pitch = pitch > 2 ? 2 : pitch; |
---|
| 119 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
| 120 | defEngineSndNormal_->setPitch(pitch); |
---|
| 121 | } |
---|
[6187] | 122 | else if (direction.z > 0 && velocity.z < 0) |
---|
| 123 | newState = Brake; |
---|
[2254] | 124 | else |
---|
[6187] | 125 | newState = Idle; |
---|
[2254] | 126 | |
---|
[6187] | 127 | if (newState != this->state_) |
---|
| 128 | { |
---|
| 129 | int changes = newState | this->state_; |
---|
| 130 | if (changes & Idle) |
---|
| 131 | { |
---|
| 132 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle); |
---|
| 133 | lua_setglobal(this->lua_->getInternalLuaState(), "idle"); |
---|
| 134 | } |
---|
| 135 | if (changes & Normal) |
---|
| 136 | { |
---|
| 137 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal); |
---|
| 138 | lua_setglobal(this->lua_->getInternalLuaState(), "normal"); |
---|
[6202] | 139 | if(newState & Normal) |
---|
| 140 | { |
---|
| 141 | defEngineSndNormal_->play(); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | defEngineSndNormal_->stop(); |
---|
| 146 | } |
---|
[6187] | 147 | } |
---|
| 148 | if (changes & Brake) |
---|
| 149 | { |
---|
| 150 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake); |
---|
| 151 | lua_setglobal(this->lua_->getInternalLuaState(), "brake"); |
---|
| 152 | } |
---|
| 153 | if (changes & Boost) |
---|
| 154 | { |
---|
| 155 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost); |
---|
| 156 | lua_setglobal(this->lua_->getInternalLuaState(), "boost"); |
---|
| 157 | } |
---|
[2254] | 158 | |
---|
[6187] | 159 | // Update all effect conditions |
---|
| 160 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 161 | (*it)->updateCondition(); |
---|
[2254] | 162 | |
---|
[6187] | 163 | this->state_ = newState; |
---|
| 164 | } |
---|
[2254] | 165 | } |
---|
| 166 | |
---|
[2896] | 167 | if (GameMode::isMaster()) |
---|
[2254] | 168 | { |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[2809] | 172 | SUPER(MultiStateEngine, tick, dt); |
---|
[2254] | 173 | } |
---|
| 174 | |
---|
| 175 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
| 176 | { |
---|
| 177 | Engine::addToSpaceShip(ship); |
---|
| 178 | |
---|
| 179 | if (!ship) |
---|
| 180 | return; |
---|
| 181 | |
---|
[6202] | 182 | this->getShip()->attach(defEngineSndNormal_); |
---|
| 183 | |
---|
[6187] | 184 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 185 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) |
---|
| 186 | this->getShip()->attach(*it2); |
---|
[2254] | 187 | } |
---|
| 188 | |
---|
[6187] | 189 | void MultiStateEngine::addEffectContainer(EffectContainer* effect) |
---|
[2254] | 190 | { |
---|
[6187] | 191 | if (effect == NULL) |
---|
| 192 | return; |
---|
| 193 | effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size())); |
---|
| 194 | this->effectContainers_.push_back(effect); |
---|
[2254] | 195 | if (this->getShip()) |
---|
| 196 | { |
---|
[6187] | 197 | for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) |
---|
| 198 | this->getShip()->attach(*it); |
---|
[2254] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[6187] | 202 | EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const |
---|
[2254] | 203 | { |
---|
| 204 | unsigned int i = 0; |
---|
[6187] | 205 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
[2254] | 206 | { |
---|
| 207 | if (i == index) |
---|
| 208 | return (*it); |
---|
| 209 | } |
---|
[6187] | 210 | return NULL; |
---|
[2254] | 211 | } |
---|
[6202] | 212 | |
---|
| 213 | void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound) |
---|
| 214 | { |
---|
| 215 | defEngineSndNormal_->setSource(engineSound); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | const std::string& MultiStateEngine::getDefEngSndNormal() |
---|
| 219 | { |
---|
| 220 | return defEngineSndNormal_->getSource(); |
---|
| 221 | } |
---|
[2254] | 222 | } |
---|