Skip to content

Commit 3145c3f

Browse files
authored
Add game over dialog. (#36)
* Fix class/struct mismatch, upgrade bave to v0.5.1. * Remove `IEnemyDeathListener`. * Add `ui::Dialog`. * Add game over dialog.
1 parent d4a138e commit 3145c3f

File tree

18 files changed

+159
-36
lines changed

18 files changed

+159
-36
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include(FetchContent)
1111
FetchContent_Declare(
1212
bave
1313
GIT_REPOSITORY https://github.com/karnkaul/bave
14-
GIT_TAG v0.5.0
14+
GIT_TAG v0.5.1
1515
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/bave"
1616
)
1717

assets/styles.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"grey": "#535151ff",
55
"mocha": "#6f5a48ff",
66
"milk": "#e5cdaeff",
7-
"ice": "#0xd6dbe1e1",
7+
"ice": "#d6dbe1e1",
88
"orange": "#f75c03ff",
99
"gun_beam": "#bc96e6ff"
1010
},
@@ -43,6 +43,18 @@
4343
"padding": 5
4444
}
4545
},
46+
"dialogs": {
47+
"default": {
48+
"footer_padding": [
49+
20.000000,
50+
10.000000
51+
],
52+
"corner_ratio": 0.25,
53+
"background_tint": "#e5cdaeff",
54+
"outline_tint": "#231d2aff",
55+
"content_text_tint": "#231d2aff"
56+
}
57+
},
4658
"loading_screen": {
4759
"background_tint": "#231d2aff",
4860
"spinner": {

src/spaced/spaced/game/enemy.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <bave/graphics/shape.hpp>
44
#include <bave/platform.hpp>
55
#include <spaced/game/damageable.hpp>
6-
#include <spaced/game/enemy_death.hpp>
76
#include <spaced/game/health.hpp>
87
#include <spaced/services/layout.hpp>
98
#include <spaced/services/services.hpp>

src/spaced/spaced/game/enemy_death.hpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/spaced/spaced/game/player.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void Player::tick(State const& state, Seconds const dt) {
5555

5656
for (auto const& target : state.targets) {
5757
if (is_intersecting(target->get_bounds(), ship.get_bounds())) {
58-
on_death();
58+
on_death(dt);
5959
target->force_death();
6060
return;
6161
}
@@ -92,10 +92,11 @@ void Player::set_controller(std::unique_ptr<IController> controller) {
9292
m_controller = std::move(controller);
9393
}
9494

95-
void Player::on_death() {
95+
void Player::on_death(Seconds const dt) {
9696
health = 0.0f;
9797
m_death = m_death_source;
9898
m_death->set_position(ship.transform.position);
99+
m_death->tick(dt);
99100
}
100101

101102
void Player::do_inspect() {

src/spaced/spaced/game/player.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Player : public bave::IDrawable {
3535

3636
void set_special_weapon(std::unique_ptr<Weapon> weapon) { m_arsenal.set_special(std::move(weapon)); }
3737

38+
void on_death(bave::Seconds dt);
39+
3840
void inspect() {
3941
if constexpr (bave::debug_v) { do_inspect(); }
4042
}
@@ -43,8 +45,6 @@ class Player : public bave::IDrawable {
4345
Health health{};
4446

4547
private:
46-
void on_death();
47-
4848
void do_inspect();
4949

5050
bave::Logger m_log{"Player"};

src/spaced/spaced/game/world.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ World::World(bave::NotNull<Services const*> services, bave::NotNull<IScorer*> sc
3939
m_enemy_factories["CreepFactory"] = std::make_unique<CreepFactory>(services);
4040
}
4141

42-
void World::on_death(EnemyDeath const& death) {
43-
m_scorer->add_score(death.points);
44-
45-
// temp
46-
if (random_in_range(0, 10) < 3) { debug_spawn_powerup(death.position); }
47-
// temp
48-
}
49-
5042
void World::tick(Seconds const dt) {
5143
bool const in_play = !player.health.is_dead();
5244

src/spaced/spaced/game/world.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
#include <spaced/game/target_provider.hpp>
66

77
namespace spaced {
8-
class Resources;
8+
struct Resources;
99

10-
class World : public ITargetProvider, public IEnemyDeathListener {
10+
class World : public ITargetProvider {
1111
public:
1212
explicit World(bave::NotNull<Services const*> services, bave::NotNull<IScorer*> scorer);
1313

1414
[[nodiscard]] auto get_targets() const -> std::span<bave::NotNull<IDamageable*> const> final { return m_targets; }
1515

16-
void on_death(EnemyDeath const& death) final;
17-
1816
void tick(bave::Seconds dt);
1917
void draw(bave::Shader& shader) const;
2018

src/spaced/spaced/scenes/game.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <spaced/scenes/home.hpp>
66
#include <spaced/services/scene_switcher.hpp>
77
#include <spaced/services/styles.hpp>
8+
#include <spaced/ui/button.hpp>
9+
#include <spaced/ui/dialog.hpp>
810

911
namespace spaced {
1012
using bave::Action;
@@ -59,6 +61,7 @@ void Game::tick(Seconds const dt) {
5961
auto ft = bave::DeltaTime{};
6062

6163
m_world.tick(dt);
64+
if (m_world.player.health.is_dead() && !m_game_over_dialog_pushed) { on_game_over(); }
6265

6366
if constexpr (bave::debug_v) { inspect(dt, ft.update()); }
6467
}
@@ -70,6 +73,19 @@ void Game::add_score(std::int64_t const score) {
7073
m_hud->set_score(m_score);
7174
}
7275

76+
void Game::on_game_over() {
77+
auto dci = ui::DialogCreateInfo{
78+
.size = {600.0f, 200.0f},
79+
.content_text = "GAME OVER",
80+
.main_button = {.text = "RESTART", .callback = [this] { get_services().get<ISceneSwitcher>().switch_to<Game>(); }},
81+
.second_button = {.text = "QUIT", .callback = [this] { get_app().shutdown(); }},
82+
};
83+
84+
auto dialog = std::make_unique<ui::Dialog>(get_services(), std::move(dci));
85+
m_game_over_dialog_pushed = true;
86+
push_view(std::move(dialog));
87+
}
88+
7389
void Game::inspect(Seconds const dt, Seconds const frame_time) {
7490
if constexpr (bave::imgui_v) {
7591
m_debug.fps.tick(dt);
@@ -83,6 +99,9 @@ void Game::inspect(Seconds const dt, Seconds const frame_time) {
8399
ImGui::Separator();
84100
im_text("score: {}", get_score());
85101

102+
ImGui::Separator();
103+
if (ImGui::Button("end game")) { m_world.player.on_death({}); }
104+
86105
ImGui::Separator();
87106
im_text("dt: {:05.2f}", std::chrono::duration<float, std::milli>(dt).count());
88107
im_text("fps: {}", m_debug.fps.fps);

src/spaced/spaced/scenes/game.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ class Game : public Scene, public IScorer {
2424

2525
[[nodiscard]] auto get_score() const -> std::int64_t final { return m_score; }
2626
void add_score(std::int64_t score) final;
27+
void on_game_over();
2728

2829
void inspect(bave::Seconds dt, bave::Seconds frame_time);
2930

3031
World m_world;
3132
std::int64_t m_score{};
3233
bave::Ptr<Hud> m_hud{};
34+
bool m_game_over_dialog_pushed{};
3335

3436
struct {
3537
struct {

0 commit comments

Comments
 (0)