[10871] | 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 | * Dominik Solenicki |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | #include "controllers/FlyingController.h" |
---|
| 29 | #include "core/XMLPort.h" |
---|
| 30 | #include "worldentities/pawns/SpaceShip.h" |
---|
[10875] | 31 | #include "util/Math.h" |
---|
[10871] | 32 | |
---|
| 33 | namespace orxonox |
---|
| 34 | { |
---|
| 35 | RegisterClass (FlyingController); |
---|
| 36 | |
---|
| 37 | FlyingController::FlyingController( Context* context ): CommonController( context ) |
---|
| 38 | { |
---|
| 39 | this->spread_ = 200; |
---|
| 40 | this->tolerance_ = 80; |
---|
| 41 | RegisterObject( FlyingController ); |
---|
| 42 | } |
---|
| 43 | FlyingController::~FlyingController() |
---|
| 44 | { |
---|
| 45 | |
---|
| 46 | } |
---|
| 47 | void FlyingController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
| 48 | { |
---|
| 49 | XMLPortParam( FlyingController, "spread", setSpread, getSpread, xmlelement, mode ); |
---|
| 50 | XMLPortParam( FlyingController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); |
---|
| 51 | SUPER( FlyingController, XMLPort, xmlelement, mode ); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void FlyingController::setFormationModeXML( std::string val ) |
---|
| 55 | { |
---|
| 56 | const std::string valUpper = getUppercase( val ); |
---|
| 57 | FormationMode::Value value; |
---|
| 58 | |
---|
| 59 | if ( valUpper == "WALL" ) |
---|
| 60 | value = FormationMode::WALL; |
---|
| 61 | else if ( valUpper == "FINGER4" ) |
---|
| 62 | value = FormationMode::FINGER4; |
---|
| 63 | else if ( valUpper == "DIAMOND" ) |
---|
| 64 | value = FormationMode::DIAMOND; |
---|
| 65 | else |
---|
| 66 | ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." ); |
---|
| 67 | this->setFormationMode( value ); |
---|
| 68 | } |
---|
| 69 | std::string FlyingController::getFormationModeXML() |
---|
| 70 | { |
---|
| 71 | switch ( this->formationMode_ ) |
---|
| 72 | { |
---|
| 73 | case FormationMode::WALL: |
---|
| 74 | { return "WALL"; break; } |
---|
| 75 | case FormationMode::FINGER4: |
---|
| 76 | { return "FINGER4"; break; } |
---|
| 77 | case FormationMode::DIAMOND: |
---|
| 78 | { return "DIAMOND"; break; } |
---|
| 79 | default: |
---|
| 80 | return "DIAMOND"; break; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | void FlyingController::stopMoving() |
---|
| 87 | { |
---|
| 88 | this->bHasTargetPosition_ = false; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void FlyingController::moveToPosition( const Vector3& target, float dt ) |
---|
| 92 | { |
---|
| 93 | ControllableEntity* entity = this->getControllableEntity(); |
---|
| 94 | Vector2 coord = get2DViewCoordinates |
---|
| 95 | ( entity->getPosition() , |
---|
| 96 | entity->getOrientation() * WorldEntity::FRONT, |
---|
| 97 | entity->getOrientation() * WorldEntity::UP, |
---|
| 98 | target ); |
---|
| 99 | |
---|
| 100 | float distance = ( target - this->getControllableEntity() ->getPosition() ).length(); |
---|
| 101 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
| 102 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
| 103 | |
---|
| 104 | if ( distance > this->tolerance_ ) |
---|
| 105 | { |
---|
| 106 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
| 107 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
| 108 | |
---|
| 109 | if ( distance < 300 ) |
---|
| 110 | { |
---|
| 111 | if ( bHasTargetOrientation_ ) |
---|
| 112 | { |
---|
| 113 | copyTargetOrientation( dt ); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | if (distance > this->tolerance_*1.5f || (rotateX > -0.01 && rotateX < 0.01 && rotateY > -0.01 && rotateY < 0.01)) |
---|
| 117 | this->getControllableEntity() ->moveFrontBack( SPEED * dt ); |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | bHasTargetPosition_ = false; |
---|
| 122 | bHasTargetOrientation_ = false; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | void FlyingController::moveToTargetPosition(float dt) |
---|
| 126 | { |
---|
| 127 | this->moveToPosition (this->targetPosition_, dt); |
---|
| 128 | } |
---|
| 129 | void FlyingController::copyOrientation( const Quaternion& orient, float dt ) |
---|
| 130 | { |
---|
[10875] | 131 | |
---|
| 132 | double diff=orient.getRoll(false).valueRadians() - |
---|
[10871] | 133 | ( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() ); |
---|
[10875] | 134 | while( diff>math::twoPi)diff-=math::twoPi; |
---|
[10871] | 135 | while( diff<-math::twoPi )diff+=math::twoPi; |
---|
[10875] | 136 | |
---|
| 137 | this->getControllableEntity() ->rotateRoll(diff * ROTATEFACTOR * dt); |
---|
[10871] | 138 | } |
---|
[10875] | 139 | //change log: increased precision, increased rotation speed |
---|
[10871] | 140 | void FlyingController::copyTargetOrientation( float dt ) |
---|
| 141 | { |
---|
| 142 | if ( bHasTargetOrientation_ ) |
---|
| 143 | { |
---|
| 144 | copyOrientation( targetOrientation_, dt ); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void FlyingController::setTargetPosition( const Vector3& target ) |
---|
| 149 | { |
---|
| 150 | this->targetPosition_ = target; |
---|
| 151 | this->bHasTargetPosition_ = true; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void FlyingController::setTargetOrientation( const Quaternion& orient ) |
---|
| 155 | { |
---|
| 156 | this->targetOrientation_=orient; |
---|
| 157 | this->bHasTargetOrientation_=true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void FlyingController::setTargetOrientation( ControllableEntity* target ) |
---|
| 161 | { |
---|
| 162 | if ( target ) |
---|
| 163 | setTargetOrientation( target->getOrientation() ); |
---|
| 164 | } |
---|
| 165 | void FlyingController::boostControl() |
---|
| 166 | { |
---|
| 167 | SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); |
---|
| 168 | if(ship == NULL) return; |
---|
| 169 | if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost |
---|
| 170 | { |
---|
| 171 | |
---|
| 172 | this->getControllableEntity()->boost(true); |
---|
| 173 | } |
---|
| 174 | else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost |
---|
| 175 | { |
---|
| 176 | this->getControllableEntity()->boost(false); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | } |
---|