| 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 | #include "carparticleeffectmanager.hpp" |
| 17 | #include "car.hpp" |
| 18 | |
| 19 | #include <MCCollisionEvent> |
| 20 | #include <MCPhysicsComponent> |
| 21 | #include <MCRandom> |
| 22 | #include <cmath> |
| 23 | |
| 24 | namespace { |
| 25 | static const int SKID_MARK_DENSITY = 8; |
| 26 | static const int NEW_SKID_LIMIT = SKID_MARK_DENSITY * 4; |
| 27 | static const int ON_TRACK_ANIMATION_SPEED_MIN = 5; |
| 28 | } // namespace |
| 29 | |
| 30 | CarParticleEffectManager::CarParticleEffectManager(Car & car) |
| 31 | : m_car(car) |
| 32 | , m_smokeCounter(0) |
| 33 | , m_mudCounter(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void CarParticleEffectManager::update() |
| 38 | { |
| 39 | doOnTrackAnimations(); |
| 40 | |
| 41 | doOffTrackAnimations(); |
| 42 | |
| 43 | doDamageSmoke(); |
| 44 | } |
| 45 | |
| 46 | float CarParticleEffectManager::calculateSkidAngle(float distance, double dx, double dy) |
| 47 | { |
| 48 | return distance > NEW_SKID_LIMIT ? m_car.angle() : std::atan2(dy, dx) * 180 / 3.1415; |
| 49 | } |
| 50 | |
| 51 | void CarParticleEffectManager::doLeftSkidMark(ParticleFactory::ParticleType type) |
| 52 | { |
| 53 | const MCVector2dF skidLocation(m_car.leftRearTireLocation()); |
| 54 | if (const float distance = (m_prevLeftSkidMarkLocation - skidLocation).lengthFast(); distance > SKID_MARK_DENSITY) |
| 55 | { |
| 56 | const double dx = skidLocation.i() - m_prevLeftSkidMarkLocation.i(); |
| 57 | const double dy = skidLocation.j() - m_prevLeftSkidMarkLocation.j(); |
| 58 | const int angle = static_cast<int>(calculateSkidAngle(distance, dx, dy)); |
| 59 | ParticleFactory::instance().doParticle(type, skidLocation, MCVector3dF(0, 0, 0), angle); |
| 60 | m_prevLeftSkidMarkLocation = skidLocation; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void CarParticleEffectManager::doRightSkidMark(ParticleFactory::ParticleType type) |
| 65 | { |
| 66 | const MCVector2dF skidLocation(m_car.rightRearTireLocation()); |
| 67 | if (const float distance = (m_prevRightSkidMarkLocation - skidLocation).lengthFast(); distance > SKID_MARK_DENSITY) |
| 68 | { |
| 69 | const double dx = skidLocation.i() - m_prevRightSkidMarkLocation.i(); |
| 70 | const double dy = skidLocation.j() - m_prevRightSkidMarkLocation.j(); |
| 71 | const int angle = static_cast<int>(calculateSkidAngle(distance, dx, dy)); |
| 72 | ParticleFactory::instance().doParticle(type, skidLocation, MCVector3dF(0, 0, 0), angle); |
| 73 | m_prevRightSkidMarkLocation = skidLocation; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void CarParticleEffectManager::doDamageSmoke() |
| 78 | { |
| 79 | if (m_car.damageLevel() <= 0.3f && MCRandom::getValue() > m_car.damageLevel()) |
| 80 | { |
| 81 | MCVector3dF smokeLocation = (m_car.leftFrontTireLocation() + m_car.rightFrontTireLocation()) * 0.5f; |
| 82 | ParticleFactory::instance().doParticle(ParticleFactory::DamageSmoke, smokeLocation); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void CarParticleEffectManager::doOnTrackAnimations() |
| 87 | { |
| 88 | if ((m_car.isBraking() && m_car.speedInKmh() > ON_TRACK_ANIMATION_SPEED_MIN && m_car.speedInKmh() < 200) || m_car.isSkidding()) |
| 89 | { |
| 90 | if (!m_car.leftSideOffTrack()) |
| 91 | { |
| 92 | doLeftSkidMark(ParticleFactory::OnTrackSkidMark); |
| 93 | ParticleFactory::instance().doParticle(ParticleFactory::SkidSmoke, m_car.leftRearTireLocation(), m_car.physicsComponent().velocity() * 0.25f); |
| 94 | } |
| 95 | |
| 96 | if (!m_car.rightSideOffTrack()) |
| 97 | { |
| 98 | doRightSkidMark(ParticleFactory::OnTrackSkidMark); |
| 99 | ParticleFactory::instance().doParticle(ParticleFactory::SkidSmoke, m_car.rightRearTireLocation(), m_car.physicsComponent().velocity() * 0.25f); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void CarParticleEffectManager::doOffTrackAnimations() |
| 105 | { |
| 106 | const bool leftSideOffTrack = m_car.leftSideOffTrack(); |
| 107 | const bool rightSideOffTrack = m_car.rightSideOffTrack(); |
| 108 | const float speed = std::abs(m_car.speedInKmh()); |
| 109 | const float minSpeedMud = 15; |
| 110 | const float minSpeedSkidMark = 5; |
| 111 | const float minSpeedSmoke = 5; |
| 112 | |
| 113 | // Left skid mark |
| 114 | if (leftSideOffTrack && speed > minSpeedSkidMark) |
| 115 | { |
| 116 | doLeftSkidMark(ParticleFactory::OffTrackSkidMark); |
| 117 | } |
| 118 | |
| 119 | // Left mud particle |
| 120 | if (leftSideOffTrack && speed > minSpeedMud) |
| 121 | { |
| 122 | if (++m_mudCounter >= 5) // This is to prevent a continuous spray of mud particles |
| 123 | { |
| 124 | ParticleFactory::instance().doParticle( |
| 125 | ParticleFactory::Mud, m_car.leftRearTireLocation(), m_car.physicsComponent().velocity() * 0.5f); |
| 126 | m_mudCounter = 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Right skid mark |
| 131 | if (rightSideOffTrack && speed > minSpeedSkidMark) |
| 132 | { |
| 133 | doRightSkidMark(ParticleFactory::OffTrackSkidMark); |
| 134 | } |
| 135 | |
| 136 | // Right mud particle |
| 137 | if (rightSideOffTrack && speed > minSpeedMud) |
| 138 | { |
| 139 | if (++m_mudCounter >= 5) // This is to prevent a continuous spray of mud particles |
| 140 | { |
| 141 | ParticleFactory::instance().doParticle( |
| 142 | ParticleFactory::Mud, m_car.rightRearTireLocation(), m_car.physicsComponent().velocity() * 0.5f); |
| 143 | m_mudCounter = 0; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Smoke |
| 148 | if ((leftSideOffTrack || rightSideOffTrack) && speed > minSpeedSmoke) |
| 149 | { |
| 150 | if (++m_smokeCounter >= 2) // This is to prevent a continuous spray of smoke particles |
| 151 | { |
| 152 | MCVector3dF smokeLocation = (m_car.leftRearTireLocation() + m_car.rightRearTireLocation()) * 0.5f; |
| 153 | ParticleFactory::instance().doParticle(ParticleFactory::OffTrackSmoke, smokeLocation); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void CarParticleEffectManager::collision(size_t typeId, const MCVector3dF & contactPoint) |
| 159 | { |
| 160 | if (m_car.physicsComponent().velocity().lengthFast() > 4.0f) |
| 161 | { |
| 162 | // Check if the car is colliding with another car. |
| 163 | const size_t particles = 10; |
| 164 | if (typeId == m_car.typeId()) |
| 165 | { |
| 166 | for (size_t i = 0; i < particles; i++) |
| 167 | { |
| 168 | ParticleFactory::instance().doParticle(ParticleFactory::Sparkle, |
| 169 | contactPoint, m_car.physicsComponent().velocity() * 0.75f); |
| 170 | } |
| 171 | } |
| 172 | // Check if the car is colliding with hard stationary objects. |
| 173 | else if (typeId == MCObject::typeId("crate") || typeId == MCObject::typeId("dustRacing2DBanner") || typeId == MCObject::typeId("grandstand") || typeId == MCObject::typeId("wall") || typeId == MCObject::typeId("wallLong") || typeId == MCObject::typeId("rock")) |
| 174 | { |
| 175 | for (size_t i = 0; i < particles; i++) |
| 176 | { |
| 177 | ParticleFactory::instance().doParticle(ParticleFactory::Sparkle, |
| 178 | contactPoint, m_car.physicsComponent().velocity() * 0.5f); |
| 179 | } |
| 180 | } |
| 181 | else if (typeId == MCObject::typeId("tree")) |
| 182 | { |
| 183 | ParticleFactory::instance().doParticle(ParticleFactory::Leaf, |
| 184 | contactPoint, m_car.physicsComponent().velocity() * 0.1f); |
| 185 | } |
| 186 | } |
| 187 | } |