Skip to content

Add Player death foundation. #31

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 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/worlds/playground.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"background_tint": "mocha",
"player": {
"tint": "black",
"exhaust_emitter": "particles/exhaust.json"
"exhaust_emitter": "particles/exhaust.json",
"death_emitter": "particles/explode.json"
},
"enemy_factories": [
{
Expand Down
2 changes: 2 additions & 0 deletions src/spaced/spaced/game/asset_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ auto AssetList::read_world_spec(std::string_view const uri) -> WorldSpec {
if (auto const& player = json["player"]) {
ret.player.tint = player["tint"].as_string();
ret.player.exhaust_emitter = player["exhaust_emitter"].as_string();
ret.player.death_emitter = player["death_emitter"].as_string();
add_particle_emitter(ret.player.exhaust_emitter);
add_particle_emitter(ret.player.death_emitter);
}

for (auto const& enemy_factory : json["enemy_factories"].array_view()) {
Expand Down
1 change: 1 addition & 0 deletions src/spaced/spaced/game/damageable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class IDamageable : public bave::Polymorphic {
public:
[[nodiscard]] virtual auto get_bounds() const -> bave::Rect<> = 0;
virtual auto take_damage(float damage) -> bool = 0;
virtual void force_death() = 0;
};
} // namespace spaced
6 changes: 4 additions & 2 deletions src/spaced/spaced/game/enemies/creep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace spaced {
using bave::Seconds;

void Creep::tick(Seconds const dt) {
Enemy::tick(dt);
void Creep::tick(Seconds const dt, bool const in_play) {
Enemy::tick(dt, in_play);
if (!in_play) { return; }

shape.transform.position.x -= x_speed * dt.count();
if (shape.transform.position.x < -0.5f * (get_layout().get_world_space().x + shape.get_shape().size.x)) { set_destroyed(); }
}
Expand Down
2 changes: 1 addition & 1 deletion src/spaced/spaced/game/enemies/creep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Creep : public Enemy {
public:
explicit Creep(Services const& services, bave::NotNull<IEnemyDeathListener*> listener) : Enemy(services, listener, "Creep") {}

void tick(bave::Seconds dt) override;
void tick(bave::Seconds dt, bool in_play) override;

float x_speed{100.0f};
};
Expand Down
10 changes: 8 additions & 2 deletions src/spaced/spaced/game/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Enemy::Enemy(Services const& services, bave::NotNull<IEnemyDeathListener*> liste
static constexpr auto init_size_v = glm::vec2{100.0f};
auto const play_area = m_layout->get_play_area();
auto const y_min = play_area.rb.y + 0.5f * init_size_v.y;
auto const y_max = play_area.lt.y - 0.5f * init_size_v.y;
auto const y_max = play_area.lt.y - 0.5f * init_size_v.y - 50.0f;
setup(init_size_v, random_in_range(y_min, y_max));

health_bar.set_style(services.get<Styles>().progress_bars["enemy"]);
Expand All @@ -29,7 +29,13 @@ auto Enemy::take_damage(float const damage) -> bool {
return true;
}

void Enemy::tick(Seconds const dt) {
void Enemy::force_death() {
health = 0.0f;
health_bar.set_progress(0.0f);
m_listener->on_death(EnemyDeath{.position = shape.transform.position});
}

void Enemy::tick(Seconds const dt, bool const /*in_play*/) {
health_bar.position = shape.transform.position;
health_bar.position.y += 0.5f * shape.get_shape().size.y + 20.0f;
health_bar.size = {shape.get_shape().size.x, 10.0f};
Expand Down
3 changes: 2 additions & 1 deletion src/spaced/spaced/game/enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class Enemy : public IDamageable, public bave::IDrawable {

[[nodiscard]] auto get_bounds() const -> bave::Rect<> override { return shape.get_bounds(); }
auto take_damage(float damage) -> bool override;
void force_death() override;

[[nodiscard]] auto is_dead() const -> bool { return health.is_dead(); }
[[nodiscard]] auto is_destroyed() const -> bool { return is_dead() || m_destroyed; }
void set_destroyed() { m_destroyed = true; }

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

void setup(glm::vec2 max_size, float y_position);
Expand Down
4 changes: 2 additions & 2 deletions src/spaced/spaced/game/enemy_spawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ EnemySpawner::EnemySpawner(std::unique_ptr<IEnemyFactory> factory) : m_factory(s
if (!m_factory) { throw std::runtime_error{"Null EnemyFactory passed to EnemySpawner"}; }
}

void EnemySpawner::tick(Seconds const dt) {
void EnemySpawner::tick(Seconds const dt, bool const in_play) {
if (m_factory->tick(dt)) { spawn(); }

for (auto const& enemy : m_enemies) {
enemy->tick(dt);
enemy->tick(dt, in_play);
if (enemy->is_dead()) { explode_at(enemy->get_bounds().centre()); }
}

Expand Down
2 changes: 1 addition & 1 deletion src/spaced/spaced/game/enemy_spawner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EnemySpawner {
public:
explicit EnemySpawner(std::unique_ptr<IEnemyFactory> factory);

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

void spawn() { m_enemies.push_back(m_factory->spawn_enemy()); }
Expand Down
32 changes: 30 additions & 2 deletions src/spaced/spaced/game/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,24 @@ void Player::on_move(PointerMove const& pointer_move) { m_controller->on_move(po
void Player::on_tap(PointerTap const& pointer_tap) { m_controller->on_tap(pointer_tap); }

void Player::tick(State const& state, Seconds const dt) {
if (m_death) {
m_death->tick(dt);
if (m_death->active_particles() == 0) { m_death.reset(); }
}

if (health.is_dead()) { return; }

auto const y_position = m_controller->tick(dt);
set_y(y_position);

for (auto const& target : state.targets) {
if (is_intersecting(target->get_bounds(), ship.get_bounds())) {
on_death();
target->force_death();
return;
}
}

auto const round_state = IWeaponRound::State{.targets = state.targets, .muzzle_position = get_muzzle_position()};
m_arsenal.tick(round_state, m_controller->is_firing(), dt);

Expand All @@ -40,18 +55,24 @@ void Player::tick(State const& state, Seconds const dt) {
}

void Player::draw(Shader& shader) const {
m_exhaust.draw(shader);
ship.draw(shader);
if (!health.is_dead()) {
m_exhaust.draw(shader);
ship.draw(shader);
}
m_arsenal.draw(shader);
if (m_death) { m_death->draw(shader); }
}

void Player::setup(WorldSpec::Player const& spec) {
auto const& rgbas = m_services->get<Styles>().rgbas;
auto const& resources = m_services->get<Resources>();
ship.tint = rgbas[spec.tint];

if (auto const exhaust = resources.get<ParticleEmitter>(spec.exhaust_emitter)) { m_exhaust = *exhaust; }
m_exhaust.set_position(get_exhaust_position());
m_exhaust.pre_warm();

if (auto const death = resources.get<ParticleEmitter>(spec.death_emitter)) { m_death_source = *death; }
}

void Player::set_y(float const y) { ship.transform.position.y = y; }
Expand All @@ -74,6 +95,13 @@ void Player::setup_ship() {
ship.set_shape(rounded_quad);
}

void Player::on_death() {
health = 0.0f;
m_death = m_death_source;
m_death->set_position(ship.transform.position);
m_death->config.respawn = false;
}

void Player::do_inspect() {
if constexpr (bave::imgui_v) {
if (ImGui::TreeNodeEx("Controller", ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down
3 changes: 3 additions & 0 deletions src/spaced/spaced/game/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ class Player : public bave::IDrawable {

private:
void setup_ship();
void on_death();

void do_inspect();

bave::Logger m_log{"Player"};
bave::NotNull<Services const*> m_services;
std::unique_ptr<IController> m_controller;
bave::ParticleEmitter m_exhaust{};
bave::ParticleEmitter m_death_source{};
std::optional<bave::ParticleEmitter> m_death{};

Arsenal m_arsenal{*m_services};
};
Expand Down
4 changes: 3 additions & 1 deletion src/spaced/spaced/game/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ void World::on_death(EnemyDeath const& death) {
}

void World::tick(Seconds const dt) {
bool const in_play = !player.health.is_dead();

m_targets.clear();
for (auto& spawner : m_enemy_spawners) {
spawner.tick(dt);
spawner.tick(dt, in_play);
spawner.append_targets(m_targets);
}

Expand Down
1 change: 1 addition & 0 deletions src/spaced/spaced/game/world_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct WorldSpec {
struct Player {
std::string tint{};
std::string exhaust_emitter{};
std::string death_emitter{};
};

std::string name{};
Expand Down