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.
95 lines
2.7 KiB
Rust
95 lines
2.7 KiB
Rust
use games::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((BaseGamePlugin {
|
|
name: "parallax example".into(),
|
|
title: "Parallax".into(),
|
|
game_type: GameType::Two,
|
|
},))
|
|
.add_systems(Startup, spawn_background)
|
|
.add_systems(Update, move_camera)
|
|
.add_systems(Update, parallax_gizmos)
|
|
.add_systems(Update, move_parallax_items)
|
|
.run();
|
|
}
|
|
|
|
fn spawn_background(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
) {
|
|
let mesh = Mesh2d(meshes.add(Circle::new(50.0)));
|
|
let material = MeshMaterial2d(materials.add(ColorMaterial::from_color(RED)));
|
|
commands.spawn((
|
|
mesh.clone(),
|
|
material.clone(),
|
|
Parallax(1.0),
|
|
Visibility::Inherited,
|
|
children![(Text2d("1.0".into()), Transform::from_xyz(0.0, 35.0, 0.0))],
|
|
));
|
|
commands.spawn((
|
|
mesh.clone(),
|
|
material.clone(),
|
|
Parallax(2.0),
|
|
Visibility::Inherited,
|
|
children![(Text2d("2.0".into()), Transform::from_xyz(0.0, 35.0, 0.0))],
|
|
));
|
|
commands.spawn((
|
|
mesh.clone(),
|
|
material.clone(),
|
|
Parallax(4.0),
|
|
Visibility::Inherited,
|
|
children![(Text2d("4.0".into()), Transform::from_xyz(0.0, 35.0, 0.0))],
|
|
));
|
|
commands.spawn((
|
|
mesh.clone(),
|
|
material.clone(),
|
|
Parallax(8.0),
|
|
Visibility::Inherited,
|
|
children![(Text2d("8.0".into()), Transform::from_xyz(0.0, 35.0, 0.0))],
|
|
));
|
|
}
|
|
|
|
fn move_camera(mut t: Single<&mut Transform, With<Camera2d>>, keys: Res<ButtonInput<KeyCode>>) {
|
|
if keys.pressed(KeyCode::ArrowLeft) {
|
|
t.translation.x -= 5.0;
|
|
} else if keys.pressed(KeyCode::ArrowRight) {
|
|
t.translation.x += 5.0;
|
|
}
|
|
|
|
if keys.pressed(KeyCode::ArrowDown) {
|
|
t.translation.y -= 5.0;
|
|
} else if keys.pressed(KeyCode::ArrowUp) {
|
|
t.translation.y += 5.0;
|
|
}
|
|
}
|
|
|
|
fn parallax_gizmos(mut gizmos: Gizmos, q: Query<&Transform, With<Parallax>>) {
|
|
// Closest to camera
|
|
// Parallax(1)
|
|
q.iter().for_each(|t| {
|
|
gizmos
|
|
.grid_2d(
|
|
t.translation.truncate(),
|
|
UVec2::new(5, 5),
|
|
Vec2::splat(10.),
|
|
RED,
|
|
)
|
|
.outer_edges();
|
|
});
|
|
}
|
|
|
|
// TODO: Move to src/parallax.rs
|
|
fn move_parallax_items(
|
|
mut q: Query<(Entity, &ViewVisibility, &mut ParallaxRepeatIteration), (With<Parallax>, Changed<ViewVisibility>)>,
|
|
) {
|
|
q.iter_mut().for_each(|(e, vis, mut pri)| {
|
|
warn!("{e} {:?}", vis.get());
|
|
if !vis.get() {
|
|
pri.0 += 1;
|
|
todo!("Works when moving camera right, make it work left too");
|
|
}
|
|
});
|
|
}
|