From 63c64254a80564e11987ba4af289cf8d47774668 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 25 Apr 2022 00:18:36 +0200 Subject: Added move constraints --- GameScreen.cpp | 7 +++++-- Player.cpp | 18 +++++++++++++++++- Player.h | 1 + Stage.h | 2 ++ TestStage.cpp | 1 - TestStage.h | 1 - 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/GameScreen.cpp b/GameScreen.cpp index a5b4c6c..aeda96b 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -11,12 +11,15 @@ static constexpr Color INTERFACE {0, 0, 0, 230}; +static constexpr float BAR_WIDTH {160}; GameScreen::GameScreen(std::unique_ptr stage) : m_stage {std::move(stage)}, m_stats {std::make_shared()} { + m_stage->m_player.m_playground = Rectangle{ + BAR_WIDTH, 0, GetScreenWidth() - 2 * BAR_WIDTH, static_cast(GetScreenHeight())}; m_stage->m_stats = m_stats; } @@ -34,8 +37,8 @@ void GameScreen::draw() { m_stage->draw(); - DrawRectangle(0, 0, 160, 600, INTERFACE); - DrawRectangle(800 - 160, 0, 160, 600, INTERFACE); + DrawRectangle(0, 0, BAR_WIDTH, 600, INTERFACE); + DrawRectangle(800 - BAR_WIDTH, 0, BAR_WIDTH, 600, INTERFACE); DrawText(TextFormat("%d", m_stats->total_bullets), 5, 25, 20, DARKGRAY); DrawText(TextFormat("%d", m_stats->lifes), 5, 45, 20, DARKGREEN); DrawText(TextFormat("%d", m_stats->points), 5, 65, 20, GOLD); diff --git a/Player.cpp b/Player.cpp index 4e13ad0..94371c1 100644 --- a/Player.cpp +++ b/Player.cpp @@ -13,6 +13,7 @@ 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 constexpr float RADIUS {4}; static float damped_velocity(float dt, float direction, float velocity); @@ -22,6 +23,7 @@ Player::Player() : m_invulnerability {HIT_INVUL / 2.f}, m_position {400, 450}, m_velocity {0, 0}, + m_playground {0, 0, 800, 600}, m_controller {std::make_unique()} { } @@ -35,6 +37,20 @@ 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; + if (m_position.y < m_playground.y + RADIUS || m_position.y > m_playground.y + m_playground.height - RADIUS) { + m_position.y = + m_position.y < m_playground.y + m_playground.height / 2 ? + m_playground.y + RADIUS : + m_playground.y + m_playground.height - RADIUS; + m_velocity.y = 0; + } + if (m_position.x < m_playground.x + RADIUS || m_position.x > m_playground.x + m_playground.width - RADIUS) { + m_position.x = + m_position.x < m_playground.x + m_playground.width / 2 ? + m_playground.x + RADIUS : + m_playground.x + m_playground.width - RADIUS; + m_velocity.x = 0; + } m_invulnerability -= dt; } @@ -42,7 +58,7 @@ Player::update(const float dt) void Player::draw() { - DrawCircle(m_position.x, m_position.y, 4, m_invulnerability > 0 ? RED : LIGHTGRAY); + DrawCircle(m_position.x, m_position.y, RADIUS, m_invulnerability > 0 ? RED : LIGHTGRAY); } diff --git a/Player.h b/Player.h index b1cb629..d831234 100644 --- a/Player.h +++ b/Player.h @@ -18,6 +18,7 @@ struct Player float m_invulnerability; Vector2 m_position; Vector2 m_velocity; + Rectangle m_playground; std::unique_ptr m_controller; }; diff --git a/Stage.h b/Stage.h index fcb69b3..e5b4172 100644 --- a/Stage.h +++ b/Stage.h @@ -2,6 +2,7 @@ #include +#include "Player.h" #include "Stats.h" @@ -10,5 +11,6 @@ struct Stage virtual ~Stage() = default; virtual void update(float dt) = 0; virtual void draw() = 0; + Player m_player; std::shared_ptr m_stats; }; diff --git a/TestStage.cpp b/TestStage.cpp index 7dd5779..4aa1767 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -9,7 +9,6 @@ static constexpr Color DEEPSPACE {3, 5, 22, 255}; TestStage::TestStage() : - m_player {}, m_const {} { m_enemies.reserve(5); diff --git a/TestStage.h b/TestStage.h index 7ddbe06..776d32f 100644 --- a/TestStage.h +++ b/TestStage.h @@ -15,7 +15,6 @@ public: void update(float dt) override; void draw() override; private: - Player m_player; ConstantVelocitySystem m_const; std::vector m_enemies; }; -- cgit v1.1