From 96601c49c7423cb9e4cc5bd849a01e746b0fa39d Mon Sep 17 00:00:00 2001 From: aleokdev Date: Sat, 13 Jul 2024 14:37:23 +0200 Subject: [PATCH 1/3] Add heat/cooldown mechanics --- assets/assets/styles.json | 5 +++-- src/spaced/spaced/game/arsenal.cpp | 10 ++++++--- src/spaced/spaced/game/arsenal.hpp | 5 +++-- src/spaced/spaced/game/player.cpp | 21 ++++++++++++++++++- src/spaced/spaced/game/player.hpp | 8 +++++++ .../spaced/game/weapons/gun_kinetic.cpp | 2 ++ .../spaced/game/weapons/gun_kinetic.hpp | 3 ++- 7 files changed, 45 insertions(+), 9 deletions(-) diff --git a/assets/assets/styles.json b/assets/assets/styles.json index d4951ce..2420b9d 100644 --- a/assets/assets/styles.json +++ b/assets/assets/styles.json @@ -9,7 +9,8 @@ "gun_beam": "#bc96e6ff", "exhaust": "#36bbf5ff", "bg_top": "#10020eff", - "bg_bottom": "#040003ff" + "bg_bottom": "#040003ff", + "ship_heat": "#ac3939ff" }, "buttons": { "default": { @@ -81,4 +82,4 @@ "padding": 20 } } -} +} \ No newline at end of file diff --git a/src/spaced/spaced/game/arsenal.cpp b/src/spaced/spaced/game/arsenal.cpp index e928d0d..2d562be 100644 --- a/src/spaced/spaced/game/arsenal.cpp +++ b/src/spaced/spaced/game/arsenal.cpp @@ -21,11 +21,13 @@ auto Arsenal::get_weapon() -> Weapon& { return const_cast(std::as_const(*this).get_weapon()); // NOLINT(cppcoreguidelines-pro-type-const-cast) } -void Arsenal::tick(IWeaponRound::State const& round_state, bool const fire, Seconds const dt) { +auto Arsenal::tick(IWeaponRound::State const& round_state, bool const fire, Seconds const dt) -> bool { tick_weapons(dt); check_switch_weapon(); - if (round_state.in_play && fire) { fire_weapon(round_state.muzzle_position); } + bool has_fired = false; + if (round_state.in_play && fire) { has_fired = fire_weapon(round_state.muzzle_position); } tick_rounds(round_state, dt); + return has_fired; } void Arsenal::draw(Shader& shader) const { @@ -53,11 +55,13 @@ void Arsenal::check_switch_weapon() { } } -void Arsenal::fire_weapon(glm::vec2 const muzzle_position) { +auto Arsenal::fire_weapon(glm::vec2 const muzzle_position) -> bool { if (auto round = get_weapon().fire(muzzle_position)) { m_rounds.push_back(std::move(round)); ++m_stats->player.shots_fired; + return true; } + return false; } void Arsenal::tick_rounds(IWeaponRound::State const& round_state, Seconds const dt) { diff --git a/src/spaced/spaced/game/arsenal.hpp b/src/spaced/spaced/game/arsenal.hpp index ae96bee..c49d784 100644 --- a/src/spaced/spaced/game/arsenal.hpp +++ b/src/spaced/spaced/game/arsenal.hpp @@ -18,13 +18,14 @@ class Arsenal { void set_special(std::unique_ptr weapon) { m_next = std::move(weapon); } - void tick(IWeaponRound::State const& round_state, bool fire, bave::Seconds dt); + // returns whether a round was fired this frame + auto tick(IWeaponRound::State const& round_state, bool fire, bave::Seconds dt) -> bool; void draw(bave::Shader& shader) const; private: void tick_weapons(bave::Seconds dt); void check_switch_weapon(); - void fire_weapon(glm::vec2 muzzle_position); + auto fire_weapon(glm::vec2 muzzle_position) -> bool; void tick_rounds(IWeaponRound::State const& round_state, bave::Seconds dt); bave::NotNull m_stats; diff --git a/src/spaced/spaced/game/player.cpp b/src/spaced/spaced/game/player.cpp index 54c189b..a0ffc91 100644 --- a/src/spaced/spaced/game/player.cpp +++ b/src/spaced/spaced/game/player.cpp @@ -46,6 +46,8 @@ Player::Player(Services const& services, std::unique_ptr controller if (auto const death = resources.get("assets/particles/explode.json")) { m_death_source = *death; } m_death_source.config.respawn = false; + + m_heat_color = rgbas["ship_heat"]; } void Player::on_focus(bave::FocusChange const& /*focus_changed*/) { m_controller->untap(); } @@ -65,7 +67,20 @@ void Player::tick(State const& state, Seconds const dt) { .muzzle_position = get_muzzle_position(), .in_play = !m_health.is_dead(), }; - m_arsenal.tick(round_state, m_controller->is_firing(), dt); + + auto const has_fired = m_arsenal.tick(round_state, m_controller->is_firing() && !m_is_cooling_down, dt); + + if (has_fired) { + m_heat += m_heat_increment; + } else { + m_heat -= (m_is_cooling_down ? m_heat_dissipated * 0.5f : m_heat_dissipated) * dt.count(); + } + if (m_heat >= 1.0f) { m_is_cooling_down = true; } + if (m_heat <= m_cooldown_threshold) { m_is_cooling_down = false; } + m_heat = std::clamp(m_heat, 0.f, 1.0f); + + m_heat_being_rendered = glm::mix(m_heat_being_rendered, m_heat, 0.5f); + ship.tint = bave::Rgba::from(glm::mix(bave::white_v.to_vec4(), m_heat_color.to_vec4(), m_heat_being_rendered)); m_shield.set_position(ship.transform.position); m_shield.tick(dt); @@ -188,6 +203,10 @@ void Player::do_inspect() { } if (ImGui::Button("1up")) { one_up(); } + + ImGui::DragFloat("heat dissipated per sec", &m_heat_dissipated, 0.05f, 0.f, 1.f); + ImGui::DragFloat("heat per round fired", &m_heat_increment, 0.1f, 0.f, 1.f); + ImGui::DragFloat("cooldown threshold", &m_cooldown_threshold, 0.1f, 0.f, 0.9f); } } } // namespace spaced diff --git a/src/spaced/spaced/game/player.hpp b/src/spaced/spaced/game/player.hpp index 7a5888e..105bfff 100644 --- a/src/spaced/spaced/game/player.hpp +++ b/src/spaced/spaced/game/player.hpp @@ -74,6 +74,14 @@ class Player : public IDamageable, public bave::IDrawable { bave::ParticleEmitter m_exhaust{}; Shield m_shield; + float m_heat_being_rendered{}; + float m_heat{}; + float m_heat_increment = 0.1f; + float m_heat_dissipated = 0.4f; // per second + bave::Rgba m_heat_color; + bool m_is_cooling_down{}; + float m_cooldown_threshold = 0.5f; + bave::ParticleEmitter m_death_source{}; std::optional m_death{}; diff --git a/src/spaced/spaced/game/weapons/gun_kinetic.cpp b/src/spaced/spaced/game/weapons/gun_kinetic.cpp index 15ff49d..2495909 100644 --- a/src/spaced/spaced/game/weapons/gun_kinetic.cpp +++ b/src/spaced/spaced/game/weapons/gun_kinetic.cpp @@ -38,6 +38,8 @@ void GunKinetic::do_inspect() { auto tint = projectile_config.tint.to_vec4(); if (ImGui::ColorEdit4("projectile tint", &tint.x)) { projectile_config.tint = Rgba::from(tint); } ImGui::DragFloat("damage", &projectile_config.damage, 0.25f, 0.25f, 10.0f); + auto reload_delay_s = reload_delay.count(); + if (ImGui::DragFloat("reload delay", &reload_delay_s, 0.05f, 0.1f, 1.f)) { reload_delay = Seconds{reload_delay_s}; } } } } // namespace spaced diff --git a/src/spaced/spaced/game/weapons/gun_kinetic.hpp b/src/spaced/spaced/game/weapons/gun_kinetic.hpp index de6b633..60ffbee 100644 --- a/src/spaced/spaced/game/weapons/gun_kinetic.hpp +++ b/src/spaced/spaced/game/weapons/gun_kinetic.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -15,7 +16,7 @@ class GunKinetic final : public Weapon { Projectile::Config projectile_config{}; std::string_view fire_sfx{"assets/sfx/kinetic_fire.wav"}; - bave::Seconds reload_delay{0.25s}; + bave::Seconds reload_delay{0.15s}; private: auto do_fire(glm::vec2 muzzle_position) -> std::unique_ptr final; From 00097bc316f5444bb97508a0031cd726ae7cc246 Mon Sep 17 00:00:00 2001 From: aleokdev Date: Sat, 13 Jul 2024 14:37:30 +0200 Subject: [PATCH 2/3] Add more health to enemies --- src/spaced/spaced/game/enemies/creep.cpp | 2 +- src/spaced/spaced/game/enemies/gunner.cpp | 2 +- src/spaced/spaced/game/enemies/trooper.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/spaced/spaced/game/enemies/creep.cpp b/src/spaced/spaced/game/enemies/creep.cpp index 903a896..a98b71e 100644 --- a/src/spaced/spaced/game/enemies/creep.cpp +++ b/src/spaced/spaced/game/enemies/creep.cpp @@ -11,7 +11,7 @@ Creep::Creep(bave::Services const& services) : Enemy(services, "Creep") { m_sprite.set_texture(std::move(texture)); } - health = 1.0f; + health = 2.0f; speed = 100.0f; points = 10; } diff --git a/src/spaced/spaced/game/enemies/gunner.cpp b/src/spaced/spaced/game/enemies/gunner.cpp index e096d73..8a323b2 100644 --- a/src/spaced/spaced/game/enemies/gunner.cpp +++ b/src/spaced/spaced/game/enemies/gunner.cpp @@ -11,7 +11,7 @@ Gunner::Gunner(bave::Services const& services, bave::NotNull gun) : m_sprite.set_texture(std::move(texture)); } - health = 2.0f; + health = 3.0f; speed = 120.0f; points = 20; } diff --git a/src/spaced/spaced/game/enemies/trooper.cpp b/src/spaced/spaced/game/enemies/trooper.cpp index 27c6e48..8eea120 100644 --- a/src/spaced/spaced/game/enemies/trooper.cpp +++ b/src/spaced/spaced/game/enemies/trooper.cpp @@ -18,7 +18,7 @@ Trooper::Trooper(Services const& services, NotNull gun) : GunnerBas m_direction = random_in_range(0, 1) == 0 ? 1.0f : -1.0f; - health = 3.0f; + health = 4.0f; speed = 150.0f; points = 30; } From b65f1fb7a1adbb57a54900306036082393025a9a Mon Sep 17 00:00:00 2001 From: Karn Kaul Date: Sun, 14 Jul 2024 16:15:09 +0530 Subject: [PATCH 3/3] Cleanup. --- src/spaced/spaced/game/player.cpp | 2 +- src/spaced/spaced/game/player.hpp | 8 ++++---- src/spaced/spaced/game/weapons/gun_kinetic.hpp | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/spaced/spaced/game/player.cpp b/src/spaced/spaced/game/player.cpp index a0ffc91..64f3cab 100644 --- a/src/spaced/spaced/game/player.cpp +++ b/src/spaced/spaced/game/player.cpp @@ -47,7 +47,7 @@ Player::Player(Services const& services, std::unique_ptr controller if (auto const death = resources.get("assets/particles/explode.json")) { m_death_source = *death; } m_death_source.config.respawn = false; - m_heat_color = rgbas["ship_heat"]; + m_heat_color = rgbas.get_or("ship_heat", bave::red_v); } void Player::on_focus(bave::FocusChange const& /*focus_changed*/) { m_controller->untap(); } diff --git a/src/spaced/spaced/game/player.hpp b/src/spaced/spaced/game/player.hpp index 105bfff..787fc9d 100644 --- a/src/spaced/spaced/game/player.hpp +++ b/src/spaced/spaced/game/player.hpp @@ -76,11 +76,11 @@ class Player : public IDamageable, public bave::IDrawable { float m_heat_being_rendered{}; float m_heat{}; - float m_heat_increment = 0.1f; - float m_heat_dissipated = 0.4f; // per second - bave::Rgba m_heat_color; + float m_heat_increment{0.1f}; + float m_heat_dissipated{0.4f}; // per second + bave::Rgba m_heat_color{}; bool m_is_cooling_down{}; - float m_cooldown_threshold = 0.5f; + float m_cooldown_threshold{0.5f}; bave::ParticleEmitter m_death_source{}; std::optional m_death{}; diff --git a/src/spaced/spaced/game/weapons/gun_kinetic.hpp b/src/spaced/spaced/game/weapons/gun_kinetic.hpp index 60ffbee..46866c8 100644 --- a/src/spaced/spaced/game/weapons/gun_kinetic.hpp +++ b/src/spaced/spaced/game/weapons/gun_kinetic.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include