diff --git a/main.cpp b/main.cpp index 0554aaa..0b65279 100644 --- a/main.cpp +++ b/main.cpp @@ -31,16 +31,15 @@ Player player = { .height = player_width, }, .lives = 3, + .speed = 5.0, + .invulnerability_secs = 1, .last_hit = -10, .tear_speed = 10, .tear_range = 300, .tear_radius = 10.0f, .last_fired = 0, .fire_rate = .5, - }; -auto player_speed = 5.0f; -auto player_invulnerability = 1.0f; uint32_t score; @@ -56,28 +55,26 @@ Vector2 Player::center() const noexcept { } bool Player::is_invulnerable(const double now) const noexcept { - return now < this->last_hit + player_invulnerability; + return now < last_hit + invulnerability_secs; } -void Player::move(const float delta_x, const float delta_y) { - const auto x = std::min( - std::max(this->rect.x + delta_x, 0.0f), - level_right - this->rect.width); - - const auto y = std::min( - std::max(this->rect.y + delta_y, 0.0f), - level_bottom - this->rect.height); +void Player::move(const Vector2 delta) { + const auto [next_x, next_y] = Vector2Clamp( + Vector2Add({this->rect.x, this->rect.y}, delta), + {level_left, level_top}, + Vector2Subtract({level_right, level_bottom}, {this->rect.width, this->rect.height}) + ); bool collided = false; - const Rectangle next_position = { - .x = x, - .y = y, + const Rectangle next_rect = { + .x = next_x, + .y = next_y, .width = this->rect.width, .height = this->rect.height }; for (const auto wall: walls) { - if (CheckCollisionRecs(wall, next_position)) { + if (CheckCollisionRecs(wall, next_rect)) { collided = true; break; } @@ -85,8 +82,8 @@ void Player::move(const float delta_x, const float delta_y) { if (collided) { } else { - this->rect.x = x; - this->rect.y = y; + this->rect.x = next_x; + this->rect.y = next_y; } } @@ -220,9 +217,10 @@ int main() { std::string item_pickup_text_buffer; item_pickup_text_buffer.reserve(64); double item_last_picked_up = 0; - double item_pickup_message_duration = 3; while (!WindowShouldClose()) { + constexpr double item_pickup_message_duration = 3; + float dt_secs = GetFrameTime(); if (IsWindowResized()) { screen_height = static_cast(GetScreenHeight()); screen_width = static_cast(GetScreenWidth()); @@ -236,11 +234,11 @@ int main() { Vector2 delta{}; - if (IsKeyDown(KEY_W)) delta.y -= player_speed; - if (IsKeyDown(KEY_S)) delta.y += player_speed; - if (IsKeyDown(KEY_A)) delta.x -= player_speed; - if (IsKeyDown(KEY_D)) delta.x += player_speed; - player.move(delta.x, delta.y); + if (IsKeyDown(KEY_W)) delta.y -= player.speed; + if (IsKeyDown(KEY_S)) delta.y += player.speed; + if (IsKeyDown(KEY_A)) delta.x -= player.speed; + if (IsKeyDown(KEY_D)) delta.x += player.speed; + player.move(delta); std::optional tear_direction; @@ -291,7 +289,7 @@ int main() { } if (CheckCollisionCircleRec(enemy.center, enemy.radius, player.rect) && - player.last_hit + player_invulnerability < now) { + player.last_hit + player.invulnerability_secs < now) { player.lives--; player.last_hit = now; } diff --git a/types.h b/types.h index 0d50293..e623f39 100644 --- a/types.h +++ b/types.h @@ -11,6 +11,8 @@ struct Player { Rectangle rect; uint8_t lives; + float speed; + float invulnerability_secs; double last_hit; float tear_speed; float tear_range; @@ -22,7 +24,7 @@ struct Player { [[nodiscard]] bool is_invulnerable(double now) const noexcept; - void move(float delta_x, float delta_y); + void move(Vector2 delta); }; enum Direction {