Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

vehicle.cpp

00001 /***************************************************************************
00002                         vehicle.cpp  -  description
00003                             -------------------
00004     begin                : dim mai 16 2004
00005     modified             : jun 9 2004
00006     copyright            : (C) 2004-2006 by Duong-Khang NGUYEN
00007     email                : neoneurone @ users sourceforge net
00008 
00009     $Id: vehicle.cpp 63 2006-10-17 20:45:12Z neoneurone $
00010  ***************************************************************************/
00011 
00012 /***************************************************************************
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   any later version.                                                    *
00018  *                                                                         *
00019  ***************************************************************************/
00020 
00021 #include "vehicle.h"
00022 
00023 
00024    /*=====================================================================*/
00025 Vehicle::Vehicle():
00026 uiCapacity( 0 ),
00027 enumType( VEHICLE_BUS )
00028 {
00029     OPENCITY_DEBUG("ctor");
00030     _eGC = OC_VEHICLE_BUS;
00031 }
00032 
00033 
00034    /*=====================================================================*/
00035 Vehicle::Vehicle(
00036     const VEHICLE_TYPE & type ):
00037 uiCapacity( 0 ),
00038 enumType( type )
00039 {
00040     OPENCITY_DEBUG("ctor");
00041 
00042     switch (type) {
00043         case VEHICLE_STD:
00044             _eGC = OC_VEHICLE_STD;
00045             break;
00046         case VEHICLE_BUS:
00047             _eGC = OC_VEHICLE_PICKUP;
00048             break;
00049         case VEHICLE_SPORT:
00050             _eGC = OC_VEHICLE_ROBBER;
00051             break;
00052         default:
00053             OPENCITY_DEBUG("Error game design");
00054             assert( 0 );
00055             break;
00056     }
00057 }
00058 
00059 
00060    /*=====================================================================*/
00061 Vehicle::~Vehicle()
00062 {
00063     OPENCITY_DEBUG("dtor");
00064 }
00065 
00066 
00067    /*=====================================================================*/
00068 const bool
00069 Vehicle::Move()
00070 {
00071 // Animate the vehicle each frame;
00072     _fCurrentW += _fDeltaW;
00073     _fCurrentL += _fDeltaL;
00074     _fCurrentH += _fDeltaH;
00075 
00076 // Move to next WH unit if we have counted "uiFramePerUnit" times
00077     if (++uiNumberOfFrame == uiFramePerUnit) {
00078     // Move to direction as much time as specified by "uiTime"
00079         if (this->destCurrent._uiTime > 1) {
00080             --this->destCurrent._uiTime;
00081         //  Movement::Move2Dir( this->destCurrent );
00082         }
00083         else {
00084         // Use next destination
00085             if (uiCurrentIndex < vdest.size()-1) {
00086                 ++uiCurrentIndex;
00087                 this->destCurrent = vdest[ uiCurrentIndex ];
00088                 _fCurrentW = destCurrent._uiW;
00089                 _fCurrentL = destCurrent._uiL;
00090                 _fCurrentH = destCurrent._iHMin;
00091                 _CalculateDelta();
00092             }
00093             else {
00094             // Finished, notify the caller
00095                 OPENCITY_DEBUG("done");
00096                 return false;
00097             }
00098         }
00099 
00100 //debug
00101 /*
00102 cout << "vehicle gogogo, W: " << fCurrentW << " / H: " << fCurrentH
00103      << "dW : " << fDeltaW << " / dH: " << fDeltaH << endl;
00104 */
00105 
00106        // reset the number of frame
00107         this->uiNumberOfFrame = 0;
00108     }
00109 
00110    // yes, we have successfully moved
00111     return true;
00112 }
00113 
00114 
00115    /*=====================================================================*/
00116 void
00117 Vehicle::Start()
00118 {
00119 // reset the counted number of frame
00120     this->uiNumberOfFrame = 0;
00121 
00122 // set the current destination to the first element of the vector
00123     this->uiCurrentIndex = 0;
00124     this->destCurrent = this->vdest[ this->uiCurrentIndex ];
00125     _fCurrentW = destCurrent._uiW;
00126     _fCurrentL = destCurrent._uiL;
00127     _fCurrentH = destCurrent._iHMin;
00128 /*
00129     OPENCITY_DEBUG( "W: " << destCurrent._uiW << " / L: " << destCurrent._uiL <<
00130                     "/ H: " << _fCurrentH << " / delta: " << _fDeltaH <<
00131                     "/ Min: " << destCurrent._iHMin << " / Max: " << destCurrent._iHMax );
00132 */
00133     _CalculateDelta();
00134 }
00135 
00136 
00137    /*=====================================================================*/
00138 void
00139 Vehicle::_CalculateDelta()
00140 {
00141     static Destination destNext;        // Static to reduce memory manipulation
00142 
00143 // Calculate the WH variations to go to next destination if applicable
00144     if (uiCurrentIndex+1 < vdest.size()) {
00145         destNext = vdest[ uiCurrentIndex+1 ];
00146 
00147     // Calculate the frame per unit
00148         this->uiFramePerUnit = (uint)(destNext._ubTraffic+10);
00149 
00150     // Calculate the differences of coordinates between each frame
00151         _fDeltaW = (destNext._uiW - _fCurrentW) / uiFramePerUnit;
00152         _fDeltaL = (destNext._uiL - _fCurrentL) / uiFramePerUnit;
00153         _fDeltaH = (float)(this->destCurrent._iHMax - this->destCurrent._iHMin) / uiFramePerUnit;
00154 
00155     // Set the model's rotation angle according to the next destination
00156         SetAngle( this->destCurrent );
00157 
00158     // Set the slope according to the next destination
00159         SetSlope( this->destCurrent, destNext );
00160 
00161 /*
00162         OPENCITY_DEBUG( "W: " << destCurrent._uiW << " / L: " << destCurrent._uiL <<
00163                         "/ H: " << _fCurrentH << " / delta: " << _fDeltaH <<
00164                         "/ Min: " << destCurrent._iHMin << " / Max: " << destCurrent._iHMax );
00165 */
00166     }
00167     else {
00168         this->uiFramePerUnit = OC_VEHICLE_DEFAULT_SPEED;
00169         _fDeltaW = 0;
00170         _fDeltaL = 0;
00171         _fDeltaH = 0;
00172     }
00173 }
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 

Generated on Sat Nov 11 10:21:10 2006 for OpenCity by  doxygen 1.4.2