| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2013 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 "carsoundeffectmanager.hpp" |
| 17 | #include "car.hpp" |
| 18 | |
| 19 | #include <MCCollisionEvent> |
| 20 | #include <MCMathUtil> |
| 21 | #include <MCPhysicsComponent> |
| 22 | #include <MCTrigonom> |
| 23 | |
| 24 | CarSoundEffectManager::CarSoundEffectManager( |
| 25 | Car & car, const CarSoundEffectManager::MultiSoundHandles & handles) |
| 26 | : m_car(car) |
| 27 | , m_gear(0) |
| 28 | , m_prevSpeed(0) |
| 29 | , m_handles(handles) |
| 30 | , m_skidPlaying(false) |
| 31 | { |
| 32 | m_hitTimer.setSingleShot(true); |
| 33 | m_hitTimer.setInterval(500); |
| 34 | |
| 35 | m_skidTimer.setSingleShot(true); |
| 36 | m_skidTimer.setInterval(100); |
| 37 | } |
| 38 | |
| 39 | void CarSoundEffectManager::startEngineSound() |
| 40 | { |
| 41 | emit playRequested(m_handles.engineSoundHandle, true); |
| 42 | emit locationChanged(m_handles.engineSoundHandle, m_car.location().i(), m_car.location().j()); |
| 43 | } |
| 44 | |
| 45 | void CarSoundEffectManager::stopEngineSound() |
| 46 | { |
| 47 | emit stopRequested(m_handles.engineSoundHandle); |
| 48 | } |
| 49 | |
| 50 | void CarSoundEffectManager::update() |
| 51 | { |
| 52 | processEngineSound(); |
| 53 | processSkidSound(); |
| 54 | } |
| 55 | |
| 56 | void CarSoundEffectManager::processEngineSound() |
| 57 | { |
| 58 | if (const int speed = static_cast<int>(m_car.absSpeed() * 10.0); speed != m_prevSpeed) |
| 59 | { |
| 60 | const float virtualRev = speed * 50; |
| 61 | const std::vector<float> gearRatios = { 1.0f, 0.8f, 0.6f, 0.5f, 0.4f, 0.3f }; |
| 62 | const float effRev = virtualRev * gearRatios[static_cast<size_t>(m_gear)]; |
| 63 | const float pitch = 1.0 + effRev / 5000; |
| 64 | |
| 65 | if (effRev > 3000) |
| 66 | { |
| 67 | if (m_gear < static_cast<int>(gearRatios.size() - 1)) |
| 68 | { |
| 69 | m_gear++; |
| 70 | } |
| 71 | } |
| 72 | else if (effRev < 1000) |
| 73 | { |
| 74 | if (m_gear) |
| 75 | { |
| 76 | m_gear--; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | m_prevSpeed = speed; |
| 81 | emit pitchChanged(m_handles.engineSoundHandle, pitch); |
| 82 | } |
| 83 | |
| 84 | emit locationChanged(m_handles.engineSoundHandle, m_car.location().i(), m_car.location().j()); |
| 85 | m_prevLocation = m_car.location(); |
| 86 | } |
| 87 | |
| 88 | void CarSoundEffectManager::processSkidSound() |
| 89 | { |
| 90 | if (m_car.isSliding()) |
| 91 | { |
| 92 | if (!m_skidTimer.isActive()) |
| 93 | { |
| 94 | emit locationChanged(m_handles.skidSoundHandle, m_car.location().i(), m_car.location().j()); |
| 95 | emit playRequested(m_handles.skidSoundHandle, false); |
| 96 | m_skidPlaying = true; |
| 97 | m_skidTimer.start(); |
| 98 | } |
| 99 | } |
| 100 | else if (m_skidPlaying) |
| 101 | { |
| 102 | emit stopRequested(m_handles.skidSoundHandle); |
| 103 | m_skidPlaying = false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void CarSoundEffectManager::collision(MCObject & collidingObject) |
| 108 | { |
| 109 | const MCVector3dF speedDiff(collidingObject.physicsComponent().velocity() - m_car.physicsComponent().velocity()); |
| 110 | if (!m_hitTimer.isActive() && speedDiff.lengthFast() > 4.0) |
| 111 | { |
| 112 | if (collidingObject.typeId() == m_car.typeId() || collidingObject.typeId() == MCObject::typeId("grandstand") || collidingObject.typeId() == MCObject::typeId("tree") || collidingObject.typeId() == MCObject::typeId("rock")) |
| 113 | { |
| 114 | emit locationChanged(m_handles.hitSoundHandle, m_car.location().i(), m_car.location().j()); |
| 115 | emit playRequested(m_handles.hitSoundHandle, false); |
| 116 | m_hitTimer.start(); |
| 117 | } |
| 118 | else if ( |
| 119 | collidingObject.typeId() == MCObject::typeId("wall") || collidingObject.typeId() == MCObject::typeId("bridgeRail") || collidingObject.typeId() == MCObject::typeId("wallLong")) |
| 120 | { |
| 121 | emit locationChanged("carHit2", m_car.location().i(), m_car.location().j()); |
| 122 | emit playRequested("carHit2", false); |
| 123 | m_hitTimer.start(); |
| 124 | } |
| 125 | else if ( |
| 126 | collidingObject.typeId() == MCObject::typeId("dustRacing2DBanner") || collidingObject.typeId() == MCObject::typeId("brake") || collidingObject.typeId() == MCObject::typeId("crate") || collidingObject.typeId() == MCObject::typeId("left") || collidingObject.typeId() == MCObject::typeId("plant") || collidingObject.typeId() == MCObject::typeId("right") || collidingObject.typeId() == MCObject::typeId("tire")) |
| 127 | { |
| 128 | emit locationChanged("carHit3", m_car.location().i(), m_car.location().j()); |
| 129 | emit playRequested("carHit3", false); |
| 130 | m_hitTimer.start(); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | CarSoundEffectManager::~CarSoundEffectManager() = default; |