[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" |
---|
[2254] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20; |
---|
| 47 | |
---|
| 48 | CreateFactory(MultiStateEngine); |
---|
| 49 | |
---|
| 50 | MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) |
---|
| 51 | { |
---|
| 52 | RegisterObject(MultiStateEngine); |
---|
| 53 | |
---|
[6187] | 54 | this->lua_ = new LuaState(); |
---|
[2254] | 55 | this->state_ = 0; |
---|
| 56 | |
---|
| 57 | this->registerVariables(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | MultiStateEngine::~MultiStateEngine() |
---|
| 61 | { |
---|
| 62 | if (this->isInitialized() && !this->getShip()) |
---|
| 63 | { |
---|
| 64 | // We have no ship, so the effects are not attached and won't be destroyed automatically |
---|
[6187] | 65 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 66 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) |
---|
| 67 | (*it2)->destroy(); |
---|
| 68 | delete this->lua_; |
---|
[2254] | 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 73 | { |
---|
| 74 | SUPER(MultiStateEngine, XMLPort, xmlelement, mode); |
---|
[6187] | 75 | XMLPortObject(MultiStateEngine, EffectContainer, "", addEffectContainer, getEffectContainer, xmlelement, mode); |
---|
[2254] | 76 | } |
---|
| 77 | |
---|
| 78 | void MultiStateEngine::registerVariables() |
---|
| 79 | { |
---|
[3280] | 80 | registerVariable(this->state_, VariableDirection::ToServer); |
---|
[2254] | 81 | } |
---|
| 82 | |
---|
| 83 | void MultiStateEngine::tick(float dt) |
---|
| 84 | { |
---|
| 85 | if (this->getShip()) |
---|
| 86 | { |
---|
| 87 | if (this->getShip()->hasLocalController()) |
---|
| 88 | { |
---|
[5929] | 89 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2254] | 90 | |
---|
| 91 | const Vector3& direction = this->getDirection(); |
---|
[2488] | 92 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
[2254] | 93 | |
---|
| 94 | bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
| 95 | |
---|
[6187] | 96 | int newState = 0; |
---|
| 97 | if (this->getShip()->getBoost() && forward) |
---|
| 98 | newState = Boost; |
---|
| 99 | else if (forward && !newState) // newState == Boost |
---|
| 100 | newState = Normal; |
---|
| 101 | else if (direction.z > 0 && velocity.z < 0) |
---|
| 102 | newState = Brake; |
---|
[2254] | 103 | else |
---|
[6187] | 104 | newState = Idle; |
---|
[2254] | 105 | |
---|
[6187] | 106 | if (newState != this->state_) |
---|
| 107 | { |
---|
| 108 | int changes = newState | this->state_; |
---|
| 109 | if (changes & Idle) |
---|
| 110 | { |
---|
| 111 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle); |
---|
| 112 | lua_setglobal(this->lua_->getInternalLuaState(), "idle"); |
---|
| 113 | } |
---|
| 114 | if (changes & Normal) |
---|
| 115 | { |
---|
| 116 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal); |
---|
| 117 | lua_setglobal(this->lua_->getInternalLuaState(), "normal"); |
---|
| 118 | } |
---|
| 119 | if (changes & Brake) |
---|
| 120 | { |
---|
| 121 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake); |
---|
| 122 | lua_setglobal(this->lua_->getInternalLuaState(), "brake"); |
---|
| 123 | } |
---|
| 124 | if (changes & Boost) |
---|
| 125 | { |
---|
| 126 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost); |
---|
| 127 | lua_setglobal(this->lua_->getInternalLuaState(), "boost"); |
---|
| 128 | } |
---|
[2254] | 129 | |
---|
[6187] | 130 | // Update all effect conditions |
---|
| 131 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 132 | (*it)->updateCondition(); |
---|
[2254] | 133 | |
---|
[6187] | 134 | this->state_ = newState; |
---|
| 135 | } |
---|
[2254] | 136 | } |
---|
| 137 | |
---|
[2896] | 138 | if (GameMode::isMaster()) |
---|
[2254] | 139 | { |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[2809] | 143 | SUPER(MultiStateEngine, tick, dt); |
---|
[2254] | 144 | } |
---|
| 145 | |
---|
| 146 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
| 147 | { |
---|
| 148 | Engine::addToSpaceShip(ship); |
---|
| 149 | |
---|
| 150 | if (!ship) |
---|
| 151 | return; |
---|
| 152 | |
---|
[6187] | 153 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 154 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) |
---|
| 155 | this->getShip()->attach(*it2); |
---|
[2254] | 156 | } |
---|
| 157 | |
---|
[6187] | 158 | void MultiStateEngine::addEffectContainer(EffectContainer* effect) |
---|
[2254] | 159 | { |
---|
[6187] | 160 | if (effect == NULL) |
---|
| 161 | return; |
---|
| 162 | effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size())); |
---|
| 163 | this->effectContainers_.push_back(effect); |
---|
[2254] | 164 | if (this->getShip()) |
---|
| 165 | { |
---|
[6187] | 166 | for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) |
---|
| 167 | this->getShip()->attach(*it); |
---|
[2254] | 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
[6187] | 171 | EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const |
---|
[2254] | 172 | { |
---|
| 173 | unsigned int i = 0; |
---|
[6187] | 174 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
[2254] | 175 | { |
---|
| 176 | if (i == index) |
---|
| 177 | return (*it); |
---|
| 178 | } |
---|
[6187] | 179 | return NULL; |
---|
[2254] | 180 | } |
---|
| 181 | } |
---|