diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | GameScreen.cpp | 4 | ||||
-rw-r--r-- | OverScreen.cpp | 36 | ||||
-rw-r--r-- | OverScreen.h | 18 | ||||
-rw-r--r-- | Player-inl.h | 8 | ||||
-rw-r--r-- | Player.cpp | 12 | ||||
-rw-r--r-- | Player.h | 4 |
7 files changed, 79 insertions, 4 deletions
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 <raylib.h> +#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<OverScreen>(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 <memory> + +#include <raylib.h> + +#include "Globals.h" +#include "Stats.h" +#include "TitleScreen.h" + + +static constexpr const char* DIED {"The Bone is Gone"}; + + +OverScreen::OverScreen(std::shared_ptr<Stats> 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<TitleScreen>()); +} + + +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 <memory> + +#include "Screen.h" +#include "Stats.h" + + +class OverScreen : public Screen +{ +public: + explicit OverScreen(std::shared_ptr<Stats> stats); + void update(float dt) override; + void draw() override; +private: + std::shared_ptr<Stats> 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<typename T> bool -Player::collide(const std::vector<T>& bullets) const +Player::collide(const std::vector<T>& 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; } @@ -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<KeyboardController>()} @@ -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; } @@ -13,7 +13,9 @@ struct Player Player(); void update(float dt); void draw(); - template<typename T> bool collide(const std::vector<T>& bullets) const; + void hit(); + template<typename T> bool collide(const std::vector<T>& bullets); + float m_invulnerability; Vector2 m_position; Vector2 m_velocity; std::unique_ptr<Controller> m_controller; |