Skip to content

Commit

Permalink
Randomize car starting velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
nonk123 committed Jun 12, 2024
1 parent 92d17fb commit 42dcfb4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/entities/car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "entities/pedo.hpp"

const constexpr float CAR_VELOCITY = 128.0f, CAR_RANDOM = 312.0f;
const constexpr float CAR_ACCEL = 212.0f, CAR_SLOWDOWN = 864.0f;

const constexpr float PEDO_DEATH_DELAY = 5.0f, CAR_REALIZATION_DELAY = 3.0f;
Expand All @@ -22,9 +23,12 @@ const constexpr float BRAKE_TRAIL_WIDTH = 1.5f;

static void draw_brake_trail(float, float, float, float, float);

Car::Car(float left, float width, float height, float velocity, Direction direction)
Car::Car(float left, float width, float height, Direction direction)
: Entity(left, 0.0f, width, height), direction(direction) {
vel.y = velocity * (direction == Direction::FORWARD ? 1.0f : -1.0f);
const float fdir = (direction == Direction::FORWARD ? 1.0f : -1.0f);
const float random = GetRandomValue(0, 100) / 100.0f;

vel.y = (CAR_VELOCITY + random * CAR_RANDOM) * fdir;
starting_velocity = std::abs(vel.y);

if (direction == Direction::FORWARD) {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/car.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Car : public Entity {
float realization_countdown = 0.0f;

public:
Car(float, float, float, float, Direction);
Car(float, float, float, Direction);

std::optional<std::size_t> get_lane_idx() const;

Expand Down
3 changes: 1 addition & 2 deletions src/runner/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "entities/pedo.hpp"

const constexpr float BASE_SPAWN_DELAY_PER_LANE = 3.0f, SPAWN_DELAY_RANDOM_OFFSET = 6.0f;
const constexpr float CAR_VELOCITY = 312.0f;

extern void restart();

Expand Down Expand Up @@ -96,6 +95,6 @@ static void spawn_cars() {

left += (LANE_WIDTH - WIDTH) * 0.5f;

Game::entities.emplace_back(new Car(left, WIDTH, HEIGHT, CAR_VELOCITY, dir));
Game::entities.emplace_back(new Car(left, WIDTH, HEIGHT, dir));
}
}

0 comments on commit 42dcfb4

Please sign in to comment.