From d87217dffc1582b8dbed10da62b3b4d3f7b511de Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 31 Mar 2018 15:14:59 +0200 Subject: Footstep sounds (not finished). --- src/audio.cpp | 11 +++++++++-- src/common.hpp | 3 ++- src/game/assets.cpp | 7 +++++++ src/game/game.hpp | 4 +++- src/game/units.cpp | 6 ++++++ 5 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/audio.cpp b/src/audio.cpp index af6d24b..ff4fcd3 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -32,13 +32,19 @@ void clean_sounds(void) void sound_t::load(const char *path) { printf("load %s\n", path); - buffer.loadFromFile(path); + + auto sound = sounds.emplace(sounds.end()); + sound->loadFromFile(path); } void sound_t::play(void) { + if (!sounds.size()) + return; + auto sound = playing_sounds.emplace(playing_sounds.end()); - sound->setBuffer(buffer); + sound->setBuffer(sounds[rand() % sounds.size()]); + sound->setVolume(volume * 100.0f); sound->play(); } @@ -47,6 +53,7 @@ void ambient_t::load(const char *path) printf("load %s\n", path); sound.openFromFile(path); sound.setLoop(true); + sound.setRelativeToListener(true); ambient_sounds.push_back(this); } diff --git a/src/common.hpp b/src/common.hpp index b4c16c6..2b9b9ca 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -432,9 +432,10 @@ namespace audio { void update_ambience(bool playing); class sound_t { - sf::SoundBuffer buffer; + std::vector sounds; public: + float volume = 1.0f; void load(const char *path); void play(void); }; diff --git a/src/game/assets.cpp b/src/game/assets.cpp index f14a060..f0dddbf 100644 --- a/src/game/assets.cpp +++ b/src/game/assets.cpp @@ -43,6 +43,13 @@ void load(void) soldier.fire.load("assets/units/soldier/fire.ogg"); + soldier.step_stone.load("assets/units/soldier/step_stone_1.ogg"); + soldier.step_stone.load("assets/units/soldier/step_stone_2.ogg"); + soldier.step_stone.load("assets/units/soldier/step_stone_3.ogg"); + soldier.step_stone.load("assets/units/soldier/step_stone_4.ogg"); + soldier.step_stone.load("assets/units/soldier/step_stone_5.ogg"); + soldier.step_stone.volume = 0.4f; + spider.idle.load("assets/units/spider/idle", 2, 2, 2); spider.walking.load("assets/units/spider/walking", 2, 2, 2); spider.dead.load("assets/units/spider/dead_", 1); diff --git a/src/game/game.hpp b/src/game/game.hpp index 848ba92..bb2ce69 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -55,7 +55,7 @@ namespace game { render::oriented_sprite_4M2_t legs_idle, legs_walking; render::animated_texture_t dead; - audio::sound_t fire; + audio::sound_t fire, step_stone, step_dirt; } soldier_assets_t; typedef struct { @@ -194,6 +194,8 @@ namespace game { bool blocked; size_t attempts_left; float next_attempt; + + v2f_t last_step; } move; bool keep_moving(double speed); diff --git a/src/game/units.cpp b/src/game/units.cpp index f3d53d7..30cfdd5 100644 --- a/src/game/units.cpp +++ b/src/game/units.cpp @@ -171,6 +171,7 @@ bool unit_t::start_moving(v2f_t dst) move.dst = dst; move.path.clear(); + move.last_step = x; rep.cflags = move.cflags & ~(cmodel.cflags); rep.bounds = cmodel.bounds; @@ -454,6 +455,11 @@ void unit_soldier_t::on_think(void) panic_turn = game->now + 0.3; } } + + if (move.moving && (x - move.last_step).len() > 0.5f) { + move.last_step = x; + assets::soldier.step_stone.play(); + } } void unit_soldier_t::on_death(void) -- cgit