Skip to content

Commit aa569cd

Browse files
Copilotnpv2k1
andcommitted
Address code review feedback: extract magic numbers to constants
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
1 parent f993f73 commit aa569cd

5 files changed

Lines changed: 62 additions & 11 deletions

File tree

src/components/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use bevy::prelude::*;
77
use serde::{Deserialize, Serialize};
88

9+
use crate::game::constants::camera::{MAX_SMOOTHING, MIN_SMOOTHING};
10+
911
/// Marker component for the player entity
1012
#[derive(Component, Debug, Default)]
1113
pub struct Player;
@@ -210,7 +212,7 @@ impl CameraFollow {
210212
}
211213

212214
pub fn with_smoothing(mut self, smoothing: f32) -> Self {
213-
self.smoothing = smoothing.clamp(0.01, 1.0);
215+
self.smoothing = smoothing.clamp(MIN_SMOOTHING, MAX_SMOOTHING);
214216
self
215217
}
216218
}

src/game/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,44 @@ pub mod constants {
1111
pub const WINDOW_HEIGHT: f32 = 720.0;
1212
/// Game title
1313
pub const GAME_TITLE: &str = "2D Mario-Style Platformer";
14+
15+
/// Camera smoothing constants
16+
pub mod camera {
17+
/// Target framerate for smoothing calculations
18+
pub const TARGET_FRAMERATE: f32 = 60.0;
19+
/// Minimum smoothing factor (instant follow)
20+
pub const MIN_SMOOTHING: f32 = 0.01;
21+
/// Maximum smoothing factor (slowest follow)
22+
pub const MAX_SMOOTHING: f32 = 1.0;
23+
}
24+
25+
/// Obstacle spawning constants
26+
pub mod obstacles {
27+
/// X position where obstacles spawn (right side of screen)
28+
pub const SPAWN_X: f32 = 700.0;
29+
/// Minimum Y position for obstacle spawn
30+
pub const SPAWN_Y_MIN: f32 = -200.0;
31+
/// Maximum Y position for obstacle spawn
32+
pub const SPAWN_Y_MAX: f32 = 150.0;
33+
/// Minimum obstacle width
34+
pub const WIDTH_MIN: f32 = 30.0;
35+
/// Maximum obstacle width
36+
pub const WIDTH_MAX: f32 = 60.0;
37+
/// Minimum obstacle height
38+
pub const HEIGHT_MIN: f32 = 30.0;
39+
/// Maximum obstacle height
40+
pub const HEIGHT_MAX: f32 = 60.0;
41+
/// Minimum obstacle speed
42+
pub const SPEED_MIN: f32 = 100.0;
43+
/// Maximum obstacle speed
44+
pub const SPEED_MAX: f32 = 250.0;
45+
/// X position at which obstacles are despawned (left side of screen)
46+
pub const DESPAWN_X: f32 = -800.0;
47+
}
48+
49+
/// Scoring constants
50+
pub mod scoring {
51+
/// Points awarded for surviving obstacle collision
52+
pub const OBSTACLE_SURVIVE_POINTS: u32 = 10;
53+
}
1454
}

src/systems/camera.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use bevy::prelude::*;
44

55
use crate::components::{CameraFollow, MainCamera, Player};
6+
use crate::game::constants::camera::TARGET_FRAMERATE;
67

78
/// Updates camera to follow the target entity smoothly
89
pub fn camera_follow_system(
@@ -17,8 +18,11 @@ pub fn camera_follow_system(
1718
for (mut camera_transform, camera_follow) in camera_query.iter_mut() {
1819
let target_position = player_transform.translation + camera_follow.offset;
1920

20-
// Smooth camera movement using lerp
21-
let lerp_factor = 1.0 - camera_follow.smoothing.powf(time.delta_seconds() * 60.0);
21+
// Smooth camera movement using lerp (frame-rate independent)
22+
let lerp_factor = 1.0
23+
- camera_follow
24+
.smoothing
25+
.powf(time.delta_seconds() * TARGET_FRAMERATE);
2226
camera_transform.translation.x = camera_transform
2327
.translation
2428
.x

src/systems/collision.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use bevy::prelude::*;
44

55
use crate::components::{BoxCollider, DamageOnContact, Health, Obstacle, Player};
6+
use crate::game::constants::scoring::OBSTACLE_SURVIVE_POINTS;
67
use crate::resources::Score;
78

89
/// Checks for collisions between player and obstacles
@@ -50,8 +51,8 @@ pub fn check_obstacle_collisions(
5051
// Remove the obstacle
5152
commands.entity(obstacle_entity).despawn();
5253

53-
// Add score for surviving
54-
score.add(10);
54+
// Add score for surviving collision
55+
score.add(OBSTACLE_SURVIVE_POINTS);
5556
}
5657
}
5758
}

src/systems/obstacle.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use bevy::prelude::*;
44
use rand::Rng;
55

66
use crate::components::{AutoMove, BoxCollider, DamageOnContact, Obstacle};
7+
use crate::game::constants::obstacles::{
8+
DESPAWN_X, HEIGHT_MAX, HEIGHT_MIN, SPAWN_X, SPAWN_Y_MAX, SPAWN_Y_MIN, SPEED_MAX, SPEED_MIN,
9+
WIDTH_MAX, WIDTH_MIN,
10+
};
711
use crate::resources::ObstacleSpawnTimer;
812

913
/// Spawns obstacles at regular intervals
@@ -18,15 +22,15 @@ pub fn spawn_obstacles(
1822
let mut rng = rand::thread_rng();
1923

2024
// Random spawn position on the right side of the screen
21-
let spawn_x = 700.0;
22-
let spawn_y = rng.gen_range(-200.0..150.0);
25+
let spawn_x = SPAWN_X;
26+
let spawn_y = rng.gen_range(SPAWN_Y_MIN..SPAWN_Y_MAX);
2327

2428
// Random obstacle size
25-
let width = rng.gen_range(30.0..60.0);
26-
let height = rng.gen_range(30.0..60.0);
29+
let width = rng.gen_range(WIDTH_MIN..WIDTH_MAX);
30+
let height = rng.gen_range(HEIGHT_MIN..HEIGHT_MAX);
2731

2832
// Random speed
29-
let speed = rng.gen_range(100.0..250.0);
33+
let speed = rng.gen_range(SPEED_MIN..SPEED_MAX);
3034

3135
commands.spawn((
3236
Obstacle,
@@ -53,7 +57,7 @@ pub fn despawn_offscreen_obstacles(
5357
) {
5458
for (entity, transform) in query.iter() {
5559
// Despawn if too far left
56-
if transform.translation.x < -800.0 {
60+
if transform.translation.x < DESPAWN_X {
5761
commands.entity(entity).despawn();
5862
}
5963
}

0 commit comments

Comments
 (0)