You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
use bevy::{color::palettes::css::*, prelude::*};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.insert_resource(ClearColor(WHITE.into()))
|
|
.add_systems(Startup, setup_2d)
|
|
.add_systems(Update, move_camera)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct ParallaxDistance(f32);
|
|
|
|
fn setup_2d(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
) {
|
|
commands.spawn((
|
|
Camera2d,
|
|
AmbientLight {
|
|
brightness: 160.0,
|
|
..default()
|
|
},
|
|
));
|
|
|
|
commands.spawn((
|
|
Mesh2d(meshes.add(Rectangle::new(100.0, 100.0))),
|
|
MeshMaterial2d(materials.add(Color::from(RED))),
|
|
ParallaxDistance(1.0),
|
|
));
|
|
|
|
commands.spawn((
|
|
Mesh2d(meshes.add(Rectangle::new(100.0, 100.0))),
|
|
MeshMaterial2d(materials.add(Color::from(GREEN))),
|
|
ParallaxDistance(5.0),
|
|
));
|
|
|
|
commands.spawn((
|
|
Mesh2d(meshes.add(Rectangle::new(100.0, 100.0))),
|
|
MeshMaterial2d(materials.add(Color::from(BLUE))),
|
|
ParallaxDistance(10.0),
|
|
));
|
|
}
|
|
|
|
fn move_camera(keys: Res<ButtonInput<KeyCode>>, mut camera: Single<&mut Transform, With<Camera>>) {
|
|
const SPEED: f32 = 0.1;
|
|
if keys.pressed(KeyCode::ArrowLeft) {
|
|
camera.translation.z += SPEED;
|
|
} else if keys.pressed(KeyCode::ArrowRight) {
|
|
camera.translation.z -= SPEED;
|
|
} else if keys.pressed(KeyCode::ArrowUp) {
|
|
camera.translation.x -= SPEED;
|
|
} else if keys.pressed(KeyCode::ArrowDown) {
|
|
camera.translation.x += SPEED;
|
|
}
|
|
info!("Position: {:?}", camera.translation);
|
|
}
|