Skip to content

Commit

Permalink
Refactor powerup hierarchy.
Browse files Browse the repository at this point in the history
  • Loading branch information
karnkaul committed Jun 25, 2024
1 parent 3b1d829 commit 1afa59f
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/spaced/spaced/game/enemies/creep.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bave/services/resources.hpp>
#include <spaced/game/enemies/creep.hpp>

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

update_health_bar();
}
} // namespace spaced
} // namespace spaced::enemy
4 changes: 2 additions & 2 deletions src/spaced/spaced/game/enemies/creep.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <spaced/game/enemy.hpp>

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

float x_speed{100.0f};
};
} // namespace spaced
} // namespace spaced::enemy
2 changes: 1 addition & 1 deletion src/spaced/spaced/game/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Player : public bave::IDrawable {
public:
struct State {
std::span<bave::NotNull<IDamageable*> const> targets{};
std::span<bave::NotNull<IPowerup*> const> powerups{};
std::span<bave::NotNull<Powerup*> const> powerups{};
};

explicit Player(bave::Services const& services, std::unique_ptr<IController> controller);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bave/services/resources.hpp>
#include <spaced/game/powerups/pu_base.hpp>
#include <spaced/game/powerup.hpp>

namespace spaced {
using bave::Circle;
Expand All @@ -9,7 +9,7 @@ using bave::Seconds;
using bave::Services;
using bave::Shader;

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

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

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

void PUBase::draw(Shader& shader) const {
void Powerup::draw(Shader& shader) const {
shape.draw(shader);
emitter.draw(shader);
}

void PUBase::activate(Player& player) {
void Powerup::activate(Player& player) {
do_activate(player);
m_destroyed = true;
}
Expand Down
33 changes: 25 additions & 8 deletions src/spaced/spaced/game/powerup.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
#pragma once
#include <bave/core/polymorphic.hpp>
#include <bave/core/time.hpp>
#include <bave/graphics/drawable.hpp>
#include <bave/graphics/rect.hpp>
#include <bave/graphics/particle_system.hpp>
#include <bave/graphics/shape.hpp>
#include <bave/services/services.hpp>
#include <spaced/services/layout.hpp>

namespace spaced {
class Player;

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

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

virtual void tick(bave::Seconds dt) = 0;
[[nodiscard]] auto get_bounds() const -> bave::Rect<> { return shape.get_bounds(); }
void activate(Player& player);

[[nodiscard]] auto is_destroyed() const -> bool { return m_destroyed; }

float speed{300.0f};
bave::CircleShape shape{};
bave::ParticleEmitter emitter{};

protected:
virtual void do_activate(Player& player) = 0;

bave::NotNull<bave::Services const*> m_services;
bave::NotNull<Layout const*> m_layout;
std::string_view m_name{};
bool m_emitter_ticked{};
bool m_destroyed{};
};
} // namespace spaced
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <bave/services/styles.hpp>
#include <spaced/game/player.hpp>
#include <spaced/game/powerups/pu_beam.hpp>
#include <spaced/game/powerups/beam.hpp>
#include <spaced/game/weapons/gun_beam.hpp>

namespace spaced {
namespace spaced::powerup {
using bave::Services;
using bave::Styles;

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

void PUBeam::do_activate(Player& player) {
void Beam::do_activate(Player& player) {
auto beam = std::make_unique<GunBeam>(*m_services);
beam->rounds = m_rounds;
player.set_special_weapon(std::move(beam));
}
} // namespace spaced
} // namespace spaced::powerup
14 changes: 14 additions & 0 deletions src/spaced/spaced/game/powerups/beam.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <spaced/game/powerup.hpp>

namespace spaced::powerup {
class Beam : public Powerup {
public:
explicit Beam(bave::Services const& services, int rounds = 2);

private:
void do_activate(Player& player) final;

int m_rounds{};
};
} // namespace spaced::powerup
35 changes: 0 additions & 35 deletions src/spaced/spaced/game/powerups/pu_base.hpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/spaced/spaced/game/powerups/pu_beam.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/spaced/spaced/game/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void World::push(std::unique_ptr<Enemy> enemy) {
m_active_enemies.push_back(std::move(enemy));
}

void World::push(std::unique_ptr<IPowerup> powerup) {
void World::push(std::unique_ptr<Powerup> powerup) {
if (!powerup) { return; }
m_active_powerups.push_back(std::move(powerup));
}
Expand Down
8 changes: 4 additions & 4 deletions src/spaced/spaced/game/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class World : public ITargetProvider {

[[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; }
[[nodiscard]] auto get_powerups() const -> std::span<bave::NotNull<Powerup*> const> { return m_powerups; }

void tick(bave::Seconds dt, bool in_play);
void draw(bave::Shader& shader) const;

void push(std::unique_ptr<Enemy> enemy);
void push(std::unique_ptr<IPowerup> powerup);
void push(std::unique_ptr<Powerup> powerup);

void inspect() {
if constexpr (bave::debug_v) { do_inspect(); }
Expand All @@ -54,9 +54,9 @@ class World : public ITargetProvider {

std::vector<std::unique_ptr<Enemy>> m_active_enemies{};
std::vector<bave::ParticleEmitter> m_enemy_death_emitters{};
std::vector<std::unique_ptr<IPowerup>> m_active_powerups{};
std::vector<std::unique_ptr<Powerup>> m_active_powerups{};

std::vector<bave::NotNull<IDamageable*>> m_targets{};
std::vector<bave::NotNull<IPowerup*>> m_powerups{};
std::vector<bave::NotNull<Powerup*>> m_powerups{};
};
} // namespace spaced
4 changes: 2 additions & 2 deletions src/spaced/spaced/scenes/endless.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bave/core/random.hpp>
#include <spaced/game/powerups/pu_beam.hpp>
#include <spaced/game/powerups/beam.hpp>
#include <spaced/scenes/endless.hpp>
#include <spaced/services/game_signals.hpp>

Expand All @@ -24,7 +24,7 @@ void EndlessScene::tick(Seconds const dt) {
}

void EndlessScene::debug_spawn_powerup(glm::vec2 const position) {
auto powerup = std::make_unique<PUBeam>(get_services());
auto powerup = std::make_unique<powerup::Beam>(get_services());
powerup->shape.transform.position = position;
push_powerup(std::move(powerup));
}
Expand Down
2 changes: 1 addition & 1 deletion src/spaced/spaced/scenes/endless.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class EndlessScene : public GameScene {

SignalHandle m_on_player_scored{};

SpawnTimer<Creep> m_creeps;
SpawnTimer<enemy::Creep> m_creeps;
};
} // namespace spaced
2 changes: 1 addition & 1 deletion src/spaced/spaced/scenes/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GameScene : public bave::Scene {
void tick(bave::Seconds dt) override;

void push_enemy(std::unique_ptr<Enemy> enemy) { m_world.value().push(std::move(enemy)); }
void push_powerup(std::unique_ptr<IPowerup> powerup) { m_world.value().push(std::move(powerup)); }
void push_powerup(std::unique_ptr<Powerup> powerup) { m_world.value().push(std::move(powerup)); }

private:
void on_focus(bave::FocusChange const& focus_change) final;
Expand Down

0 comments on commit 1afa59f

Please sign in to comment.