Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10872 was 10871, checked in by gania, 9 years ago

Split up CommonController, so it is easier to debug

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