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 #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
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
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
00087 rotate( getViewAxis(), roll * age );
00088 rotate( getRightAxis(), pitch * age );
00089
00090
00091 double old_speed = speed;
00092
00093
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
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
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
00137
00138 tick_translation = getViewAxis() * speed;
00139
00140
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
00234
00235
00236
00237
00238 }
00239
00240
00241 };
00242 };
00243