feat(audio): so del sarcòfag amb pitch aleatori per obertura
This commit is contained in:
1 parent
3e31a88f78
commit
932d195eb0
11 files changed
+112
-11
No files matched your search
@@ -1,14 +1,26 @@
|
||||
#include "game/entities/concrete/sarcophagus.hpp"
|
||||
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/resources/resource_cache.hpp"
|
||||
#include "game/entities/entity_factory.hpp"
|
||||
#include "game/entities/entity_manager.hpp"
|
||||
#include "game/entities/event_bus.hpp"
|
||||
#include "utils/defaults.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
// RNG local al mòdul, mateix patró que `mummy.cpp`: llavor fixa per
|
||||
// facilitar la depuració. Si algun dia volem aleatoritat real entre
|
||||
// partides, prou amb llegir-la de std::random_device.
|
||||
auto rng() -> std::mt19937& {
|
||||
static std::mt19937 k_rng_{0xCAFEU};
|
||||
return k_rng_;
|
||||
}
|
||||
|
||||
auto buildAnimation(const fkyaml::node& yaml, const std::string& key) -> AnimationComponent {
|
||||
std::vector<int> frames = {0};
|
||||
float fps = 1.0F;
|
||||
@@ -57,6 +69,20 @@ Sarcophagus::Sarcophagus(const fkyaml::node& yaml)
|
||||
interactable_ = true;
|
||||
readTileSize(yaml, tile_size_w_, tile_size_h_);
|
||||
anim_ = anim_closed_;
|
||||
|
||||
// Sons opcionals (bloc `sound:` del YAML). El basename val tant de
|
||||
// clau al Resource::Cache com de path dins `Defaults::Paths::AUDIO_SOUNDS_DIR`.
|
||||
// Mateix patró que `mummy.hurt.sound` i `player.jump.sound`.
|
||||
if (yaml.contains("sound")) {
|
||||
const auto& s = yaml["sound"];
|
||||
if (s.contains("opening")) {
|
||||
opening_sound_key_ = s["opening"].get_value<std::string>();
|
||||
Resource::Cache::get().loadSound(opening_sound_key_, std::string{Defaults::Paths::AUDIO_SOUNDS_DIR} + opening_sound_key_);
|
||||
}
|
||||
if (s.contains("opening_pitch_spread")) {
|
||||
opening_pitch_spread_ = s["opening_pitch_spread"].get_value<float>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Sarcophagus::place(int anchor_col, int anchor_row, const BlockedPredicate& is_blocked) {
|
||||
@@ -122,6 +148,18 @@ void Sarcophagus::notifyTilePainted(int col, int row) {
|
||||
state_ = State::OPENING;
|
||||
anim_ = anim_opening_;
|
||||
if (anim_.has_value()) { anim_->reset(); }
|
||||
if (!opening_sound_key_.empty() && Audio::get() != nullptr) {
|
||||
// Variació de pitch/velocitat per evitar la fatiga d'escoltar
|
||||
// el mateix sample idèntic a cada obertura. Si l'spread és 0
|
||||
// (default), reproduïm amb ratio 1.0; en cas contrari sortegem
|
||||
// uniformement [1 - spread, 1 + spread].
|
||||
if (opening_pitch_spread_ > 0.0F) {
|
||||
std::uniform_real_distribution<float> dist(1.0F - opening_pitch_spread_, 1.0F + opening_pitch_spread_);
|
||||
Audio::get()->playSound(opening_sound_key_, Audio::Group::GAME, dist(rng()));
|
||||
} else {
|
||||
Audio::get()->playSound(opening_sound_key_, Audio::Group::GAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "external/fkyaml_node.hpp"
|
||||
@@ -83,5 +84,12 @@ class Sarcophagus : public Interactable {
|
||||
AnimationComponent anim_opening_;
|
||||
AnimationComponent anim_open_;
|
||||
|
||||
// Clau (basename del fitxer) al Resource::Cache del so que es dispara
|
||||
// a la transició CLOSED → OPENING. Buit ⇒ no sona res.
|
||||
std::string opening_sound_key_;
|
||||
// Variació uniforme del ratio de reproducció al voltant de 1.0. Cada
|
||||
// obertura sorteja [1 - spread, 1 + spread]. 0 ⇒ ratio fix a 1.0.
|
||||
float opening_pitch_spread_{0.0F};
|
||||
|
||||
bool opened_event_emitted_{false};
|
||||
};
|
||||
Reference in new issue
Block a user