diff options
author | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-06 15:52:40 +0100 |
---|---|---|
committer | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-06 15:52:40 +0100 |
commit | 2f278d36afd79efafaa5ed08ef123789b5563680 (patch) | |
tree | f6e2f4b48f1d37bad34d3cd6ef5d01861d89ad77 /game/player | |
parent | 1f799c9cab4374e75e7a629874aa922faa14dae5 (diff) |
Added footstep sound
Diffstat (limited to 'game/player')
-rw-r--r-- | game/player/PlayerAudioScript.cpp | 62 | ||||
-rw-r--r-- | game/player/PlayerAudioScript.h | 13 | ||||
-rw-r--r-- | game/player/PlayerSubScene.cpp | 15 |
3 files changed, 90 insertions, 0 deletions
diff --git a/game/player/PlayerAudioScript.cpp b/game/player/PlayerAudioScript.cpp new file mode 100644 index 0000000..6b5f630 --- /dev/null +++ b/game/player/PlayerAudioScript.cpp @@ -0,0 +1,62 @@ +#include "PlayerAudioScript.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/AudioSource.h> + +using namespace crepe; +using namespace std; + +void PlayerAudioScript::fixed_update(crepe::duration_t dt) { + Animator & animator = this->get_components_by_name<Animator>("player").front(); + + if (animator.data.col == 0) { + if (animator.data.row != this->last_row) { + if (animator.data.row == 0) { + // right footstep + if (current_footstep == 0) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(0); + audio.play(); + } else if (current_footstep == 1) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(2); + audio.play(); + } else if (current_footstep == 2) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(4); + audio.play(); + } else if (current_footstep == 3) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(6); + audio.play(); + } + } else if (animator.data.row == 2) { + // left footstep + if (current_footstep == 0) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(1); + audio.play(); + current_footstep = 1; + } else if (current_footstep == 1) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(3); + audio.play(); + current_footstep = 2; + } else if (current_footstep == 2) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(5); + audio.play(); + current_footstep = 3; + } else if (current_footstep == 3) { + AudioSource & audio + = this->get_components_by_name<AudioSource>("player_audio").at(7); + audio.play(); + current_footstep = 0; + } + } + this->last_row = animator.data.row; + } + } else { + this->last_row = -1; + } +} diff --git a/game/player/PlayerAudioScript.h b/game/player/PlayerAudioScript.h new file mode 100644 index 0000000..764cb20 --- /dev/null +++ b/game/player/PlayerAudioScript.h @@ -0,0 +1,13 @@ +#pragma once + +#include <crepe/api/Event.h> +#include <crepe/api/Script.h> + +class PlayerAudioScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); + +private: + int last_row = -1; + int current_footstep = 0; +}; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index c1e5e2f..11575c6 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -1,10 +1,12 @@ #include "PlayerSubScene.h" +#include "PlayerAudioScript.h" #include "PlayerEndScript.h" #include "PlayerScript.h" #include "../Config.h" #include <crepe/api/Animator.h> +#include <crepe/api/AudioSource.h> #include <crepe/api/BoxCollider.h> #include <crepe/api/CircleCollider.h> #include <crepe/api/GameObject.h> @@ -150,4 +152,17 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { }); player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; player.add_component<BehaviorScript>().set_script<PlayerEndScript>().active = false; + + GameObject player_audio = scn.new_object("player_audio", "player_audio", vec2(0, 0)); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_left_1.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_right_1.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_left_2.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_right_2.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_left_3.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_right_3.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_left_4.ogg")); + player_audio.add_component<AudioSource>(Asset("asset/sfx/barefoot_step_right_4.ogg")); + + player_audio.add_component<BehaviorScript>().set_script<PlayerAudioScript>().active + = false; } |