Created
April 30, 2021 07:23
-
-
Save rhulha/1697c629433896608b9485b6fe112321 to your computer and use it in GitHub Desktop.
Breakout Step 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::{ | |
prelude::*, | |
render::pass::ClearColor, | |
}; | |
fn main() { | |
App::build() | |
.add_plugins(DefaultPlugins) | |
.insert_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9))) | |
.add_startup_system(setup.system()) | |
.add_system(paddle_movement_system.system()) | |
.run(); | |
} | |
struct Paddle { | |
speed: f32, | |
} | |
enum Collider { | |
Solid, | |
Scorable, | |
Paddle, | |
} | |
fn setup( | |
commands: &mut Commands, | |
mut materials: ResMut<Assets<ColorMaterial>>, | |
asset_server: Res<AssetServer>, | |
) { | |
commands | |
// cameras | |
.spawn(OrthographicCameraBundle::new_2d()) | |
.spawn(UiCameraBundle::default()) | |
// paddle | |
.spawn(SpriteBundle { | |
material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()), | |
transform: Transform::from_xyz(0.0, -215.0, 0.0), | |
sprite: Sprite::new(Vec2::new(120.0, 30.0)), | |
..Default::default() | |
}) | |
.with(Paddle { speed: 500.0 }) | |
.with(Collider::Paddle); | |
// Add walls | |
let wall_material = materials.add(Color::rgb(0.8, 0.8, 0.8).into()); | |
let wall_thickness = 10.0; | |
let bounds = Vec2::new(900.0, 600.0); | |
commands | |
// left | |
.spawn(SpriteBundle { | |
material: wall_material.clone(), | |
transform: Transform::from_xyz(-bounds.x / 2.0, 0.0, 0.0), | |
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)), | |
..Default::default() | |
}) | |
.with(Collider::Solid) | |
// right | |
.spawn(SpriteBundle { | |
material: wall_material.clone(), | |
transform: Transform::from_xyz(bounds.x / 2.0, 0.0, 0.0), | |
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)), | |
..Default::default() | |
}) | |
.with(Collider::Solid) | |
// bottom | |
.spawn(SpriteBundle { | |
material: wall_material.clone(), | |
transform: Transform::from_xyz(0.0, -bounds.y / 2.0, 0.0), | |
sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)), | |
..Default::default() | |
}) | |
.with(Collider::Solid) | |
// top | |
.spawn(SpriteBundle { | |
material: wall_material, | |
transform: Transform::from_xyz(0.0, bounds.y / 2.0, 0.0), | |
sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)), | |
..Default::default() | |
}) | |
.with(Collider::Solid); | |
} | |
fn paddle_movement_system( | |
time: Res<Time>, | |
keyboard_input: Res<Input<KeyCode>>, | |
mut query: Query<(&Paddle, &mut Transform)>, | |
) { | |
for (paddle, mut transform) in query.iter_mut() { | |
let mut direction = 0.0; | |
if keyboard_input.pressed(KeyCode::Left) { | |
direction -= 1.0; | |
} | |
if keyboard_input.pressed(KeyCode::Right) { | |
direction += 1.0; | |
} | |
let translation = &mut transform.translation; | |
// move the paddle horizontally | |
translation.x += time.delta_seconds() * direction * paddle.speed; | |
// bound the paddle | |
translation.x = translation.x.min(380.0).max(-380.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment