Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

ShipControls.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: ShipControls.cpp,v 1.1 2002/01/08 20:47:05 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SpaceGame/Ship.h"
00026 #include "Teddy/SpaceGame/ShipType.h"
00027 
00028 
00029 namespace Teddy     {
00030 namespace SpaceGame {
00031 
00032 
00034 void Ship::applyControls( float age ){
00035     float accel_const = ship_type->getAcceleration();
00036     float pitch_const = ship_type->getPitchConst  ();
00037     float roll_const  = ship_type->getRollConst   ();
00038     float max_pitch   = ship_type->getMaxPitch    ();
00039     float max_roll    = ship_type->getMaxRoll     ();
00040     float max_speed   = ship_type->getMaxSpeed    ();
00041     
00042     //  Damp pitch
00043     if( !active_pitch ){
00044         if( pitch > pitch_const ){
00045             pitch -= pitch_const*age; 
00046             if( pitch < pitch_const ){
00047                 pitch = 0;
00048             }
00049         }else if( pitch < -pitch_const ){
00050             pitch += pitch_const*age;
00051             if( pitch > -pitch_const ){
00052                 pitch = 0;
00053             }
00054         }
00055     }else{
00056         pitch += pitch_delta * age;
00057         if( pitch>max_pitch ){
00058             pitch = max_pitch;
00059         }else if( pitch<-max_pitch ){
00060             pitch = -max_pitch;
00061         }
00062     }
00063 
00064     //  Damp roll
00065     if( !active_roll ){
00066         if( roll > roll_const ){
00067             roll -= roll_const*age;
00068             if( roll < roll_const ){
00069                 roll = 0;
00070             }
00071         }else if( roll < -roll_const ){
00072             roll += roll_const*age;
00073             if( roll > -roll_const ){
00074                 roll = 0;
00075             }
00076         }
00077     }else{
00078         roll += roll_delta * age;
00079         if( roll>max_roll ){
00080             roll = max_roll;
00081         }else if( roll<-max_roll ){
00082             roll = -max_roll;
00083         }
00084     }
00085 
00086     //  Apply pitch and roll
00087     rotate( getViewAxis(),  roll  * age );
00088     rotate( getRightAxis(), pitch * age );
00089     
00090     //  ----- Control Speed -----
00091     double old_speed = speed;
00092 
00093     //  Faster
00094     if( control_more_speed ){
00095         if( speed < max_speed ){
00096             speed += accel_const * age;
00097             if(  (old_speed < 0) && 
00098                  (speed     > 0)    )
00099             {
00100                 speed = 0;
00101             }else if( speed > max_speed ){
00102                 speed = max_speed;
00103             }
00104         }
00105     }
00106 
00107     //  Slower
00108     if( control_less_speed ){
00109         if( speed > -max_speed ){
00110             speed -= accel_const * age;
00111             if(  (old_speed > 0) &&
00112                  (speed     < 0)    )
00113             {
00114                 speed = 0;
00115             }else if( speed< -max_speed ){
00116                 speed = -max_speed;
00117             }
00118         }
00119     }
00120 
00121     //  Stop
00122     if( control_stop ){
00123         if( speed < 0 ){
00124             speed += accel_const * age;
00125             if( speed > 0 ){
00126                 speed = 0;
00127             }
00128         }else if( speed > 0 ){
00129             speed -= accel_const * age;
00130             if( speed < 0 ){
00131                 speed = 0;
00132             }
00133         }
00134     }
00135 
00136     //  Fully controlled 'Elite' flight mode - go where nose points...
00137 //  if( flight_mode == FM_ELITE ){
00138         tick_translation = getViewAxis() * speed;
00139 /*}else{
00140         tick_translation_delta = getViewAxis() * speed;
00141     }*/
00142 }
00143 
00144 
00146 void Ship::controlPitchUp( bool apply ){
00147     float pitch_const = ship_type->getPitchConst();
00148     control_pitch_up  = apply;
00149 
00150     if( control_pitch_up ){
00151         active_pitch =  true;
00152         pitch_delta  =  pitch_const;
00153     }else if( control_pitch_down ){
00154         pitch_delta  = -pitch_const;
00155     }else{
00156         active_pitch =  false;
00157         pitch_delta  =  0;
00158     }
00159 }
00160 
00161 
00163 void Ship::controlPitchDown( bool apply ){
00164     float pitch_const  = ship_type->getPitchConst();
00165     control_pitch_down = apply;
00166 
00167     if( control_pitch_down ){
00168         active_pitch =  true;
00169         pitch_delta  = -pitch_const;
00170     }else if( control_pitch_up ){
00171         pitch_delta  =  pitch_const;
00172     }else{
00173         active_pitch =  false;
00174         pitch_delta  =  0;
00175     }
00176 }
00177 
00178 
00180 void Ship::controlRollRight( bool apply ){
00181     float roll_const  = ship_type->getRollConst();
00182     control_roll_left = apply;
00183 
00184     if( control_roll_left ){
00185         active_roll =  true;
00186         roll_delta  =  roll_const;
00187     }else if( control_roll_right ){
00188         roll_delta  = -roll_const;
00189     }else{
00190         active_roll =  false;
00191         roll_delta  =  0;
00192     }
00193 }
00194 
00195 
00197 void Ship::controlRollLeft( bool apply ){
00198     float roll_const   = ship_type->getRollConst();
00199     control_roll_right = apply;
00200 
00201     if( control_roll_right ){
00202         active_roll =  true;
00203         roll_delta  = -roll_const;
00204     }else if( control_roll_left ){
00205         roll_delta  =  roll_const;
00206     }else{
00207         active_roll =  false;
00208         roll_delta  =  0;
00209     }
00210 }
00211 
00212 
00214 void Ship::controlMoreSpeed( bool apply ){
00215     control_more_speed = apply;
00216 }
00217 
00218 
00220 void Ship::controlLessSpeed( bool apply ){
00221     control_less_speed = apply;
00222 }
00223 
00224 
00226 void Ship::controlStop( bool apply ){
00227     control_stop = apply;
00228 }
00229 
00231 void Ship::controlFireWeapon( bool apply ){
00232     control_fire_weapon = apply;
00233 /*  if( apply == false ){
00234         controlMoreSpeed( false ); controlLessSpeed( false );
00235         controlRollRight( false ); controlRollLeft ( false );
00236         controlPitchUp  ( false ); controlPitchDown( false );
00237     }*/
00238 }
00239 
00240 
00241 };  //  namespace SpaceGame
00242 };  //  namespace Teddy
00243