From 38df088b80cb6c159eb9941cf6d3c0a8492e65ee Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 22 Apr 2022 19:53:36 +0200 Subject: Added invulnerability timer and death screen --- CMakeLists.txt | 1 + GameScreen.cpp | 4 ++++ OverScreen.cpp | 36 ++++++++++++++++++++++++++++++++++++ OverScreen.h | 18 ++++++++++++++++++ Player-inl.h | 8 ++++++-- Player.cpp | 12 +++++++++++- Player.h | 4 +++- 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 OverScreen.cpp create mode 100644 OverScreen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 176b3f8..e3e9cea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable( KeyboardController.cpp main.cpp Oscillating.cpp + OverScreen.cpp Player.cpp Stats.cpp TestStage.cpp diff --git a/GameScreen.cpp b/GameScreen.cpp index f813a60..1a8795f 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -5,6 +5,8 @@ #include +#include "Globals.h" +#include "OverScreen.h" #include "Stage.h" @@ -20,6 +22,8 @@ void GameScreen::update(const float dt) { m_stage->update(dt); + if (m_stats->lifes < 0) + g_game.set(std::make_unique(m_stats)); } diff --git a/OverScreen.cpp b/OverScreen.cpp new file mode 100644 index 0000000..348e5a9 --- /dev/null +++ b/OverScreen.cpp @@ -0,0 +1,36 @@ +#include "OverScreen.h" + +#include + +#include + +#include "Globals.h" +#include "Stats.h" +#include "TitleScreen.h" + + +static constexpr const char* DIED {"The Bone is Gone"}; + + +OverScreen::OverScreen(std::shared_ptr stats) : + m_stats {stats} +{ + const int screen_width = GetScreenWidth(); + const int died_width = MeasureText(DIED, 20); + m_died_x = (screen_width - died_width) / 2; +} + + +void +OverScreen::update(const float) +{ + if (IsKeyPressed(KEY_SPACE)) + g_game.set(std::make_unique()); +} + + +void +OverScreen::draw() +{ + DrawText(DIED, m_died_x, 160, 20, RED); +} diff --git a/OverScreen.h b/OverScreen.h new file mode 100644 index 0000000..3dd7fa2 --- /dev/null +++ b/OverScreen.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "Screen.h" +#include "Stats.h" + + +class OverScreen : public Screen +{ +public: + explicit OverScreen(std::shared_ptr stats); + void update(float dt) override; + void draw() override; +private: + std::shared_ptr m_stats; + int m_died_x; +}; diff --git a/Player-inl.h b/Player-inl.h index 140bfad..32f6117 100644 --- a/Player-inl.h +++ b/Player-inl.h @@ -7,11 +7,15 @@ template bool -Player::collide(const std::vector& bullets) const +Player::collide(const std::vector& bullets) { + if (m_invulnerability > 0) + return false; for (const auto& bullet : bullets) { - if (CheckCollisionCircles(m_position, 4, bullet.position, bullet.radius)) + if (CheckCollisionCircles(m_position, 4, bullet.position, bullet.radius)) { + hit(); return true; + } } return false; } diff --git a/Player.cpp b/Player.cpp index 490b810..4e13ad0 100644 --- a/Player.cpp +++ b/Player.cpp @@ -12,12 +12,14 @@ static constexpr float ACCELERATION {1800}; static constexpr float MAX_SPEED {180}; static constexpr float DAMP {2200}; static constexpr float MARGIN {0.3f}; +static constexpr float HIT_INVUL {1.2f}; static float damped_velocity(float dt, float direction, float velocity); Player::Player() : + m_invulnerability {HIT_INVUL / 2.f}, m_position {400, 450}, m_velocity {0, 0}, m_controller {std::make_unique()} @@ -33,13 +35,21 @@ Player::update(const float dt) m_velocity.y = std::clamp(damped_velocity(dt, direction.y, m_velocity.y), -MAX_SPEED, MAX_SPEED); m_position.x += m_velocity.x * dt; m_position.y += m_velocity.y * dt; + m_invulnerability -= dt; } void Player::draw() { - DrawCircle(m_position.x, m_position.y, 4, LIGHTGRAY); + DrawCircle(m_position.x, m_position.y, 4, m_invulnerability > 0 ? RED : LIGHTGRAY); +} + + +void +Player::hit() +{ + m_invulnerability = HIT_INVUL; } diff --git a/Player.h b/Player.h index 59e8282..b1cb629 100644 --- a/Player.h +++ b/Player.h @@ -13,7 +13,9 @@ struct Player Player(); void update(float dt); void draw(); - template bool collide(const std::vector& bullets) const; + void hit(); + template bool collide(const std::vector& bullets); + float m_invulnerability; Vector2 m_position; Vector2 m_velocity; std::unique_ptr m_controller; -- cgit v1.1