[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 "MultiStateEngine.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "core/CoreIncludes.h" |
---|
[2896] | 32 | #include "core/GameMode.h" |
---|
[2254] | 33 | #include "core/XMLPort.h" |
---|
[5735] | 34 | #include "worldentities/pawns/SpaceShip.h" |
---|
[2254] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
| 38 | static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20; |
---|
| 39 | |
---|
| 40 | static const unsigned char STATE_ACTIVE = 1; |
---|
| 41 | static const unsigned char STATE_FORWARD = 2; |
---|
| 42 | static const unsigned char STATE_BOOST = 4; |
---|
| 43 | static const unsigned char STATE_BRAKE = 8; |
---|
| 44 | |
---|
| 45 | CreateFactory(MultiStateEngine); |
---|
| 46 | |
---|
| 47 | MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) |
---|
| 48 | { |
---|
| 49 | RegisterObject(MultiStateEngine); |
---|
| 50 | |
---|
| 51 | this->state_ = 0; |
---|
| 52 | |
---|
| 53 | this->registerVariables(); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | MultiStateEngine::~MultiStateEngine() |
---|
| 57 | { |
---|
| 58 | if (this->isInitialized() && !this->getShip()) |
---|
| 59 | { |
---|
| 60 | // We have no ship, so the effects are not attached and won't be destroyed automatically |
---|
| 61 | for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) |
---|
| 62 | delete (*it); |
---|
| 63 | for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) |
---|
| 64 | delete (*it); |
---|
| 65 | for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) |
---|
| 66 | delete (*it); |
---|
| 67 | for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) |
---|
| 68 | delete (*it); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 73 | { |
---|
| 74 | SUPER(MultiStateEngine, XMLPort, xmlelement, mode); |
---|
| 75 | |
---|
| 76 | XMLPortObject(MultiStateEngine, WorldEntity, "active", addActiveEffect, getActiveEffect, xmlelement, mode); |
---|
| 77 | XMLPortObject(MultiStateEngine, WorldEntity, "forward", addForwardEffect, getForwardEffect, xmlelement, mode); |
---|
| 78 | XMLPortObject(MultiStateEngine, WorldEntity, "boost", addBoostEffect, getBoostEffect, xmlelement, mode); |
---|
| 79 | XMLPortObject(MultiStateEngine, WorldEntity, "brake", addBrakeEffect, getBrakeEffect, xmlelement, mode); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void MultiStateEngine::registerVariables() |
---|
| 83 | { |
---|
[3280] | 84 | registerVariable(this->state_, VariableDirection::ToServer); |
---|
[2254] | 85 | } |
---|
| 86 | |
---|
| 87 | void MultiStateEngine::tick(float dt) |
---|
| 88 | { |
---|
| 89 | if (this->getShip()) |
---|
| 90 | { |
---|
| 91 | if (this->getShip()->hasLocalController()) |
---|
| 92 | { |
---|
[3280] | 93 | this->setObjectMode(ObjectDirection::Bidirectional); |
---|
[2254] | 94 | |
---|
| 95 | const Vector3& direction = this->getDirection(); |
---|
[2488] | 96 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
[2254] | 97 | |
---|
| 98 | bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
| 99 | bool boost = (this->getShip()->getBoost() && forward); |
---|
| 100 | bool brake = (direction.z > 0 && velocity.z < 0); |
---|
| 101 | bool active = (direction != Vector3::ZERO && !brake); |
---|
| 102 | |
---|
| 103 | if (active) |
---|
| 104 | this->state_ |= STATE_ACTIVE; |
---|
| 105 | else |
---|
| 106 | this->state_ &= ~STATE_ACTIVE; |
---|
| 107 | |
---|
| 108 | if (forward) |
---|
| 109 | this->state_ |= STATE_FORWARD; |
---|
| 110 | else |
---|
| 111 | this->state_ &= ~STATE_FORWARD; |
---|
| 112 | |
---|
| 113 | if (boost) |
---|
| 114 | this->state_ |= STATE_BOOST; |
---|
| 115 | else |
---|
| 116 | this->state_ &= ~STATE_BOOST; |
---|
| 117 | |
---|
| 118 | if (brake) |
---|
| 119 | this->state_ |= STATE_BRAKE; |
---|
| 120 | else |
---|
| 121 | this->state_ &= ~STATE_BRAKE; |
---|
| 122 | } |
---|
| 123 | |
---|
[2896] | 124 | if (GameMode::isMaster()) |
---|
[2254] | 125 | { |
---|
| 126 | for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) |
---|
| 127 | (*it)->setMainState(this->state_ & STATE_ACTIVE); |
---|
| 128 | for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) |
---|
| 129 | (*it)->setMainState(this->state_ & STATE_FORWARD); |
---|
| 130 | for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) |
---|
| 131 | (*it)->setMainState(this->state_ & STATE_BOOST); |
---|
| 132 | for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) |
---|
| 133 | (*it)->setMainState(this->state_ & STATE_BRAKE); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
[2809] | 137 | SUPER(MultiStateEngine, tick, dt); |
---|
[2254] | 138 | } |
---|
| 139 | |
---|
| 140 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
| 141 | { |
---|
| 142 | Engine::addToSpaceShip(ship); |
---|
| 143 | |
---|
| 144 | if (!ship) |
---|
| 145 | return; |
---|
| 146 | |
---|
| 147 | for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) |
---|
| 148 | ship->attach(*it); |
---|
| 149 | for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) |
---|
| 150 | ship->attach(*it); |
---|
| 151 | for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) |
---|
| 152 | ship->attach(*it); |
---|
| 153 | for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) |
---|
| 154 | ship->attach(*it); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void MultiStateEngine::addActiveEffect(WorldEntity* effect) |
---|
| 158 | { |
---|
| 159 | this->activeEffects_.push_back(effect); |
---|
| 160 | if (this->getShip()) |
---|
| 161 | this->getShip()->attach(effect); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | void MultiStateEngine::addForwardEffect(WorldEntity* effect) |
---|
| 165 | { |
---|
| 166 | this->forwardEffects_.push_back(effect); |
---|
| 167 | if (this->getShip()) |
---|
| 168 | this->getShip()->attach(effect); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void MultiStateEngine::addBoostEffect(WorldEntity* effect) |
---|
| 172 | { |
---|
| 173 | this->boostEffects_.push_back(effect); |
---|
| 174 | if (this->getShip()) |
---|
| 175 | this->getShip()->attach(effect); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void MultiStateEngine::addBrakeEffect(WorldEntity* effect) |
---|
| 179 | { |
---|
| 180 | this->brakeEffects_.push_back(effect); |
---|
| 181 | if (this->getShip()) |
---|
| 182 | this->getShip()->attach(effect); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | WorldEntity* MultiStateEngine::getActiveEffect(unsigned int index) const |
---|
| 186 | { |
---|
| 187 | unsigned int i = 0; |
---|
| 188 | for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it) |
---|
| 189 | { |
---|
| 190 | if (i == index) |
---|
| 191 | return (*it); |
---|
| 192 | ++i; |
---|
| 193 | } |
---|
| 194 | return 0; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | WorldEntity* MultiStateEngine::getForwardEffect(unsigned int index) const |
---|
| 198 | { |
---|
| 199 | unsigned int i = 0; |
---|
| 200 | for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it) |
---|
| 201 | { |
---|
| 202 | if (i == index) |
---|
| 203 | return (*it); |
---|
| 204 | ++i; |
---|
| 205 | } |
---|
| 206 | return 0; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | WorldEntity* MultiStateEngine::getBoostEffect(unsigned int index) const |
---|
| 210 | { |
---|
| 211 | unsigned int i = 0; |
---|
| 212 | for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it) |
---|
| 213 | { |
---|
| 214 | if (i == index) |
---|
| 215 | return (*it); |
---|
| 216 | ++i; |
---|
| 217 | } |
---|
| 218 | return 0; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | WorldEntity* MultiStateEngine::getBrakeEffect(unsigned int index) const |
---|
| 222 | { |
---|
| 223 | unsigned int i = 0; |
---|
| 224 | for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it) |
---|
| 225 | { |
---|
| 226 | if (i == index) |
---|
| 227 | return (*it); |
---|
| 228 | ++i; |
---|
| 229 | } |
---|
| 230 | return 0; |
---|
| 231 | } |
---|
| 232 | } |
---|