Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ball moving slowly, no collision yet #1

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const BALL_START_X: f32 = 32.5;
const BALL_START_Y: f32 = -270.;
const BALL_SIZE: f32 = 12.5;
const BALL_COLOR: Color = Color::PURPLE;
const BALL_SPEED: f32 = 4.0;
const INITIAL_BALL_DIRECTION: Vec2 = Vec2::new(0.5, -0.5);
const PADDLE_START_X: f32 = 32.5;
const PADDLE_START_Y: f32 = -300.;
const PADDLE_HEIGHT: f32 = 20.;
Expand All @@ -34,12 +36,18 @@ const RIGHT_BOUND_PADDLE: f32 = RIGHT_WALL - WALL_SIZE - (PADDLE_WIDTH / 2.);
#[derive(Component)]
struct Paddle;

#[derive(Component)]
struct Ball;

#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec2);

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(BACKGROUND_COLOR))
.add_systems(Startup, setup)
.add_systems(FixedUpdate, move_paddle)
.add_systems(FixedUpdate, (move_paddle, apply_velocity))
.add_systems(Update, bevy::window::close_on_esc)
.run();
}
Expand Down Expand Up @@ -120,12 +128,16 @@ fn setup(
}

// Draw Ball
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(BALL_SIZE).into()).into(),
material: materials.add(ColorMaterial::from(BALL_COLOR)),
transform: Transform::from_translation(Vec3::new(BALL_START_X, BALL_START_Y, 0.)),
..default()
});
commands.spawn((
MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(BALL_SIZE).into()).into(),
material: materials.add(ColorMaterial::from(BALL_COLOR)),
transform: Transform::from_translation(Vec3::new(BALL_START_X, BALL_START_Y, 0.)),
..default()
},
Ball,
Velocity(INITIAL_BALL_DIRECTION.normalize() * BALL_SPEED),
));

// Draw Paddle
commands.spawn((
Expand Down Expand Up @@ -159,3 +171,10 @@ fn move_paddle(
paddle_transform.translation.x =
new_paddle_position.clamp(LEFT_BOUND_PADDLE, RIGHT_BOUND_PADDLE);
}

fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
for (mut transform, velocity) in &mut query {
transform.translation.x += velocity.x * time_step.period.as_secs_f32();
transform.translation.y += velocity.y * time_step.period.as_secs_f32();
}
}
Loading