Commit 17705460 authored by Michael Ochmann's avatar Michael Ochmann
Browse files

added ship audio with stereo flanging

parent f3e1379a
......@@ -18,14 +18,17 @@ namespace gdw {
TRAKTORBEAM_REVERSE_ACTIVATED,
TRAKTORBEAM_REVERSE_DEACTIVATED,
PLAY_STATE_ENTER,
PLAY_STATE_LEAVE
PLAY_STATE_LEAVE,
};
AudioEventHandler(gdw::engine& engine);
~AudioEventHandler();
void trigger(gdw::AudioEventHandler::Event event);
void playStateUpdate(float deltaTime);
private:
gdw::engine &engine;
std::unordered_map<std::string, int> channels;
bool playerMoving;
static constexpr float MinPanPercentage = 30.0f;
};
}
......
......@@ -63,6 +63,8 @@ namespace gdw {
Mix_Volume(channel, (int)(1.28f * volume));
}
void stereo(int channel, int left, int right);
int sound_volume() {
return sound_volume_;
}
......
......@@ -9,6 +9,7 @@ class game_input_manager {
public:
game_input_manager(engine& engine);
~game_input_manager();
static const float maximum_movement_distance;
void update(float dt);
private:
......
#include <audio/AudioEventHandler.hpp>
#include <audio/audio.hpp>
#include <core/engine.hpp>
#include <ecs/entity.hpp>
#include <glm/glm.hpp>
#include <physics/movement/movement_component.h>
#include <gameplay/game_play_system.h>
#include <gameplay/level_manager.h>
#include <gameplay/game_input_manager.h>
namespace gdw {
AudioEventHandler::AudioEventHandler(gdw::engine& engine) :
engine(engine){
engine(engine),
playerMoving(false){
}
......@@ -15,7 +22,7 @@ namespace gdw {
switch (event) {
case Event::PLAY_STATE_ENTER:
this->channels["background"] = this->engine.audio().queue_sound("backgroundNoise", -1);
this->engine.audio().sound_volume(5, this->channels["background"]);
this->engine.audio().sound_volume(15, this->channels["background"]);
break;
case Event::PLAY_STATE_LEAVE:
this->engine.audio().stop_sound(this->channels["background"]);
......@@ -31,4 +38,39 @@ namespace gdw {
return;
}
}
void AudioEventHandler::playStateUpdate(float deltaTime) {
float movement = glm::length(this->engine.game_play_system().level_manager().player()->component<movement_component>()->current_velocity());
if (movement > 0.4f && !this->playerMoving) {
this->playerMoving = true;
this->channels["ship"] = this->engine.audio().queue_sound("engine", -1);
this->engine.audio().sound_volume(40, this->channels["ship"]);
}
if (movement < 0.4f && this->playerMoving) {
this->playerMoving = false;
this->engine.audio().stop_sound(this->channels["ship"], 400);
}
if (this->playerMoving) {
float shipMax = gdw::game_input_manager::maximum_movement_distance;
float shipX = this->engine.game_play_system().level_manager().player()->position().x;
int left = 50;
int right = 50;
if (shipX < 0.5f) {
left = (int) ((shipX * -1) * 100 / shipMax);
right = (int) ((100 - left) / 50.0f * AudioEventHandler::MinPanPercentage + AudioEventHandler::MinPanPercentage);
left = (int) (left / 100.0f * 50.0f + 50);
left = left >= 100 ? 100 : left;
}
if (shipX > 0.5f) {
right = (int) (shipX * 100 / shipMax);
left = (int) ((100 - right) / 50.0f * AudioEventHandler::MinPanPercentage + AudioEventHandler::MinPanPercentage);
right = (int) (right / 100.0f * 50.0f + 50);
right = right >= 100 ? 100 : right;
}
log << "left :: " << left << ", right :: " << right << std::endl;
this->engine.audio().stereo(this->channels["ship"], left, right);
}
}
}
\ No newline at end of file
......@@ -106,4 +106,10 @@ namespace gdw {
}
Mix_FadeOutChannel(channel, fade_out);
}
void audio::stereo(int channel, int left, int right) {
left = (Uint8) (left * 255 / 100);
right = (Uint8) (right * 255 / 100);
Mix_SetPanning(channel, left, right);
}
}
......@@ -23,6 +23,10 @@ namespace gdw {
std::pair<const std::string, sound_ptr>
("tractorBeam", sound_manager_.load("sound/tractor_beam.ogg"))
);
sound_collection_.insert(
std::pair<const std::string, sound_ptr>
("engine", sound_manager_.load("sound/ship_engine.ogg"))
);
}
music_ptr audio_collection::music(const std::string& name) const {
......
......@@ -47,6 +47,7 @@ void play_state::on_enter() {
void play_state::update(float dt) {
engine_.game_play_system().update(dt);
this->engine_.audioEventHandler().playStateUpdate(dt);
if (this->engine_.input().isKeyPressed(SDLK_ESCAPE))
......
......@@ -19,6 +19,8 @@
namespace gdw {
const float game_input_manager::maximum_movement_distance = 13.0f;
game_input_manager::game_input_manager(engine& engine):engine_(engine) {
}
......@@ -40,7 +42,6 @@ namespace gdw {
if(!move) return;
auto& input = engine_.input();
constexpr float maximum_movement_distance = 13.f;
constexpr float rotation_speed = 7.f;
constexpr float turn_rate_modifier = 4.5f;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment