Add UI sounds

This commit is contained in:
Nikita Lisitsa 2024-08-20 17:02:32 +03:00
parent 8fa1ab53b8
commit 33cc157643

View file

@ -17,6 +17,18 @@
#include <psemek/log/log.hpp>
#include <psemek/audio/engine.hpp>
#include <psemek/audio/combine/mixer.hpp>
#include <psemek/audio/effect/compressor.hpp>
#include <psemek/audio/effect/volume.hpp>
#include <psemek/audio/wave/karplus_strong.hpp>
#include <psemek/audio/wave/sine.hpp>
#include <psemek/audio/wave/triangle.hpp>
#include <psemek/audio/effect/filter.hpp>
#include <psemek/audio/effect/fade_in.hpp>
#include <psemek/audio/effect/fade_out.hpp>
#include <psemek/audio/effect/pitch.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/container/flat_map.hpp>
@ -1327,7 +1339,12 @@ namespace gmtk
{
application(options const &, context const & context)
: context_(context)
, audio_(psemek::audio::make_engine())
{
mixer_ = audio::make_mixer();
volume_ = audio::volume(mixer_);
audio_->output()->stream(audio::compressor(volume_));
context_.windowed(is_windowed_);
set_start_menu();
}
@ -1390,7 +1407,10 @@ namespace gmtk
if (selected_button_)
{
if (*selected_button_ < menu_buttons_.size() && menu_buttons_[*selected_button_].action)
{
button_click_sound();
menu_buttons_[*selected_button_].action();
}
}
else if (selected_ && active_card_)
{
@ -1398,6 +1418,7 @@ namespace gmtk
{
if (*active_card_ == card_type::eraser)
{
destroy_sound();
clear_tile(map_, *selected_);
}
else if (auto t = map_.world->get(*entity).get_if<transformer>())
@ -1406,6 +1427,7 @@ namespace gmtk
{
if (map_.take_card(*active_card_))
{
built_sound();
map_.put_card(transformer_to_card(t->type));
t->type = *n;
active_card_ = std::nullopt;
@ -1457,7 +1479,10 @@ namespace gmtk
}
if (built)
{
active_card_ = std::nullopt;
built_sound();
}
if (built && tutorial_state_ <= 3)
tutorial_state_ = 4;
@ -1465,7 +1490,10 @@ namespace gmtk
}
}
else if (selected_card_)
{
active_card_ = *selected_card_;
button_click_sound();
}
else if (selected_item_)
{
item_killing_spree_ = true;
@ -1473,7 +1501,10 @@ namespace gmtk
tutorial_state_ = 5;
}
else if (selected_ && !belt_start_)
{
belt_start_ = *selected_;
button_mouseover_sound();
}
}
if (!event.down && event.button == app::mouse_button::left)
@ -1505,16 +1536,22 @@ namespace gmtk
{
for (int i = 0; i < 3; ++i)
remove_belt(*map_.world, e[i], e[i + 1]);
remove_belt_sound();
}
else if (sv.belts_from.contains(e[1]))
{
for (int i = 0; i < 3; ++i)
remove_belt(*map_.world, e[i + 1], e[i]);
remove_belt_sound();
}
else
{
for (int i = 0; i < 3; ++i)
add_belt(*map_.world, e[i], e[i + 1]);
add_belt_sound();
}
if (tutorial_state_ <= 0)
@ -1892,6 +1929,7 @@ namespace gmtk
1.f - mouse_[1] * 1.f / screen_size_[1]
);
auto old_selected = selected_;
selected_ = std::nullopt;
selected_item_ = std::nullopt;
@ -1954,6 +1992,11 @@ namespace gmtk
else if (!within_grid(p))
if (auto entity = map_.world->index<index>().find(p))
selected_ = p;
if (belt_start_ && selected_ && old_selected != selected_)
{
button_mouseover_sound();
}
}
}
@ -2159,6 +2202,7 @@ namespace gmtk
}
}
auto old_selected_card = selected_card_;
selected_card_ = std::nullopt;
{
@ -2183,6 +2227,8 @@ namespace gmtk
else if (geom::contains(box, mouse_world_))
{
selected_card_ = type;
if (old_selected_card != type)
button_mouseover_sound();
draw_selection(box, painter_, {128, 128, 128, 255});
}
@ -2274,6 +2320,7 @@ namespace gmtk
painter_.text(pen, is_sandbox_mode_ ? "Sandbox" : is_challenge_mode_ ? "Challenge" : "Campaign", opts);
}
auto old_selected_button = selected_button_;
selected_button_ = std::nullopt;
if (in_menu())
@ -2304,7 +2351,11 @@ namespace gmtk
box = geom::expand(box, menu_buttons_[i].selected_state * 8.f * pixel_size);
if (geom::contains(box, mouse_world_))
{
selected_button_ = i;
if (old_selected_button != i)
button_mouseover_sound();
}
bg_color = gfx::lerp(gfx::color_rgba{192, 192, 192, 255}, gfx::white.as_color_rgba(), menu_buttons_[i].selected_state);
text_color = gfx::lerp(gfx::black.as_color_rgba(), gfx::color_rgba{191, 96, 0, 255}, menu_buttons_[i].selected_state);
@ -2331,6 +2382,13 @@ namespace gmtk
}
private:
context const & context_;
bool is_windowed_ = false;
std::unique_ptr<audio::engine> audio_;
audio::mixer_ptr mixer_;
std::shared_ptr<audio::volume_control> volume_;
bool running_ = true;
bool in_start_menu_ = true;
@ -2452,9 +2510,6 @@ namespace gmtk
menu_buttons_.push_back({"Exit", [this]{ stop(); }});
}
context const & context_;
bool is_windowed_ = false;
random::generator map_rng_;
random::generator item_rng_;
map map_;
@ -2490,6 +2545,42 @@ namespace gmtk
util::hash_map<card_type, float> card_animation_;
int tutorial_state_ = 0;
void button_mouseover_sound()
{
mixer_->add(audio::volume(audio::fade_out(audio::fade_in(audio::triangle_wave(110.f), 0.00625f), 0.05f), 0.25f));
}
void button_click_sound()
{
mixer_->add(audio::volume(audio::fade_out(audio::fade_in(audio::triangle_wave(220.f), 0.00625f), 0.1f), 0.25f));
}
void built_sound()
{
auto base = audio::fade_out(audio::fade_in(audio::triangle_wave(110.f), 0.00625f), 0.5f);
auto pitch = audio::pitch(base, 1.f, 0.5f);
pitch->pitch(1.5f);
mixer_->add(audio::volume(pitch, 0.25f));
}
void add_belt_sound()
{
mixer_->add(audio::volume(audio::fade_out(audio::fade_in(audio::sine_wave(165.f), 1/160.f), 1/40.f, 1/160.f), 0.25f));
}
void remove_belt_sound()
{
mixer_->add(audio::volume(audio::fade_out(audio::fade_in(audio::sine_wave(110.f), 1/160.f), 1/20.f, 1/160.f), 0.25f));
}
void destroy_sound()
{
auto base = audio::fade_out(audio::fade_in(audio::triangle_wave(110.f), 0.00625f), 0.25f);
auto pitch = audio::pitch(base, 1.f, 0.25f);
pitch->pitch(0.666f);
mixer_->add(audio::volume(pitch, 0.25f));
}
};
}