From 63c64254a80564e11987ba4af289cf8d47774668 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 25 Apr 2022 00:18:36 +0200 Subject: Added move constraints --- Player.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'Player.cpp') 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); } -- cgit v1.1