Skip to content

Commit e2f8988

Browse files
authored
Track and persist various game stats. (#43)
* Track and persist various game stats. * Cleanup.
1 parent e37ebf2 commit e2f8988

File tree

15 files changed

+131
-6
lines changed

15 files changed

+131
-6
lines changed

src/spaced/spaced/game/arsenal.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include <spaced/game/arsenal.hpp>
2+
#include <spaced/services/stats.hpp>
23

34
namespace spaced {
45
using bave::Seconds;
56
using bave::Shader;
67

8+
Arsenal::Arsenal(Services const& services) : m_primary(services), m_stats(&services.get<Stats>()) {}
9+
710
auto Arsenal::get_weapon() const -> Weapon const& {
811
if (m_special) { return *m_special; }
912
return m_primary;
@@ -40,7 +43,10 @@ void Arsenal::check_switch_weapon() {
4043
}
4144

4245
void Arsenal::fire_weapon(glm::vec2 const muzzle_position) {
43-
if (auto round = get_weapon().fire(muzzle_position)) { m_rounds.push_back(std::move(round)); }
46+
if (auto round = get_weapon().fire(muzzle_position)) {
47+
m_rounds.push_back(std::move(round));
48+
++m_stats->player.shots_fired;
49+
}
4450
}
4551

4652
void Arsenal::tick_rounds(IWeaponRound::State const& round_state, Seconds const dt) {

src/spaced/spaced/game/arsenal.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
#include <spaced/game/weapons/gun_kinetic.hpp>
33

44
namespace spaced {
5+
struct Stats;
6+
57
// Arsenal models a main/primary weapon, and an possible special weapon.
68
// Weapons only switch when they are idle.
79
class Arsenal {
810
public:
9-
explicit Arsenal(Services const& services) : m_primary(services) {}
11+
explicit Arsenal(Services const& services);
1012

1113
[[nodiscard]] auto get_weapon() const -> Weapon const&;
1214
[[nodiscard]] auto get_weapon() -> Weapon&;
@@ -24,7 +26,8 @@ class Arsenal {
2426
void fire_weapon(glm::vec2 muzzle_position);
2527
void tick_rounds(IWeaponRound::State const& round_state, bave::Seconds dt);
2628

27-
GunKinetic m_primary; // main weapon
29+
GunKinetic m_primary; // main weapon
30+
bave::NotNull<Stats*> m_stats;
2831
std::unique_ptr<Weapon> m_special{}; // special weapon
2932
std::unique_ptr<Weapon> m_next{}; // next special weapon (on standby until current weapon is idle)
3033
std::vector<std::unique_ptr<Weapon::Round>> m_rounds{};

src/spaced/spaced/game/damageable.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22
#include <bave/core/polymorphic.hpp>
33
#include <bave/graphics/rect.hpp>
4+
#include <spaced/game/instigator.hpp>
45

56
namespace spaced {
67
class IDamageable : public bave::Polymorphic {
78
public:
9+
[[nodiscard]] virtual auto get_instigator() const -> Instigator = 0;
810
[[nodiscard]] virtual auto get_bounds() const -> bave::Rect<> = 0;
911
virtual auto take_damage(float damage) -> bool = 0;
1012
virtual void force_death() = 0;

src/spaced/spaced/game/enemy.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Enemy : public IDamageable, public bave::IDrawable {
1313
public:
1414
explicit Enemy(Services const& services, std::string_view type);
1515

16+
[[nodiscard]] auto get_instigator() const -> Instigator final { return Instigator::eEnemy; }
1617
[[nodiscard]] auto get_bounds() const -> bave::Rect<> override { return shape.get_bounds(); }
1718
auto take_damage(float damage) -> bool override;
1819
void force_death() override;

src/spaced/spaced/game/instigator.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
namespace spaced {
4+
enum class Instigator { ePlayer, eEnemy, eOther };
5+
}

src/spaced/spaced/game/player.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <bave/platform.hpp>
44
#include <spaced/game/player.hpp>
55
#include <spaced/services/resources.hpp>
6+
#include <spaced/services/stats.hpp>
67
#include <spaced/services/styles.hpp>
78

89
// temp for testing
@@ -16,7 +17,8 @@ using bave::RoundedQuad;
1617
using bave::Seconds;
1718
using bave::Shader;
1819

19-
Player::Player(Services const& services, std::unique_ptr<IController> controller) : m_services(&services), m_controller(std::move(controller)) {
20+
Player::Player(Services const& services, std::unique_ptr<IController> controller)
21+
: m_services(&services), m_stats(&services.get<Stats>()), m_controller(std::move(controller)) {
2022
auto const& layout = services.get<ILayout>();
2123
ship.transform.position.x = layout.get_player_x();
2224
auto rounded_quad = RoundedQuad{};
@@ -68,7 +70,10 @@ void Player::tick(State const& state, Seconds const dt) {
6870
m_exhaust.tick(dt);
6971

7072
for (auto const& powerup : state.powerups) {
71-
if (is_intersecting(powerup->get_bounds(), ship.get_bounds())) { powerup->activate(*this); }
73+
if (is_intersecting(powerup->get_bounds(), ship.get_bounds())) {
74+
powerup->activate(*this);
75+
++m_stats->player.powerups_collected;
76+
}
7277
}
7378
}
7479

@@ -97,6 +102,7 @@ void Player::on_death(Seconds const dt) {
97102
m_death = m_death_source;
98103
m_death->set_position(ship.transform.position);
99104
m_death->tick(dt);
105+
++m_stats->player.death_count;
100106
}
101107

102108
void Player::do_inspect() {

src/spaced/spaced/game/player.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <spaced/game/powerup.hpp>
99

1010
namespace spaced {
11+
struct Stats;
12+
1113
class Player : public bave::IDrawable {
1214
public:
1315
struct State {
@@ -49,6 +51,7 @@ class Player : public bave::IDrawable {
4951

5052
bave::Logger m_log{"Player"};
5153
bave::NotNull<Services const*> m_services;
54+
bave::NotNull<Stats*> m_stats;
5255
std::unique_ptr<IController> m_controller;
5356
bave::ParticleEmitter m_exhaust{};
5457
bave::ParticleEmitter m_death_source{};

src/spaced/spaced/game/weapons/projectile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void Projectile::tick(State const& state, Seconds const dt) {
3333
m_shape.transform.position.x += dx;
3434

3535
for (auto target : state.targets) {
36+
if (target->get_instigator() == m_config.instigator) { continue; }
3637
if (check_hit(m_shape.transform.position, m_config.size, dx, target->get_bounds())) {
3738
if (target->take_damage(m_config.damage)) {
3839
m_destroyed = true;

src/spaced/spaced/game/weapons/projectile.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <bave/graphics/shape.hpp>
3+
#include <spaced/game/instigator.hpp>
34
#include <spaced/game/weapon_round.hpp>
45
#include <spaced/services/layout.hpp>
56

@@ -13,6 +14,7 @@ class Projectile : public IWeaponRound {
1314
float x_speed{1500.0f};
1415
bave::Rgba tint{bave::black_v};
1516
float damage{1.0f};
17+
Instigator instigator{Instigator::ePlayer};
1618
};
1719

1820
explicit Projectile(bave::NotNull<ILayout const*> layout, Config config, glm::vec2 muzzle_position);

src/spaced/spaced/game/world.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <spaced/game/enemies/creep_factory.hpp>
33
#include <spaced/game/world.hpp>
44
#include <spaced/services/resources.hpp>
5+
#include <spaced/services/stats.hpp>
56

67
#include <bave/core/random.hpp>
78
#include <spaced/game/controllers/auto_controller.hpp>
@@ -35,7 +36,7 @@ namespace {
3536

3637
World::World(bave::NotNull<Services const*> services, bave::NotNull<IScorer*> scorer)
3738
: player(*services, make_player_controller(*services)), m_services(services), m_resources(&services->get<Resources>()), m_audio(&services->get<IAudio>()),
38-
m_scorer(scorer) {
39+
m_stats(&services->get<Stats>()), m_scorer(scorer) {
3940
m_enemy_factories["CreepFactory"] = std::make_unique<CreepFactory>(services);
4041
}
4142

@@ -87,6 +88,7 @@ void World::on_death(Enemy const& enemy, bool const add_score) {
8788

8889
if (add_score) {
8990
m_scorer->add_score(enemy.points);
91+
++m_stats->player.enemies_poofed;
9092

9193
// temp
9294
if (random_in_range(0, 10) < 3) { debug_spawn_powerup(enemy.shape.transform.position); }

0 commit comments

Comments
 (0)