Skip to content

Commit 5859bb1

Browse files
committed
accelerate ball
1 parent fc9e9e5 commit 5859bb1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/pong.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ const PADDLE_Z: f32 = Z_BACK;
3434
const PADDLE_SPRITE_NUM: usize = 0;
3535

3636
const BALL_RADIUS: f32 = 2.0;
37-
const BALL_VELOCITY_X: f32 = 25.0;
37+
const BALL_VELOCITY_X: f32 = 30.0;
3838
const BALL_VELOCITY_Y: f32 = 15.0;
39+
const BALL_ACCELERATION: f32 = 0.2;
40+
const MAX_BALL_VELOCITY_X: f32 = BALL_VELOCITY_X * 2.0;
41+
const MAX_BALL_VELOCITY_Y: f32 = BALL_VELOCITY_Y * 2.0;
3942
pub const BALL_Z: f32 = Z_BACK;
4043
const BALL_SPRITE_NUM: usize = 1;
4144

@@ -88,6 +91,7 @@ pub enum BallState {
8891

8992
pub struct Ball {
9093
pub velocity: [f32; 2],
94+
pub acceleration: f32,
9195
pub radius: f32,
9296
pub state: BallState,
9397
pub waiting_time: f32,
@@ -99,6 +103,11 @@ impl Ball {
99103
self.waiting_time = 2.0;
100104
self.velocity = [BALL_VELOCITY_X, BALL_VELOCITY_Y]
101105
}
106+
pub fn accelerate(&mut self) {
107+
let velocity_x = (self.velocity[0] + (self.velocity[0] * self.acceleration)).min(MAX_BALL_VELOCITY_X);
108+
let velocity_y = (self.velocity[1] + (self.velocity[1] * self.acceleration)).min(MAX_BALL_VELOCITY_Y);
109+
self.velocity = [velocity_x, velocity_y]
110+
}
102111
}
103112

104113
impl Component for Ball {
@@ -172,6 +181,7 @@ fn initialise_ball(world: &mut World, sprite_sheet_handle: Handle<SpriteSheet>)
172181
.with(Ball {
173182
radius: BALL_RADIUS,
174183
velocity: [BALL_VELOCITY_X, BALL_VELOCITY_Y],
184+
acceleration: BALL_ACCELERATION,
175185
state: BallState::Waiting,
176186
waiting_time: 2.0,
177187
})

src/systems/bounce.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl<'s> System<'s> for BounceSystem {
3939
|| (ball_y >= ARENA_HEIGHT - ball.radius && ball.velocity[1] > 0.0)
4040
{
4141
ball.velocity[1] = -ball.velocity[1];
42+
ball.accelerate();
4243
play_bounce(&*sounds, &storage, audio_output.as_ref().map(|o| o.deref()));
4344
}
4445

@@ -64,6 +65,7 @@ impl<'s> System<'s> for BounceSystem {
6465
|| (paddle.side == Side::Right && ball.velocity[0] > 0.0)
6566
{
6667
ball.velocity[0] = -ball.velocity[0];
68+
ball.accelerate();
6769
play_bounce(&*sounds, &storage, audio_output.as_ref().map(|o| o.deref()));
6870
}
6971
}

0 commit comments

Comments
 (0)