| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef CAR_HPP |
| 17 | #define CAR_HPP |
| 18 | |
| 19 | #include <MCCollisionEvent> |
| 20 | #include <MCForceGenerator> |
| 21 | #include <MCObject> |
| 22 | #include <MCVector2d> |
| 23 | |
| 24 | #include <deque> |
| 25 | #include <memory> |
| 26 | |
| 27 | class CarParticleEffectManager; |
| 28 | class CarSoundEffectManager; |
| 29 | class Gearbox; |
| 30 | class MCSurface; |
| 31 | class MCFrictionGenerator; |
| 32 | class Route; |
| 33 | |
| 34 | //! Base class for race cars. |
| 35 | class Car : public MCObject |
| 36 | { |
| 37 | public: |
| 38 | enum class Steer |
| 39 | { |
| 40 | Neutral, |
| 41 | Left, |
| 42 | Right |
| 43 | }; |
| 44 | |
| 45 | //! Defines the (default) car properties. |
| 46 | struct Description |
| 47 | { |
| 48 | float accelerationFriction = 0.75f; |
| 49 | |
| 50 | float rollingFrictionOnTrack = 0.1f; |
| 51 | |
| 52 | float rotationFriction = 1.0f; |
| 53 | |
| 54 | float power = 5000.0f; |
| 55 | |
| 56 | float mass = 1500.0f; |
| 57 | |
| 58 | float restitution = 0.05f; |
| 59 | |
| 60 | float dragLinear = 1.0f; |
| 61 | |
| 62 | float dragQuadratic = 5.0f; |
| 63 | }; |
| 64 | |
| 65 | //! Constructor. |
| 66 | Car(Description & desc, std::shared_ptr<MCSurface> surface, size_t index, bool isHuman); |
| 67 | |
| 68 | //! Destructor. |
| 69 | virtual ~Car() override; |
| 70 | |
| 71 | //! Return the index. |
| 72 | size_t index() const; |
| 73 | |
| 74 | void steer(Steer direction, float control = 1.0f); |
| 75 | |
| 76 | bool isAccelerating() const; |
| 77 | |
| 78 | bool isBraking() const; |
| 79 | |
| 80 | bool isSkidding() const; |
| 81 | |
| 82 | bool isSliding(); |
| 83 | |
| 84 | void setBrakeLightState(bool state); |
| 85 | |
| 86 | //! Get estimated speed in km/h. This is used e.g. by the speedometer. |
| 87 | int speedInKmh() const; |
| 88 | |
| 89 | //! Get cached speed. |
| 90 | float absSpeed() const; |
| 91 | |
| 92 | void addDamage(float damage); |
| 93 | |
| 94 | //! \reimp |
| 95 | virtual void collisionEvent(MCCollisionEvent & event) override; |
| 96 | |
| 97 | //! \reimp |
| 98 | virtual void onStepTime(int ms) override; |
| 99 | |
| 100 | void setLeftSideOffTrack(bool state); |
| 101 | |
| 102 | bool leftSideOffTrack() const; |
| 103 | |
| 104 | void setRightSideOffTrack(bool state); |
| 105 | |
| 106 | bool rightSideOffTrack() const; |
| 107 | |
| 108 | bool isOffTrack() const; |
| 109 | |
| 110 | void setTurningImpulse(float impulse); |
| 111 | |
| 112 | //! Get location of the left front tire. |
| 113 | MCVector3dF leftFrontTireLocation() const; |
| 114 | |
| 115 | //! Get location of the right front tire. |
| 116 | MCVector3dF rightFrontTireLocation() const; |
| 117 | |
| 118 | //! Get location of the left rear tire. |
| 119 | MCVector3dF leftRearTireLocation() const; |
| 120 | |
| 121 | //! Get location of the right rear tire. |
| 122 | MCVector3dF rightRearTireLocation() const; |
| 123 | |
| 124 | bool isHuman() const; |
| 125 | |
| 126 | float tireWearFactor() const; |
| 127 | |
| 128 | float tireWearLevel() const; |
| 129 | |
| 130 | void resetTireWear(); |
| 131 | |
| 132 | float damageFactor() const; |
| 133 | |
| 134 | float damageLevel() const; |
| 135 | |
| 136 | void resetDamage(); |
| 137 | |
| 138 | bool hadHardCrash(); |
| 139 | |
| 140 | using CarSoundEffectManagerPtr = std::shared_ptr<CarSoundEffectManager>; |
| 141 | void setSoundEffectManager(CarSoundEffectManagerPtr soundEffectManager); |
| 142 | |
| 143 | CarSoundEffectManagerPtr soundEffectManager() const; |
| 144 | |
| 145 | bool hasDamage() const; |
| 146 | |
| 147 | bool hasTireWear() const; |
| 148 | |
| 149 | void setAcceleratorEnabled(bool acceleratorEnabled); |
| 150 | |
| 151 | void setBrakeEnabled(bool brakeEnabled); |
| 152 | |
| 153 | private: |
| 154 | void accelerate(bool deccelerate = false); |
| 155 | |
| 156 | void createChildObjects(float maxZ, size_t index); |
| 157 | |
| 158 | void createTires(); |
| 159 | |
| 160 | void doTireSpinEffect(); |
| 161 | |
| 162 | void initForceGenerators(Description & desc); |
| 163 | |
| 164 | void setProperties(Description & desc); |
| 165 | |
| 166 | void updateAnimations(); |
| 167 | |
| 168 | void updateTireWear(int step); |
| 169 | |
| 170 | void wearOutTires(int step, float factor); |
| 171 | |
| 172 | Description m_desc; |
| 173 | |
| 174 | MCForceGeneratorPtr m_onTrackFriction; |
| 175 | |
| 176 | bool m_leftSideOffTrack; |
| 177 | |
| 178 | bool m_rightSideOffTrack; |
| 179 | |
| 180 | bool m_skidding; |
| 181 | |
| 182 | Steer m_steer; |
| 183 | |
| 184 | size_t m_index; |
| 185 | |
| 186 | float m_tireAngle; |
| 187 | |
| 188 | float m_initDamageCapacity; |
| 189 | |
| 190 | float m_damageCapacity; |
| 191 | |
| 192 | float m_initTireWearOutCapacity; |
| 193 | |
| 194 | float m_tireWearOutCapacity; |
| 195 | |
| 196 | std::shared_ptr<MCSurface> m_frontTire; |
| 197 | |
| 198 | std::shared_ptr<MCSurface> m_brakeGlow; |
| 199 | |
| 200 | float m_length; |
| 201 | |
| 202 | int m_speedInKmh; |
| 203 | |
| 204 | float m_absSpeed; |
| 205 | |
| 206 | float m_dx, m_dy; |
| 207 | |
| 208 | bool m_isHuman; |
| 209 | |
| 210 | std::unique_ptr<CarParticleEffectManager> m_particleEffectManager; |
| 211 | |
| 212 | CarSoundEffectManagerPtr m_soundEffectManager; |
| 213 | |
| 214 | MCObjectPtr m_leftFrontTire; |
| 215 | |
| 216 | MCObjectPtr m_rightFrontTire; |
| 217 | |
| 218 | MCObjectPtr m_leftRearTire; |
| 219 | |
| 220 | MCObjectPtr m_rightRearTire; |
| 221 | |
| 222 | MCObjectPtr m_leftBrakeGlow; |
| 223 | |
| 224 | MCObjectPtr m_rightBrakeGlow; |
| 225 | |
| 226 | MCVector3dF m_numberPos; |
| 227 | |
| 228 | MCVector3dF m_leftFrontTirePos; |
| 229 | |
| 230 | MCVector3dF m_rightFrontTirePos; |
| 231 | |
| 232 | MCVector3dF m_leftRearTirePos; |
| 233 | |
| 234 | MCVector3dF m_rightRearTirePos; |
| 235 | |
| 236 | MCVector3dF m_leftBrakeGlowPos; |
| 237 | |
| 238 | MCVector3dF m_rightBrakeGlowPos; |
| 239 | |
| 240 | bool m_hadHardCrash; |
| 241 | |
| 242 | std::unique_ptr<Gearbox> m_gearbox; |
| 243 | |
| 244 | bool m_acceleratorEnabled = false; |
| 245 | |
| 246 | bool m_brakeEnabled = false; |
| 247 | |
| 248 | std::deque<std::pair<MCObject *, MCVector3dF>> m_collisionEffectStack; |
| 249 | }; |
| 250 | |
| 251 | using CarS = std::shared_ptr<Car>; |
| 252 | |
| 253 | #endif // CAR_HPP |