refactor speed, invulnerability; refactor move to use Vec2

This commit is contained in:
2025-12-01 12:51:42 -05:00
parent 6a6249798e
commit d4f69278d9
2 changed files with 26 additions and 26 deletions

View File

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

View File

@@ -11,6 +11,8 @@
struct Player { struct Player {
Rectangle rect; Rectangle rect;
uint8_t lives; uint8_t lives;
float speed;
float invulnerability_secs;
double last_hit; double last_hit;
float tear_speed; float tear_speed;
float tear_range; float tear_range;
@@ -22,7 +24,7 @@ struct Player {
[[nodiscard]] bool is_invulnerable(double now) const noexcept; [[nodiscard]] bool is_invulnerable(double now) const noexcept;
void move(float delta_x, float delta_y); void move(Vector2 delta);
}; };
enum Direction { enum Direction {