From 5f90a61c3d079b68d57c4f754d0c8b9ea3a79514 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 28 Jul 2018 01:00:41 +0800 Subject: [PATCH] Improve item firing sound in network --- src/items/powerup.cpp | 113 ++++++++++++++++++++++++++++-------------- src/items/powerup.hpp | 8 +-- src/karts/kart.cpp | 2 + 3 files changed, 83 insertions(+), 40 deletions(-) diff --git a/src/items/powerup.cpp b/src/items/powerup.cpp index 70f08dff3..00331d625 100644 --- a/src/items/powerup.cpp +++ b/src/items/powerup.cpp @@ -109,6 +109,23 @@ void Powerup::rewindTo(BareNetworkString *buffer) } } // rewindTo +//----------------------------------------------------------------------------- +void Powerup::update(int ticks) +{ + // Remove any sound ticks that should have played + const int remove_ticks = World::getWorld()->getTicksSinceStart() - 1000; + for (auto it = m_played_sound_ticks.begin(); + it != m_played_sound_ticks.end();) + { + if (*it < remove_ticks) + { + it = m_played_sound_ticks.erase(it); + continue; + } + break; + } +} // update + //----------------------------------------------------------------------------- /** Sets the collected items. The number of items is increased if the same * item is currently collected, otherwise replaces the existing item. It also @@ -132,7 +149,11 @@ void Powerup::set(PowerupManager::PowerupType type, int n) m_number=n; - if(m_sound_use != NULL) + // Don't re-create sound sound during rewinding + if (RewindManager::get()->isRewinding()) + return; + + if (m_sound_use != NULL) { m_sound_use->deleteSFX(); m_sound_use = NULL; @@ -160,7 +181,7 @@ void Powerup::set(PowerupManager::PowerupType type, int n) break; case PowerupManager::POWERUP_BUBBLEGUM: - m_sound_use = SFXManager::get()->createSoundSource("goo"); + m_sound_use = SFXManager::get()->createSoundSource("goo"); break ; case PowerupManager::POWERUP_SWITCH: @@ -191,7 +212,7 @@ Material *Powerup::getIcon() const //----------------------------------------------------------------------------- /** Does the sound configuration. */ -void Powerup::adjustSound() +void Powerup::adjustSound() { m_sound_use->setPosition(m_kart->getXYZ()); // in multiplayer mode, sounds are NOT positional (because we have multiple listeners) @@ -212,18 +233,19 @@ void Powerup::adjustSound() } } // adjustSound -//----------------------------------------------------------------------------- -void Powerup::playSound() -{ - if (!RewindManager::get()->isRewinding()) - m_sound_use->play(); -} // playSound - //----------------------------------------------------------------------------- /** Use (fire) this powerup. */ void Powerup::use() { + const int ticks = World::getWorld()->getTicksSinceStart(); + bool has_played_sound = false; + auto it = m_played_sound_ticks.find(ticks); + if (it != m_played_sound_ticks.end()) + has_played_sound = true; + else + m_played_sound_ticks.insert(ticks); + const KartProperties *kp = m_kart->getKartProperties(); // The player gets an achievement point for using a powerup @@ -240,7 +262,7 @@ void Powerup::use() m_kart->playCustomSFX(SFXManager::CUSTOM_SHOOT); // FIXME - for some collectibles, set() is never called - if(m_sound_use == NULL) + if (!has_played_sound && m_sound_use == NULL) { m_sound_use = SFXManager::get()->createSoundSource("shoot"); } @@ -255,8 +277,11 @@ void Powerup::use() case PowerupManager::POWERUP_SWITCH: { ItemManager::get()->switchItems(); - m_sound_use->setPosition(m_kart->getXYZ()); - playSound(); + if (!has_played_sound) + { + m_sound_use->setPosition(m_kart->getXYZ()); + m_sound_use->play(); + } break; } case PowerupManager::POWERUP_CAKE: @@ -265,9 +290,11 @@ void Powerup::use() case PowerupManager::POWERUP_PLUNGER: if(stk_config->m_shield_restrict_weapons) m_kart->setShieldTime(0.0f); // make weapon usage destroy the shield - Powerup::adjustSound(); - playSound(); - + if (!has_played_sound) + { + Powerup::adjustSound(); + m_sound_use->play(); + } projectile_manager->newProjectile(m_kart, m_type); break ; @@ -286,9 +313,11 @@ void Powerup::use() // E.g. ground not found in raycast. if(!new_item) return; - - Powerup::adjustSound(); - playSound(); + if (!has_played_sound) + { + Powerup::adjustSound(); + m_sound_use->play(); + } } else // if the kart is looking forward, use the bubblegum as a shield { @@ -329,16 +358,20 @@ void Powerup::use() } } - if (m_sound_use != NULL) + if (!has_played_sound) { - m_sound_use->deleteSFX(); - m_sound_use = NULL; - } - m_sound_use = SFXManager::get()->createSoundSource("inflate");//Extraordinary. Usually sounds are set in Powerup::set() - //In this case this is a workaround, since the bubblegum item has two different sounds. + if (m_sound_use != NULL) + { + m_sound_use->deleteSFX(); + m_sound_use = NULL; + } + //Extraordinary. Usually sounds are set in Powerup::set() + m_sound_use = SFXManager::get()->createSoundSource("inflate"); + //In this case this is a workaround, since the bubblegum item has two different sounds. - Powerup::adjustSound(); - playSound(); + Powerup::adjustSound(); + m_sound_use->play(); + } } // end of PowerupManager::POWERUP_BUBBLEGUM break; @@ -362,12 +395,15 @@ void Powerup::use() // or the kart "throwing" the anvil? Ideally it should be both. // Meanwhile, don't play it near AI karts since they obviously // don't hear anything - if(kart->getController()->isLocalPlayerController()) - m_sound_use->setPosition(kart->getXYZ()); - else - m_sound_use->setPosition(m_kart->getXYZ()); + if (!has_played_sound) + { + if(kart->getController()->isLocalPlayerController()) + m_sound_use->setPosition(kart->getXYZ()); + else + m_sound_use->setPosition(m_kart->getXYZ()); - playSound(); + m_sound_use->play(); + } break; } } @@ -422,11 +458,14 @@ void Powerup::use() // or the kart "throwing" the anvil? Ideally it should be both. // Meanwhile, don't play it near AI karts since they obviously // don't hear anything - if(m_kart->getController()->isLocalPlayerController()) - m_sound_use->setPosition(m_kart->getXYZ()); - else if(player_kart) - m_sound_use->setPosition(player_kart->getXYZ()); - playSound(); + if (!has_played_sound) + { + if(m_kart->getController()->isLocalPlayerController()) + m_sound_use->setPosition(m_kart->getXYZ()); + else if(player_kart) + m_sound_use->setPosition(player_kart->getXYZ()); + m_sound_use->play(); + } } break; diff --git a/src/items/powerup.hpp b/src/items/powerup.hpp index 4e4965f80..54d6d3bbb 100644 --- a/src/items/powerup.hpp +++ b/src/items/powerup.hpp @@ -23,7 +23,8 @@ #include "items/powerup_manager.hpp" // needed for powerup_type #include "utils/no_copy.hpp" -#include "utils/random_generator.hpp" + +#include class AbstractKart; class BareNetworkString; @@ -48,7 +49,8 @@ private: /** The owner (kart) of this powerup. */ AbstractKart* m_kart; - void playSound(); + std::set m_played_sound_ticks; + public: Powerup (AbstractKart* kart_); ~Powerup (); @@ -60,7 +62,7 @@ public: void hitBonusBox (const ItemState &item); void saveState(BareNetworkString *buffer) const; void rewindTo(BareNetworkString *buffer); - + void update(int ticks); /** Returns the number of powerups. */ int getNum () const {return m_number;} diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index 95fe09ce6..977bb4c09 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -1273,6 +1273,8 @@ void Kart::eliminate() */ void Kart::update(int ticks) { + m_powerup->update(ticks); + // Make the restitution depend on speed: this avoids collision issues, // otherwise a collision with high speed can see a kart being push // high up in the air (and out of control). So for higher speed we