parallax3d example, stub out parallax 2d example

main
Elijah Voigt 2 months ago
parent e2554bd8f7
commit d1ad7c04bd

@ -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<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);
}

@ -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<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
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<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;
}
if keys.pressed(KeyCode::ArrowUp) {
camera.translation.x -= SPEED;
} else if keys.pressed(KeyCode::ArrowDown) {
camera.translation.x += SPEED;
}
}

@ -24,7 +24,7 @@ build-example EXAMPLE:
build-bin GAME: build-bin GAME:
# wasm binary # 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) example NAME: (build-example NAME) (bindgen "wasm-dev" "examples"/NAME)
cp web/example.html ./dist/examples/{{NAME}}/index.html cp web/example.html ./dist/examples/{{NAME}}/index.html

@ -23,7 +23,7 @@ impl Plugin for LoadingPlugin {
) )
.run_if(in_state(LoadingState::Active)), .run_if(in_state(LoadingState::Active)),
toggle_state_visibility::<LoadingState>.run_if(state_changed::<LoadingState>), toggle_state_visibility::<LoadingState>.run_if(state_changed::<LoadingState>),
) ),
) )
.add_systems(PostUpdate, check_progress); .add_systems(PostUpdate, check_progress);
} }
@ -48,8 +48,7 @@ fn spawn_loading_screen(mut commands: Commands) {
..default() ..default()
}, },
LoadingState::Active, LoadingState::Active,
children![ children![(
(
TextColor(WHITE.into()), TextColor(WHITE.into()),
Text::new("Credits"), Text::new("Credits"),
BackgroundColor(BLACK.into()) BackgroundColor(BLACK.into())

Loading…
Cancel
Save