| 1 | /*************************************************************************** |
| 2 | vehicle.cpp - description |
| 3 | ------------------- |
| 4 | begin : may 16th, 2004 |
| 5 | copyright : (C) 2004-2007 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: vehicle.cpp 375 2008-10-28 14:47:15Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | // Useful enumerations |
| 21 | #include "opencity_direction.h" |
| 22 | |
| 23 | // OpenCity headers |
| 24 | #include "vehicle.h" |
| 25 | |
| 26 | |
| 27 | /*=====================================================================*/ |
| 28 | Vehicle::Vehicle( |
| 29 | const VEHICLE_TYPE & type ): |
| 30 | uiCapacity( 0 ), |
| 31 | enumType( type ) |
| 32 | { |
| 33 | OPENCITY_DEBUG("ctor"); |
| 34 | |
| 35 | switch (type) { |
| 36 | case VEHICLE_STD: |
| 37 | _eGC = OC_VEHICLE_STD; |
| 38 | break; |
| 39 | case VEHICLE_BUS: |
| 40 | _eGC = OC_VEHICLE_PICKUP; |
| 41 | break; |
| 42 | case VEHICLE_SPORT: |
| 43 | _eGC = OC_VEHICLE_ROBBER; |
| 44 | break; |
| 45 | default: |
| 46 | OPENCITY_DEBUG("Error game design"); |
| 47 | assert( 0 ); |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /*=====================================================================*/ |
| 54 | Vehicle::~Vehicle() |
| 55 | { |
| 56 | OPENCITY_DEBUG("dtor"); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /*=====================================================================*/ |
| 61 | const bool |
| 62 | Vehicle::Move() |
| 63 | { |
| 64 | // Animate the vehicle each frame; |
| 65 | _fCurrentW += _fDeltaW; |
| 66 | _fCurrentL += _fDeltaL; |
| 67 | _fCurrentH += _fDeltaH; |
| 68 | |
| 69 | // Move to next WH unit if we have counted "uiFramePerUnit" times |
| 70 | if (++uiNumberOfFrame == uiFramePerUnit) { |
| 71 | // Move to direction as much time as specified by "uiTime" |
| 72 | if (this->destCurrent._uiTime > 1) { |
| 73 | --this->destCurrent._uiTime; |
| 74 | // Movement::Move2Dir( this->destCurrent ); |
| 75 | } |
| 76 | else { |
| 77 | // Use next destination |
| 78 | if (uiCurrentIndex < vdest.size()-1) { |
| 79 | ++uiCurrentIndex; |
| 80 | this->destCurrent = vdest[ uiCurrentIndex ]; |
| 81 | _fCurrentW = destCurrent._uiW; |
| 82 | _fCurrentL = destCurrent._uiL; |
| 83 | _fCurrentH = destCurrent._iHMin; |
| 84 | _CalculateDelta(); |
| 85 | } |
| 86 | else { |
| 87 | // Finished, notify the caller |
| 88 | OPENCITY_DEBUG("done"); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | //debug |
| 94 | /* |
| 95 | cout << "vehicle gogogo, W: " << fCurrentW << " / H: " << fCurrentH |
| 96 | << "dW : " << fDeltaW << " / dH: " << fDeltaH << endl; |
| 97 | */ |
| 98 | |
| 99 | // reset the number of frame |
| 100 | this->uiNumberOfFrame = 0; |
| 101 | } |
| 102 | |
| 103 | // yes, we have successfully moved |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*=====================================================================*/ |
| 109 | void |
| 110 | Vehicle::Start() |
| 111 | { |
| 112 | // reset the counted number of frame |
| 113 | this->uiNumberOfFrame = 0; |
| 114 | |
| 115 | // set the current destination to the first element of the vector |
| 116 | this->uiCurrentIndex = 0; |
| 117 | this->destCurrent = this->vdest[ this->uiCurrentIndex ]; |
| 118 | _fCurrentW = destCurrent._uiW; |
| 119 | _fCurrentL = destCurrent._uiL; |
| 120 | _fCurrentH = destCurrent._iHMin; |
| 121 | /* |
| 122 | OPENCITY_DEBUG( "W: " << destCurrent._uiW << " / L: " << destCurrent._uiL << |
| 123 | "/ H: " << _fCurrentH << " / delta: " << _fDeltaH << |
| 124 | "/ Min: " << destCurrent._iHMin << " / Max: " << destCurrent._iHMax ); |
| 125 | */ |
| 126 | _CalculateDelta(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /*=====================================================================*/ |
| 131 | void |
| 132 | Vehicle::_CalculateDelta() |
| 133 | { |
| 134 | static Destination destNext; // Static to reduce memory manipulation |
| 135 | |
| 136 | // Calculate the WH variations to go to next destination if applicable |
| 137 | if (uiCurrentIndex+1 < vdest.size()) { |
| 138 | destNext = vdest[ uiCurrentIndex+1 ]; |
| 139 | |
| 140 | // Calculate the frame per unit |
| 141 | this->uiFramePerUnit = (uint)(destNext._ubTraffic+10); |
| 142 | |
| 143 | // Calculate the differences of coordinates between each frame |
| 144 | _fDeltaW = (destNext._uiW - _fCurrentW) / uiFramePerUnit; |
| 145 | _fDeltaL = (destNext._uiL - _fCurrentL) / uiFramePerUnit; |
| 146 | _fDeltaH = (float)(this->destCurrent._iHMax - this->destCurrent._iHMin) / uiFramePerUnit; |
| 147 | |
| 148 | // Set the model's rotation angle according to the next destination |
| 149 | SetAngle( this->destCurrent ); |
| 150 | |
| 151 | // Set the slope according to the next destination |
| 152 | SetSlope( this->destCurrent, destNext ); |
| 153 | |
| 154 | /* |
| 155 | OPENCITY_DEBUG( "W: " << destCurrent._uiW << " / L: " << destCurrent._uiL << |
| 156 | "/ H: " << _fCurrentH << " / delta: " << _fDeltaH << |
| 157 | "/ Min: " << destCurrent._iHMin << " / Max: " << destCurrent._iHMax ); |
| 158 | */ |
| 159 | } |
| 160 | else { |
| 161 | this->uiFramePerUnit = OC_VEHICLE_DEFAULT_SPEED; |
| 162 | _fDeltaW = 0; |
| 163 | _fDeltaL = 0; |
| 164 | _fDeltaH = 0; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 |