Skip to content

GameScene subclass approach. #51

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 11 commits into from
Jun 26, 2024
Next Next commit
Replace IScorer with SigPlayerScored.
  • Loading branch information
karnkaul committed Jun 24, 2024
commit 70e2f31c9cdcb1c730abd51f5a9018de39f3a631
11 changes: 0 additions & 11 deletions src/spaced/spaced/game/scorer.hpp

This file was deleted.

9 changes: 5 additions & 4 deletions src/spaced/spaced/game/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <bave/services/styles.hpp>
#include <spaced/game/enemies/creep_factory.hpp>
#include <spaced/game/world.hpp>
#include <spaced/services/game_signals.hpp>
#include <spaced/services/layout.hpp>
#include <spaced/services/stats.hpp>

Expand All @@ -22,9 +23,9 @@ using bave::Shader;
using bave::Styles;
using bave::Texture;

World::World(bave::NotNull<Services const*> services, bave::NotNull<IScorer*> scorer)
: m_services(services), m_resources(&services->get<Resources>()), m_audio(&services->get<IAudio>()), m_stats(&services->get<Stats>()), m_scorer(scorer),
m_star_field(*services) {
World::World(bave::NotNull<Services const*> services)
: m_services(services), m_resources(&services->get<Resources>()), m_audio(&services->get<IAudio>()), m_stats(&services->get<Stats>()),
m_on_player_scored(&services->get<GameSignals>().player_scored), m_star_field(*services) {
m_enemy_factories["CreepFactory"] = std::make_unique<CreepFactory>(services);

auto const& resources = services->get<Resources>();
Expand Down Expand Up @@ -89,7 +90,7 @@ void World::on_death(Enemy const& enemy, bool const add_score) {
m_audio->play_any_sfx(enemy.death_sfx);

if (add_score) {
m_scorer->add_score(enemy.points);
m_on_player_scored->dispatch(enemy.points);
++m_stats->player.enemies_poofed;

// temp
Expand Down
6 changes: 3 additions & 3 deletions src/spaced/spaced/game/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <spaced/game/damageable.hpp>
#include <spaced/game/enemy_factory.hpp>
#include <spaced/game/powerup.hpp>
#include <spaced/game/scorer.hpp>
#include <spaced/game/star_field.hpp>
#include <spaced/game/target_provider.hpp>

Expand All @@ -12,10 +11,11 @@ struct Resources;

namespace spaced {
struct Stats;
struct SigPlayerScored;

class World : public ITargetProvider {
public:
explicit World(bave::NotNull<bave::Services const*> services, bave::NotNull<IScorer*> scorer);
explicit World(bave::NotNull<bave::Services const*> services);

[[nodiscard]] auto get_targets() const -> std::span<bave::NotNull<IDamageable*> const> final { return m_targets; }
[[nodiscard]] auto get_powerups() const -> std::span<bave::NotNull<IPowerup*> const> { return m_powerups; }
Expand All @@ -39,7 +39,7 @@ class World : public ITargetProvider {
bave::NotNull<bave::Resources const*> m_resources;
bave::NotNull<bave::IAudio*> m_audio;
bave::NotNull<Stats*> m_stats;
bave::NotNull<IScorer*> m_scorer;
bave::NotNull<SigPlayerScored*> m_on_player_scored;

bave::CustomShape m_background{};
StarField m_star_field;
Expand Down
20 changes: 11 additions & 9 deletions src/spaced/spaced/scenes/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <spaced/game/controllers/player_controller.hpp>
#include <spaced/scenes/game.hpp>
#include <spaced/scenes/menu.hpp>
#include <spaced/services/game_signals.hpp>
#include <spaced/services/stats.hpp>

namespace spaced {
Expand Down Expand Up @@ -48,7 +49,14 @@ namespace {
}
} // namespace

GameScene::GameScene(App& app, Services const& services) : Scene(app, services, "Game"), m_save(&app) { clear_colour = services.get<Styles>().rgbas["mocha"]; }
GameScene::GameScene(App& app, Services const& services) : Scene(app, services, "Game"), m_save(&app) {
clear_colour = services.get<Styles>().rgbas["mocha"];
m_on_player_scored = services.get<GameSignals>().player_scored.connect([this](std::int64_t const points) {
m_score += points;
m_hud->set_score(m_score);
update_hi_score();
});
}

auto GameScene::get_asset_manifest() -> AssetManifest {
return AssetManifest{
Expand Down Expand Up @@ -96,7 +104,7 @@ void GameScene::on_loaded() {
void GameScene::start_play() {
auto const& services = get_services();

m_world.emplace(&services, this);
m_world.emplace(&services);
m_player.emplace(services, make_player_controller(services));

m_score = 0;
Expand Down Expand Up @@ -141,12 +149,6 @@ void GameScene::render(Shader& shader) const {
m_player->draw(shader);
}

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

void GameScene::on_player_death() {
if (m_spare_lives > 0) {
--m_spare_lives;
Expand Down Expand Up @@ -201,7 +203,7 @@ void GameScene::inspect(Seconds const dt, Seconds const frame_time) {
}

ImGui::Separator();
im_text("score: {}", get_score());
im_text("score: {}", m_score);

ImGui::Separator();
if (ImGui::Button("end game")) { m_player->on_death(dt); }
Expand Down
9 changes: 4 additions & 5 deletions src/spaced/spaced/scenes/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <spaced/game/game_save.hpp>
#include <spaced/game/hud.hpp>
#include <spaced/game/player.hpp>
#include <spaced/game/scorer.hpp>
#include <spaced/game/target_provider.hpp>
#include <spaced/game/world.hpp>
#include <spaced/signal.hpp>

namespace spaced {
class GameScene : public bave::Scene, public IScorer {
class GameScene : public bave::Scene {
public:
GameScene(bave::App& app, bave::Services const& services);

Expand All @@ -24,9 +24,6 @@ class GameScene : public bave::Scene, public IScorer {
void tick(bave::Seconds dt) final;
void render(bave::Shader& shader) const final;

[[nodiscard]] auto get_score() const -> std::int64_t final { return m_score; }
void add_score(std::int64_t score) final;

void start_play();

void on_player_death();
Expand All @@ -39,6 +36,8 @@ class GameScene : public bave::Scene, public IScorer {

void debug_controller_type();

SignalHandle m_on_player_scored{};

GameSave m_save;
std::optional<World> m_world{};
std::optional<Player> m_player{};
Expand Down
2 changes: 2 additions & 0 deletions src/spaced/spaced/services/game_signals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

namespace spaced {
struct SigWeaponChanged : Signal<void(Weapon)> {};
struct SigPlayerScored : Signal<void(std::int64_t)> {};

class GameSignals : public bave::IService {
public:
SigWeaponChanged weapon_changed{};
SigPlayerScored player_scored{};
};
} // namespace spaced