Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/FlyingController.cc @ 10877

Last change on this file since 10877 was 10877, checked in by gania, 8 years ago

CommonController now has static methods only. Replace with a namespace?

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