diff options
-rw-r--r-- | GameScreen.cpp | 7 | ||||
-rw-r--r-- | Player.cpp | 18 | ||||
-rw-r--r-- | Player.h | 1 | ||||
-rw-r--r-- | Stage.h | 2 | ||||
-rw-r--r-- | TestStage.cpp | 1 | ||||
-rw-r--r-- | 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> stage) : m_stage {std::move(stage)}, m_stats {std::make_shared<Stats>()} { + m_stage->m_player.m_playground = Rectangle{ + BAR_WIDTH, 0, GetScreenWidth() - 2 * BAR_WIDTH, static_cast<float>(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); @@ -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<KeyboardController>()} { } @@ -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); } @@ -18,6 +18,7 @@ struct Player float m_invulnerability; Vector2 m_position; Vector2 m_velocity; + Rectangle m_playground; std::unique_ptr<Controller> m_controller; }; @@ -2,6 +2,7 @@ #include <memory> +#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<Stats> 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<Enemy> m_enemies; }; |