src/tron/gJoystick.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2005  by Manuel Moos
00007 and the AA DevTeam (see the file AUTHORS(.txt) in the main source directory)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024 
00025 ***************************************************************************
00026 
00027 */
00028 
00029 #include "gJoystick.h"
00030 #include "gCycleMovement.h"
00031 #include "uInput.h"
00032 #include "eGrid.h"
00033 #include "tSysTime.h"
00034 
00035 static uActionPlayer sg_JoyLeft("JOY_LEFT", -4);
00036 static uActionPlayer sg_JoyRight("JOY_RIGHT", -4);
00037 static uActionPlayer sg_JoyUp("JOY_UP", -3);
00038 static uActionPlayer sg_JoyDown("JOY_DOWN", -3);
00039 static uActionPlayer sg_JoyGlance("JOY_GLANCE", -2);
00040 
00042 bool gJoystick::Act( uActionPlayer * act, REAL value )
00043 {
00044     if ( joyDirection_.NormSquared() > .5 )
00045     {
00046         lastCommand_ = tSysTimeFloat();
00047     }
00048 
00049     if ( ActInternal( act, value ) )
00050     {
00051         // update directions
00052         if ( joyDirection_.NormSquared() > .5 )
00053         {
00054             // fetch driving direction if required
00055             if ( driveDirection_.NormSquared() < .1 )
00056             {
00057                 // con << joyDirection_ << "\n";
00058 
00059                 driveDirection_ = cycle_->Direction();
00060                 driveDirection_.Normalize();
00061             }
00062 
00063             // fetch camera direction if required
00064             if ( cameraDirection_.NormSquared() < .1 )
00065             {
00066                 cameraDirection_ = cycle_->CamDir();
00067                 cameraDirection_.Normalize();
00068             }
00069 
00070             tCoord dir = joyDirection_;
00071             dir.Normalize();
00072             if ( glance_ )
00073             {
00074                 // adapt viewing direction to driving direction
00075                 cameraDirection_ = driveDirection_.Turn( dir.Conj() );
00076             }
00077             else
00078             {
00079                 // adapt driving direction to view direction
00080                 driveDirection_ = cameraDirection_.Turn( dir );
00081             }
00082 
00083             // possibly turn
00084             turnRequested_ = true;
00085             // Turn();
00086         }
00087 
00088         return true;
00089     }
00090 
00091     return false;
00092 }
00093 
00095 void gJoystick::Turn()
00096 {
00097     if ( joyDirection_.NormSquared() < .1 && lastCommand_ < tSysTimeFloat() - .03 )
00098     {
00099         // if ( driveDirection_.NormSquared() > .5 )
00100         // con << joyDirection_ << "\n";
00101 
00102         // joystick was released, reeset driving direction and camera direction.
00103         driveDirection_ = tCoord();
00104         cameraDirection_ = tCoord();
00105         return;
00106     }
00107 
00108     // nothing to do
00109     if ( !turnRequested_ )
00110     {
00111         return;
00112     }
00113 
00114     // fetch current and possible turn driving directions
00115     int winding = cycle_->WindingNumber();
00116     int windingLeft = winding, windingRight = winding;
00117     eGrid * grid = cycle_->Grid();
00118     grid->Turn( windingLeft, -1 );
00119     grid->Turn( windingRight, +1 );
00120     eCoord leftTurn = grid->GetDirection( windingLeft );
00121     eCoord rightTurn = grid->GetDirection( windingRight );
00122     eCoord straightOn = grid->GetDirection( winding );
00123 
00124     // normalize them (axes may be unnormalized)
00125     leftTurn.Normalize();
00126     rightTurn.Normalize();
00127     straightOn.Normalize();
00128 
00129     // calculate distances between desired and possible driving directions
00130     REAL left     = ( leftTurn - driveDirection_ ).NormSquared();
00131     REAL right    = ( rightTurn - driveDirection_ ).NormSquared();
00132     REAL straight = ( straightOn - driveDirection_ ).NormSquared();
00133 
00134     // possibly turn
00135     if ( left < right && left < straight * ( lastTurn_ != 1 ? 2 : .8 ) )
00136     {
00137         if ( cycle_->CanMakeTurn( -1 ) )
00138         {
00139             cycle_->Turn( -1 );
00140             lastTurn_ = -1;
00141         }
00142     }
00143     else if ( left > right && right < straight * ( lastTurn_ != -1 ? 2 : .8 )  )
00144     {
00145         if( cycle_->CanMakeTurn( +1 ) )
00146         {
00147             cycle_->Turn( +1 );
00148             lastTurn_ = +1;
00149         }
00150     }
00151     else
00152     {
00153         // not much use repeating this calculation until new input arrives
00154         turnRequested_ = false;
00155         lastTurn_ = 0;
00156     }
00157 }
00158 
00159 bool gJoystick::ActInternal( uActionPlayer * act, REAL value )
00160 {
00161     if ( act == & sg_JoyUp )
00162     {
00163         joyDirection_.x = value;
00164         return true;
00165     }
00166     if ( act == & sg_JoyDown )
00167     {
00168         joyDirection_.x = -value;
00169         return true;
00170     }
00171     if ( act == & sg_JoyLeft )
00172     {
00173         joyDirection_.y = value;
00174         return true;
00175     }
00176     if ( act == & sg_JoyRight )
00177     {
00178         joyDirection_.y = -value;
00179         return true;
00180     }
00181     if ( act == & sg_JoyGlance )
00182     {
00183         glance_ = ( value > .5 );
00184         return true;
00185     }
00186 
00187     return false;
00188 }

Generated on Sat Mar 15 22:56:08 2008 for Armagetron Advanced by  doxygen 1.5.4