Compare commits
1 Commits
8e89f1bf70
...
fdf30d0446
| Author | SHA1 | Date | |
|---|---|---|---|
| fdf30d0446 |
45
main.cpp
45
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,11 +233,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<Direction> tear_direction;
|
||||
@@ -291,7 +288,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;
|
||||
}
|
||||
|
||||
4
types.h
4
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 {
|
||||
|
||||
Reference in New Issue
Block a user