Skip to content

Commit

Permalink
Menu for starting and quitting (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
helynranta authored Oct 30, 2022
1 parent 21d91d7 commit e1c7c13
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 39 deletions.
9 changes: 9 additions & 0 deletions assets/audio/MINIMALIST.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Universal UI Soundpack

Created and distributed by Nathan Gibson (https://nathangibson.myportfolio.com)
Creation date: 27/9/2021

License: Attribution 4.0 International (CC BY 4.0)
https://creativecommons.org/licenses/by/4.0/

Support me by crediting Nathan Gibson or https://nathangibson.myportfolio.com
3 changes: 3 additions & 0 deletions assets/audio/Minimalist2.wav
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/audio/Minimalist7.wav
Git LFS file not shown
10 changes: 9 additions & 1 deletion assets/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@
"filename": "textures/ground.png"
},
{
"type": "file",
"type": "font",
"filename": "fonts/honeyblot_caps.ttf"
},
{
"type": "file",
"filename": "audio/Minimalist2.wav"
},
{
"type": "file",
"filename": "audio/Minimalist7.wav"
}
]
}
10 changes: 9 additions & 1 deletion flappykarp.project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"startup_scene":"fs1://scenes/game.scene",
"startup_scene":"fs1://scenes/load.scene",
"name": "flappy karp",
"version_string":"0.0.0",
"dynamic_libraries": ["flappykarp"],
Expand All @@ -13,5 +13,13 @@
"archive_path": "${binaryDir}/asset1.data",
"folder_path": "${projectDir}/assets/resources.json"
}
],
"fonts":
[
{
"name": "honeyblot",
"key": "fs1://fonts/honeyblot_caps.ttf",
"size": 64
}
]
}
2 changes: 2 additions & 0 deletions src/engine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include <component/transform.hpp>
#include <core/object_manager.hpp>
#include <core/scene.hpp>
#include <core/scene_orchestrator_state.hpp>
#include <core/system.hpp>
#include <core/view.hpp>
#include <imgui.hpp>
#include <engine/plugin.hpp>
#include <framework/audio_manager.hpp>
#include <plugin.hpp>
#include <resource/resource_loader.hpp>
#include <system/sdl_input_manager.hpp>
Expand Down
144 changes: 114 additions & 30 deletions src/scenes/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,72 @@

namespace lerppana::flappykarp::scenes
{
void game::draw_start_button()
{
if (game_state != game_state::stopped)
{
return;
}

static auto is_active = false;

ImGui::GroupScope text_group;
if (m_draw_button("Flap!", 70.f, is_active))
{
audio_manager->play_clip("fs1://audio/Minimalist2.wav");
reset();
}

static auto was_button_hovered = false;
auto is_hovered = ImGui::IsItemHovered();
if (!was_button_hovered && is_hovered)
{
was_button_hovered = true;
audio_manager->play_clip("fs1://audio/Minimalist7.wav");
}
else if (was_button_hovered && !is_hovered)
{
was_button_hovered = false;
}

if (is_hovered)
{
common::console::log("!");
}

is_active = ImGui::IsItemActive();
}

void game::draw_exit_button()
{
if (game_state != game_state::stopped)
{
return;
}

static auto is_active = false;

ImGui::GroupScope text_group;
if (m_draw_button("Quit", -70.f, is_active))
{
scene_orchestrator_state->request_application_quit();
}

is_active = ImGui::IsItemActive();

static auto was_button_hovered = false;
auto is_hovered = ImGui::IsItemHovered();
if (!was_button_hovered && is_hovered)
{
was_button_hovered = true;
audio_manager->play_clip("fs1://audio/Minimalist7.wav");
}
else if (was_button_hovered && !is_hovered)
{
was_button_hovered = false;
}
}

void game::fixed_update(core::dt_t dt)
{
m_detect_player_hits();
Expand All @@ -27,21 +93,17 @@ namespace lerppana::flappykarp::scenes

void game::start()
{
if (!font_manager->fonts.contains("honeyblot"))
{
font_manager->add_font(
"honeyblot",
"fs1://fonts/honeyblot_caps.ttf",
64);
}

reset();
game_state = game_state::stopped;
pipe_scroller->enabled = false;
player_controller->enabled = false;
}

void game::on_gui()
{
m_gui_draw_score();
m_gui_draw_reset_button();
draw_start_button();
draw_exit_button();
}

void game::update(core::dt_t dt)
Expand Down Expand Up @@ -76,43 +138,65 @@ namespace lerppana::flappykarp::scenes
});
}

void game::m_gui_draw_score()
bool game::m_draw_button(
std::string_view button_text,
float padding,
bool is_active)
{
auto font_size = 1.0f;

auto* font = font_manager->get_font("honeyblot");
ImGui::SetWindowFontScale(1.0f);

ImGui::FontScope font_scope{font};
ImGui::SetWindowFontScale(font_size);

static auto width = 100.0f;
ImGui::SetCursorPos(ImVec2(ImGui::GetWindowWidth() / 2 - width / 2,20.f));
static auto button_width = 100.0f;

ImGui::GroupScope text_group;
ImGui::Text("%d", (uint32_t)score);
width = ImGui::GetItemRectSize().x;
}
const auto font_height = ImGui::GetFontSize();

void game::m_gui_draw_reset_button()
{
if (game_state != game_state::stopped)
auto window_width = ImGui::GetWindowWidth();
auto window_height = ImGui::GetWindowHeight();

auto x = window_width / 2 - button_width / 2;
auto y = window_height / 2 - font_height / 2 - padding;
auto window_pos = ImGui::GetWindowPos();

ImGui::GetWindowDrawList()->AddRectFilled(
ImVec2(window_pos.x + x, window_pos.y + y + 10.f),
ImVec2(window_pos.x + x + 250.f, window_pos.y + y + 110.f),
ImGui::ColorConvertFloat4ToU32(ImVec4(.2f, .2f, .2f, 1.f)),
0.f);

if (is_active)
{
return;
y += 10.f;
}

ImGui::StyleVar style(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.5f, 0.75f));

ImGui::StyleColorScope button_color(ImGuiCol_Button, ImVec4(0.3f, 0.3f, 0.3f, 1.0f));
ImGui::StyleColorScope button_hovered(ImGuiCol_ButtonHovered, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::StyleColorScope button_active(ImGuiCol_ButtonActive, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));

ImGui::SetCursorPos(ImVec2(x,y));
auto is_clicked = ImGui::Button(button_text.data(), ImVec2(250.f, 100.f));

button_width = ImGui::GetItemRectSize().x;

return is_clicked;
}

void game::m_gui_draw_score()
{
auto* font = font_manager->get_font("honeyblot");
ImGui::SetWindowFontScale(1.0f);
ImGui::FontScope font_scope{font};

static auto width = 100.0f;
const auto font_height = ImGui::GetFontSize();
ImGui::SetCursorPos(ImVec2(
ImGui::GetWindowWidth() / 2 - width / 2,
ImGui::GetWindowHeight()/ 2 - font_height / 2));
ImGui::SetCursorPos(ImVec2(ImGui::GetWindowWidth() / 2 - width / 2,20.f));

ImGui::GroupScope text_group;
ImGui::StyleVar style(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.5f, 0.75f));
if (ImGui::Button("Reset", ImVec2(220.f, 100.f)))
{
reset();
}
ImGui::Text("%d", (uint32_t)score);
width = ImGui::GetItemRectSize().x;
}
}
18 changes: 17 additions & 1 deletion src/scenes/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ namespace lerppana::flappykarp::scenes
{
explicit game(
std::shared_ptr<vk::font_manager> font_manager,
std::shared_ptr<core::scene_orchestrator_state> scene_orchestrator_state,
std::shared_ptr<framework::audio_manager> audio_manager,
std::shared_ptr<systems::infinite_scroller> pipe_scroller,
std::shared_ptr<systems::player_controller> player_controller) :
engine::core::scene("fs1://scenes/game.scene"),
font_manager(std::move(font_manager)),
scene_orchestrator_state(std::move(scene_orchestrator_state)),
audio_manager(std::move(audio_manager)),
pipe_scroller(std::move(pipe_scroller)),
player_controller(std::move(player_controller))
{}
{
}

void draw_exit_button();

void draw_start_button();

void fixed_update(core::dt_t dt) final;

Expand All @@ -36,13 +45,20 @@ namespace lerppana::flappykarp::scenes
void on_gui() final;
private:
std::shared_ptr<vk::font_manager> font_manager;
std::shared_ptr<core::scene_orchestrator_state> scene_orchestrator_state;
std::shared_ptr<framework::audio_manager> audio_manager;
std::shared_ptr<systems::infinite_scroller> pipe_scroller;
std::shared_ptr<systems::player_controller> player_controller;

float score{};

void m_detect_player_hits();

bool m_draw_button(
std::string_view button_text,
float padding,
bool is_active);

void m_gui_draw_score();

void m_gui_draw_reset_button();
Expand Down
49 changes: 45 additions & 4 deletions src/scenes/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,50 @@

namespace lerppana::flappykarp::scenes
{
load::load() :
engine::core::scene("fs1://scenes/load.scene")
{}

void load::start() {}

void load::on_gui()
{
if (resource_loader->idle())
{
scene_orchestrator_state->request_scene_transition("fs1://scenes/game.scene");
}

auto* font = font_manager->get_font("honeyblot");

ImGui::FontScope font_scope{font};
ImGui::SetWindowFontScale(1.0f);

static auto width = 100.0f;
static auto padding = 70.f;
const auto font_height = ImGui::GetFontSize();
ImGui::SetCursorPos(ImVec2(
ImGui::GetWindowWidth() / 2 - width / 2,
ImGui::GetWindowHeight()/ 2 - font_height / 2 - padding));

ImGui::GroupScope text_group;
ImGui::StyleVar style(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.5f, 0.75f));

ImGui::Text("loading...", ImVec2(250.f, 100.f));

width = ImGui::GetItemRectSize().x;
}

void load::start()
{
resource_loader->request_load<resource::audio_resource>("fs1://audio/Minimalist2.wav");
resource_loader->request_load<resource::audio_resource>("fs1://audio/Minimalist7.wav");

resource_loader->request_load<resource::vk_mesh_resource>("fs1://models/ground.model.mesh");
resource_loader->request_load<resource::texture_resource>("fs1://textures/ground.png");

resource_loader->request_load<resource::vk_mesh_resource>("fs1://models/magikarp.model.mesh");
resource_loader->request_load<resource::vertex_bone_data_resource>("fs1://models/magikarp.skin.mesh");
resource_loader->request_load<resource::animation_resource>("fs1://models/magikarp.splash.anim");
resource_loader->request_load<resource::texture_resource>("fs1://textures/magikarp.png");

resource_loader->request_load<resource::vk_mesh_resource>("fs1://models/pipe.model.mesh");
resource_loader->request_load<resource::texture_resource>("fs1://textures/pipe.png");

}
}
24 changes: 23 additions & 1 deletion src/scenes/load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ namespace lerppana::flappykarp::scenes
{
struct FLAPPYKARP_EXPORT load : engine::core::scene
{
explicit load();
explicit load(
std::shared_ptr<resource::resource_loader> resource_loader,
std::shared_ptr<vk::font_manager> font_manager,
std::shared_ptr<core::scene_orchestrator_state> scene_orchestrator_state,
std::shared_ptr<framework::audio_manager> audio_manager) :
engine::core::scene("fs1://scenes/load.scene"),
resource_loader(std::move(resource_loader)),
font_manager(std::move(font_manager)),
scene_orchestrator_state(std::move(scene_orchestrator_state)),
audio_manager(std::move(audio_manager))
{}

void draw_exit_button();

void draw_start_button();

void on_gui() final;

void start() final;

private:
std::shared_ptr<resource::resource_loader> resource_loader;
std::shared_ptr<vk::font_manager> font_manager;
std::shared_ptr<core::scene_orchestrator_state> scene_orchestrator_state;
std::shared_ptr<framework::audio_manager> audio_manager;
};
}
Loading

0 comments on commit e1c7c13

Please sign in to comment.