Skip to content

Commit 1afa59f

Browse files
committed
Refactor powerup hierarchy.
1 parent 3b1d829 commit 1afa59f

File tree

14 files changed

+63
-81
lines changed

14 files changed

+63
-81
lines changed

src/spaced/spaced/game/enemies/creep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <bave/services/resources.hpp>
22
#include <spaced/game/enemies/creep.hpp>
33

4-
namespace spaced {
4+
namespace spaced::enemy {
55
using bave::Resources;
66
using bave::Seconds;
77
using bave::Texture;
@@ -20,4 +20,4 @@ void Creep::tick(Seconds const dt, bool const in_play) {
2020

2121
update_health_bar();
2222
}
23-
} // namespace spaced
23+
} // namespace spaced::enemy
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include <spaced/game/enemy.hpp>
33

4-
namespace spaced {
4+
namespace spaced::enemy {
55
class Creep : public Enemy {
66
public:
77
explicit Creep(bave::Services const& services);
@@ -10,4 +10,4 @@ class Creep : public Enemy {
1010

1111
float x_speed{100.0f};
1212
};
13-
} // namespace spaced
13+
} // namespace spaced::enemy

src/spaced/spaced/game/player.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Player : public bave::IDrawable {
1515
public:
1616
struct State {
1717
std::span<bave::NotNull<IDamageable*> const> targets{};
18-
std::span<bave::NotNull<IPowerup*> const> powerups{};
18+
std::span<bave::NotNull<Powerup*> const> powerups{};
1919
};
2020

2121
explicit Player(bave::Services const& services, std::unique_ptr<IController> controller);

src/spaced/spaced/game/powerups/pu_base.cpp renamed to src/spaced/spaced/game/powerup.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <bave/services/resources.hpp>
2-
#include <spaced/game/powerups/pu_base.hpp>
2+
#include <spaced/game/powerup.hpp>
33

44
namespace spaced {
55
using bave::Circle;
@@ -9,7 +9,7 @@ using bave::Seconds;
99
using bave::Services;
1010
using bave::Shader;
1111

12-
PUBase::PUBase(Services const& services, std::string_view const name) : m_services(&services), m_layout(&services.get<Layout>()), m_name(name) {
12+
Powerup::Powerup(Services const& services, std::string_view const name) : m_services(&services), m_layout(&services.get<Layout>()), m_name(name) {
1313
auto circle = Circle{};
1414
circle.diameter = 40.0f;
1515
shape.set_shape(circle);
@@ -20,7 +20,7 @@ PUBase::PUBase(Services const& services, std::string_view const name) : m_servic
2020
emitter.config.respawn = true;
2121
}
2222

23-
void PUBase::tick(Seconds const dt) {
23+
void Powerup::tick(Seconds const dt) {
2424
shape.transform.position.x -= speed * dt.count();
2525
if (shape.transform.position.x < m_layout->play_area.lt.x - 0.5f * shape.get_shape().diameter) { m_destroyed = true; }
2626

@@ -32,12 +32,12 @@ void PUBase::tick(Seconds const dt) {
3232
emitter.tick(dt);
3333
}
3434

35-
void PUBase::draw(Shader& shader) const {
35+
void Powerup::draw(Shader& shader) const {
3636
shape.draw(shader);
3737
emitter.draw(shader);
3838
}
3939

40-
void PUBase::activate(Player& player) {
40+
void Powerup::activate(Player& player) {
4141
do_activate(player);
4242
m_destroyed = true;
4343
}

src/spaced/spaced/game/powerup.hpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
#pragma once
2-
#include <bave/core/polymorphic.hpp>
32
#include <bave/core/time.hpp>
4-
#include <bave/graphics/drawable.hpp>
5-
#include <bave/graphics/rect.hpp>
3+
#include <bave/graphics/particle_system.hpp>
4+
#include <bave/graphics/shape.hpp>
5+
#include <bave/services/services.hpp>
6+
#include <spaced/services/layout.hpp>
67

78
namespace spaced {
89
class Player;
910

10-
class IPowerup : public bave::IDrawable {
11+
class Powerup : public bave::IDrawable {
1112
public:
12-
[[nodiscard]] virtual auto get_bounds() const -> bave::Rect<> = 0;
13-
virtual void activate(Player& player) = 0;
13+
explicit Powerup(bave::Services const& services, std::string_view name);
1414

15-
[[nodiscard]] virtual auto is_destroyed() const -> bool = 0;
15+
void tick(bave::Seconds dt);
16+
void draw(bave::Shader& shader) const final;
1617

17-
virtual void tick(bave::Seconds dt) = 0;
18+
[[nodiscard]] auto get_bounds() const -> bave::Rect<> { return shape.get_bounds(); }
19+
void activate(Player& player);
20+
21+
[[nodiscard]] auto is_destroyed() const -> bool { return m_destroyed; }
22+
23+
float speed{300.0f};
24+
bave::CircleShape shape{};
25+
bave::ParticleEmitter emitter{};
26+
27+
protected:
28+
virtual void do_activate(Player& player) = 0;
29+
30+
bave::NotNull<bave::Services const*> m_services;
31+
bave::NotNull<Layout const*> m_layout;
32+
std::string_view m_name{};
33+
bool m_emitter_ticked{};
34+
bool m_destroyed{};
1835
};
1936
} // namespace spaced
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#include <bave/services/styles.hpp>
22
#include <spaced/game/player.hpp>
3-
#include <spaced/game/powerups/pu_beam.hpp>
3+
#include <spaced/game/powerups/beam.hpp>
44
#include <spaced/game/weapons/gun_beam.hpp>
55

6-
namespace spaced {
6+
namespace spaced::powerup {
77
using bave::Services;
88
using bave::Styles;
99

10-
PUBeam::PUBeam(Services const& services, int rounds) : PUBase(services, "Beam"), m_rounds(rounds) {
10+
Beam::Beam(Services const& services, int rounds) : Powerup(services, "Beam"), m_rounds(rounds) {
1111
emitter.config.lerp.tint.lo = emitter.config.lerp.tint.hi = shape.tint = services.get<Styles>().rgbas["gun_beam"];
1212
emitter.config.lerp.tint.hi.channels.w = 0;
1313
}
1414

15-
void PUBeam::do_activate(Player& player) {
15+
void Beam::do_activate(Player& player) {
1616
auto beam = std::make_unique<GunBeam>(*m_services);
1717
beam->rounds = m_rounds;
1818
player.set_special_weapon(std::move(beam));
1919
}
20-
} // namespace spaced
20+
} // namespace spaced::powerup
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <spaced/game/powerup.hpp>
3+
4+
namespace spaced::powerup {
5+
class Beam : public Powerup {
6+
public:
7+
explicit Beam(bave::Services const& services, int rounds = 2);
8+
9+
private:
10+
void do_activate(Player& player) final;
11+
12+
int m_rounds{};
13+
};
14+
} // namespace spaced::powerup

src/spaced/spaced/game/powerups/pu_base.hpp

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

src/spaced/spaced/game/powerups/pu_beam.hpp

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

src/spaced/spaced/game/world.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void World::push(std::unique_ptr<Enemy> enemy) {
7777
m_active_enemies.push_back(std::move(enemy));
7878
}
7979

80-
void World::push(std::unique_ptr<IPowerup> powerup) {
80+
void World::push(std::unique_ptr<Powerup> powerup) {
8181
if (!powerup) { return; }
8282
m_active_powerups.push_back(std::move(powerup));
8383
}

0 commit comments

Comments
 (0)