From d1ad7c04bd5e5f9e7d318eb5da9111d72091f553 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 9 Sep 2025 21:49:31 -0700 Subject: [PATCH] parallax3d example, stub out parallax 2d example --- examples/parallax2d.rs | 59 ++++++++++++++++++++++++++++++++++++ examples/parallax3d.rs | 68 ++++++++++++++++++++++++++++++++++++++++++ justfile | 2 +- src/loading.rs | 25 ++++++++-------- 4 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 examples/parallax2d.rs create mode 100644 examples/parallax3d.rs diff --git a/examples/parallax2d.rs b/examples/parallax2d.rs new file mode 100644 index 0000000..ebf6f01 --- /dev/null +++ b/examples/parallax2d.rs @@ -0,0 +1,59 @@ +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>, + mut materials: ResMut>, +) { + 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>, mut camera: Single<&mut Transform, With>) { + 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); +} diff --git a/examples/parallax3d.rs b/examples/parallax3d.rs new file mode 100644 index 0000000..5d89593 --- /dev/null +++ b/examples/parallax3d.rs @@ -0,0 +1,68 @@ +use bevy::{color::palettes::css::*, prelude::*}; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .insert_resource(ClearColor(WHITE.into())) + .add_systems(Startup, setup_3d) + .add_systems(Update, move_camera) + .run(); +} + +fn setup_3d( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.spawn(( + Camera::default(), + Camera3d::default(), + Transform::default().looking_at(Vec3::new(0.0, -1.0, 0.0), Vec3::Y), + AmbientLight { + brightness: 160.0, + ..default() + }, + )); + + commands.spawn(( + Mesh3d(meshes.add(Cuboid::default())), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: RED.into(), + ..Default::default() + })), + Transform::from_xyz(0.0, -10.0, 0.0), + )); + + commands.spawn(( + Mesh3d(meshes.add(Cuboid::default())), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: GREEN.into(), + ..Default::default() + })), + Transform::from_xyz(0.0, -50.0, 0.0), + )); + + commands.spawn(( + Mesh3d(meshes.add(Cuboid::default())), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: BLUE.into(), + ..Default::default() + })), + Transform::from_xyz(0.0, -100.0, 0.0), + )); +} + +fn move_camera(keys: Res>, mut camera: Single<&mut Transform, With>) { + const SPEED: f32 = 0.1; + if keys.pressed(KeyCode::ArrowLeft) { + camera.translation.z += SPEED; + } else if keys.pressed(KeyCode::ArrowRight) { + camera.translation.z -= SPEED; + } + + if keys.pressed(KeyCode::ArrowUp) { + camera.translation.x -= SPEED; + } else if keys.pressed(KeyCode::ArrowDown) { + camera.translation.x += SPEED; + } +} diff --git a/justfile b/justfile index 7172f17..1345ee4 100644 --- a/justfile +++ b/justfile @@ -24,7 +24,7 @@ build-example EXAMPLE: build-bin GAME: # wasm binary - cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown + cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown --features hide_debug example NAME: (build-example NAME) (bindgen "wasm-dev" "examples"/NAME) cp web/example.html ./dist/examples/{{NAME}}/index.html diff --git a/src/loading.rs b/src/loading.rs index 28a6520..d10c549 100644 --- a/src/loading.rs +++ b/src/loading.rs @@ -13,17 +13,17 @@ impl Plugin for LoadingPlugin { .add_systems( Update, ( - ( - track_loading::, - track_loading::, - track_loading::, - track_loading::, - track_loading::, - track_loading::, - ) - .run_if(in_state(LoadingState::Active)), - toggle_state_visibility::.run_if(state_changed::), - ) + ( + track_loading::, + track_loading::, + track_loading::, + track_loading::, + track_loading::, + track_loading::, + ) + .run_if(in_state(LoadingState::Active)), + toggle_state_visibility::.run_if(state_changed::), + ), ) .add_systems(PostUpdate, check_progress); } @@ -48,8 +48,7 @@ fn spawn_loading_screen(mut commands: Commands) { ..default() }, LoadingState::Active, - children![ - ( + children![( TextColor(WHITE.into()), Text::new("Credits"), BackgroundColor(BLACK.into())