Skip to content

Add logic for music, prefs and its UI. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add scene music, rename Home to MenuScene.
  • Loading branch information
karnkaul committed Jun 5, 2024
commit a9a868d3f0db09a5161d88f79e0855bca74c5be2
2 changes: 2 additions & 0 deletions src/spaced/spaced/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Scene : public bave::PolyPinned {
void tick_frame(bave::Seconds dt);
void render_frame() const;

[[nodiscard]] virtual auto get_music_uri() const -> std::string_view { return {}; }

[[nodiscard]] auto get_app() const -> bave::App& { return m_app; }
[[nodiscard]] auto get_services() const -> Services const& { return m_services; }

Expand Down
35 changes: 20 additions & 15 deletions src/spaced/spaced/scenes/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <bave/imgui/im_text.hpp>
#include <spaced/assets/asset_list.hpp>
#include <spaced/scenes/game.hpp>
#include <spaced/scenes/home.hpp>
#include <spaced/scenes/menu.hpp>
#include <spaced/services/scene_switcher.hpp>
#include <spaced/services/stats.hpp>
#include <spaced/services/styles.hpp>
Expand All @@ -23,11 +23,13 @@ using bave::Ptr;
using bave::Seconds;
using bave::Shader;

auto Game::get_manifest() -> AssetManifest {
auto GameScene::get_manifest() -> AssetManifest {
return AssetManifest{
.audio_clips =
{
"sfx/bubble.wav",
"music/menu.mp3",
"music/game.mp3",
},
.particle_emitters =
{
Expand All @@ -38,7 +40,7 @@ auto Game::get_manifest() -> AssetManifest {
};
}

Game::Game(App& app, Services const& services) : Scene(app, services, "Game"), m_save(&app), m_world(&services, this) {
GameScene::GameScene(App& app, Services const& services) : Scene(app, services, "Game"), m_save(&app), m_world(&services, this) {
clear_colour = services.get<Styles>().rgbas["mocha"];

auto hud = std::make_unique<Hud>(services);
Expand All @@ -49,19 +51,19 @@ Game::Game(App& app, Services const& services) : Scene(app, services, "Game"), m
++services.get<Stats>().game.play_count;
}

void Game::on_focus(FocusChange const& focus_change) { m_world.player.on_focus(focus_change); }
void GameScene::on_focus(FocusChange const& focus_change) { m_world.player.on_focus(focus_change); }

void Game::on_key(KeyInput const& key_input) {
void GameScene::on_key(KeyInput const& key_input) {
if (key_input.key == Key::eEscape && key_input.action == Action::eRelease && key_input.mods == KeyMods{}) {
get_services().get<ISceneSwitcher>().switch_to<Home>();
get_services().get<ISceneSwitcher>().switch_to<MenuScene>();
}
}

void Game::on_move(PointerMove const& pointer_move) { m_world.player.on_move(pointer_move); }
void GameScene::on_move(PointerMove const& pointer_move) { m_world.player.on_move(pointer_move); }

void Game::on_tap(PointerTap const& pointer_tap) { m_world.player.on_tap(pointer_tap); }
void GameScene::on_tap(PointerTap const& pointer_tap) { m_world.player.on_tap(pointer_tap); }

void Game::tick(Seconds const dt) {
void GameScene::tick(Seconds const dt) {
auto ft = bave::DeltaTime{};

m_world.tick(dt);
Expand All @@ -70,19 +72,19 @@ void Game::tick(Seconds const dt) {
if constexpr (bave::debug_v) { inspect(dt, ft.update()); }
}

void Game::render(Shader& shader) const { m_world.draw(shader); }
void GameScene::render(Shader& shader) const { m_world.draw(shader); }

void Game::add_score(std::int64_t const score) {
void GameScene::add_score(std::int64_t const score) {
m_score += score;
m_hud->set_score(m_score);
update_hi_score();
}

void Game::on_game_over() {
void GameScene::on_game_over() {
auto dci = ui::DialogCreateInfo{
.size = {600.0f, 200.0f},
.content_text = "GAME OVER",
.main_button = {.text = "RESTART", .callback = [this] { get_services().get<ISceneSwitcher>().switch_to<Game>(); }},
.main_button = {.text = "RESTART", .callback = [this] { get_services().get<ISceneSwitcher>().switch_to<GameScene>(); }},
.second_button = {.text = "QUIT", .callback = [this] { get_app().shutdown(); }},
};

Expand All @@ -91,13 +93,13 @@ void Game::on_game_over() {
push_view(std::move(dialog));
}

void Game::update_hi_score() {
void GameScene::update_hi_score() {
if (m_score <= m_save.get_hi_score()) { return; }
m_save.set_hi_score(m_score);
m_hud->set_hi_score(m_save.get_hi_score());
}

void Game::inspect(Seconds const dt, Seconds const frame_time) {
void GameScene::inspect(Seconds const dt, Seconds const frame_time) {
if constexpr (bave::imgui_v) {
m_debug.fps.tick(dt);

Expand All @@ -118,6 +120,9 @@ void Game::inspect(Seconds const dt, Seconds const frame_time) {
im_text("fps: {}", m_debug.fps.fps);
ImGui::SliderInt("fps limit", &m_debug.fps.limit, 5, 1000);
ImGui::Checkbox("fps lock", &m_debug.fps.lock);

ImGui::Separator();
if (ImGui::Button("reload scene")) { get_services().get<ISceneSwitcher>().switch_to<GameScene>(); }
}
ImGui::End();

Expand Down
6 changes: 4 additions & 2 deletions src/spaced/spaced/scenes/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <spaced/scene.hpp>

namespace spaced {
class Game : public Scene, public IScorer {
class GameScene : public Scene, public IScorer {
public:
static auto get_manifest() -> AssetManifest;

Game(bave::App& app, Services const& services);
GameScene(bave::App& app, Services const& services);

private:
void on_focus(bave::FocusChange const& focus_change) final;
Expand All @@ -23,6 +23,8 @@ class Game : public Scene, public IScorer {
void tick(bave::Seconds dt) final;
void render(bave::Shader& shader) const final;

[[nodiscard]] auto get_music_uri() const -> std::string_view final { return "music/game.mp3"; }

[[nodiscard]] auto get_score() const -> std::int64_t final { return m_score; }
void add_score(std::int64_t score) final;
void on_game_over();
Expand Down
8 changes: 4 additions & 4 deletions src/spaced/spaced/scenes/load_assets.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <spaced/assets/asset_list.hpp>
#include <spaced/scenes/game.hpp>
#include <spaced/scenes/home.hpp>
#include <spaced/scenes/load_assets.hpp>
#include <spaced/scenes/menu.hpp>
#include <spaced/services/resources.hpp>
#include <spaced/services/scene_switcher.hpp>
#include <spaced/util.hpp>
Expand All @@ -15,19 +15,19 @@ using bave::Shader;
namespace {
auto make_load_stages(Loader loader, Services const& services) -> std::vector<AsyncExec::Stage> {
auto asset_list = AssetList{std::move(loader), services};
asset_list.add_manifest(Game::get_manifest());
asset_list.add_manifest(GameScene::get_manifest());
auto ret = asset_list.build_task_stages();
auto& stage = ret.emplace_back();
auto& resources = services.get<Resources>();
stage.push_back(util::create_font_atlas_task(resources.main_font, Home::get_text_heights()));
stage.push_back(util::create_font_atlas_task(resources.main_font, MenuScene::get_text_heights()));
return ret;
}
} // namespace

LoadAssets::LoadAssets(App& app, Services const& services)
: Scene(app, services, "LoadAssets"), m_loading_screen(services), m_load(make_load_stages(make_loader(), services)) {}

void LoadAssets::on_loaded() { get_services().get<ISceneSwitcher>().switch_to<Home>(); }
void LoadAssets::on_loaded() { get_services().get<ISceneSwitcher>().switch_to<MenuScene>(); }

void LoadAssets::tick(Seconds const dt) {
auto const load_status = m_load.update();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <spaced/scenes/game.hpp>
#include <spaced/scenes/home.hpp>
#include <spaced/scenes/menu.hpp>
#include <spaced/services/layout.hpp>
#include <spaced/services/resources.hpp>
#include <spaced/services/scene_switcher.hpp>
Expand All @@ -13,19 +13,19 @@ using bave::App;
using bave::Seconds;
using bave::TextHeight;

auto Home::get_text_heights() -> std::vector<TextHeight> { return {TextHeight{100}, TextHeight{60}, ui::Dialog::text_height_v}; }
auto MenuScene::get_text_heights() -> std::vector<TextHeight> { return {TextHeight{100}, TextHeight{60}, ui::Dialog::text_height_v}; }

Home::Home(App& app, Services const& services) : Scene(app, services, "Home") { create_ui(); }
MenuScene::MenuScene(App& app, Services const& services) : Scene(app, services, "Home") { create_ui(); }

void Home::create_ui() {
void MenuScene::create_ui() {
auto m_header = std::make_unique<ui::Text>(get_services());
m_header->text.set_height(TextHeight{100}).set_string("Home");
m_header->text.tint = bave::white_v;

auto start = std::make_unique<ui::Button>(get_services());
start->set_text("start");
start->set_position({0.0f, -200.0f});
start->callback = [this]() { get_services().get<ISceneSwitcher>().switch_to<Game>(); };
start->callback = [this]() { get_services().get<ISceneSwitcher>().switch_to<GameScene>(); };

auto quit = std::make_unique<ui::Button>(get_services());
quit->set_text("quit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#include <spaced/scene.hpp>

namespace spaced {
class Home : public Scene {
class MenuScene : public Scene {
public:
static auto get_text_heights() -> std::vector<bave::TextHeight>;

explicit Home(bave::App& app, Services const& services);
explicit MenuScene(bave::App& app, Services const& services);

private:
void create_ui();

[[nodiscard]] auto get_music_uri() const -> std::string_view final { return "music/menu.mp3"; }
};
} // namespace spaced
10 changes: 9 additions & 1 deletion src/spaced/spaced/spaced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void Spaced::tick() {
m_layout->set_framebuffer_size(get_app().get_framebuffer_size());

if (m_scene_switcher->next_scene) {
m_audio->stop_music();
switch_track(m_scene->get_music_uri(), m_scene_switcher->next_scene->get_music_uri());
m_scene = std::move(m_scene_switcher->next_scene);
}

Expand Down Expand Up @@ -234,4 +234,12 @@ void Spaced::set_scene() {
switcher->switch_to<LoadAssets>();
m_services.bind<ISceneSwitcher>(std::move(switcher));
}

void Spaced::switch_track(std::string_view const from, std::string_view const to) const {
if (to.empty()) {
m_audio->stop_music();
} else if (from != to) {
m_audio->play_music(to);
}
}
} // namespace spaced
2 changes: 2 additions & 0 deletions src/spaced/spaced/spaced.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Spaced : public bave::Driver {
void set_layout();
void set_scene();

void switch_track(std::string_view from, std::string_view to) const;

bave::Logger m_log{"Spaced"};
Services m_services{};
bave::Ptr<ILayout> m_layout{};
Expand Down