00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00052 if ( joyDirection_.NormSquared() > .5 )
00053 {
00054
00055 if ( driveDirection_.NormSquared() < .1 )
00056 {
00057
00058
00059 driveDirection_ = cycle_->Direction();
00060 driveDirection_.Normalize();
00061 }
00062
00063
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
00075 cameraDirection_ = driveDirection_.Turn( dir.Conj() );
00076 }
00077 else
00078 {
00079
00080 driveDirection_ = cameraDirection_.Turn( dir );
00081 }
00082
00083
00084 turnRequested_ = true;
00085
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
00100
00101
00102
00103 driveDirection_ = tCoord();
00104 cameraDirection_ = tCoord();
00105 return;
00106 }
00107
00108
00109 if ( !turnRequested_ )
00110 {
00111 return;
00112 }
00113
00114
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
00125 leftTurn.Normalize();
00126 rightTurn.Normalize();
00127 straightOn.Normalize();
00128
00129
00130 REAL left = ( leftTurn - driveDirection_ ).NormSquared();
00131 REAL right = ( rightTurn - driveDirection_ ).NormSquared();
00132 REAL straight = ( straightOn - driveDirection_ ).NormSquared();
00133
00134
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
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 }