Last active
March 14, 2023 18:11
-
-
Save rparrett/6fbe34e8a07cabac38fd4a040e29f1f4 to your computer and use it in GitHub Desktop.
bevy_mod_parallax scaling
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
diff --git a/examples/cyberpunk.rs b/examples/cyberpunk.rs | |
index 1d27172..45ff7eb 100644 | |
--- a/examples/cyberpunk.rs | |
+++ b/examples/cyberpunk.rs | |
@@ -1,9 +1,11 @@ | |
use bevy::prelude::*; | |
use bevy_parallax::{ | |
- LayerData, LayerSpeed, ParallaxCameraComponent, ParallaxMoveEvent, ParallaxPlugin, | |
- ParallaxResource, | |
+ LayerComponent, LayerData, LayerSpeed, ParallaxCameraComponent, ParallaxMoveEvent, | |
+ ParallaxPlugin, ParallaxResource, | |
}; | |
+const INITIAL_SCALE: f32 = 4.5; | |
+ | |
fn main() { | |
// Define window | |
let primary_window = Window { | |
@@ -23,7 +25,7 @@ fn main() { | |
tile_size: Vec2::new(96.0, 160.0), | |
cols: 1, | |
rows: 1, | |
- scale: 4.5, | |
+ scale: INITIAL_SCALE, | |
z: 0.0, | |
..Default::default() | |
}, | |
@@ -33,7 +35,7 @@ fn main() { | |
tile_size: Vec2::new(144.0, 160.0), | |
cols: 1, | |
rows: 1, | |
- scale: 4.5, | |
+ scale: INITIAL_SCALE, | |
z: 1.0, | |
..Default::default() | |
}, | |
@@ -43,7 +45,7 @@ fn main() { | |
tile_size: Vec2::new(272.0, 160.0), | |
cols: 1, | |
rows: 1, | |
- scale: 4.5, | |
+ scale: INITIAL_SCALE, | |
z: 2.0, | |
..Default::default() | |
}, | |
@@ -61,7 +63,9 @@ fn main() { | |
) | |
.add_plugin(ParallaxPlugin) | |
.add_startup_system(initialize_camera_system) | |
+ .add_startup_system(setup_player) | |
.add_system(move_camera_system) | |
+ .add_system(zoom) | |
.run(); | |
} | |
@@ -72,6 +76,40 @@ pub fn initialize_camera_system(mut commands: Commands) { | |
.insert(ParallaxCameraComponent); | |
} | |
+fn setup_player(mut commands: Commands) { | |
+ commands.spawn(SpriteBundle { | |
+ sprite: Sprite { | |
+ custom_size: Some(Vec2::splat(50.)), | |
+ color: Color::RED, | |
+ ..default() | |
+ }, | |
+ transform: Transform::from_xyz(0., 0., 100.), | |
+ ..default() | |
+ }); | |
+} | |
+ | |
+fn zoom( | |
+ input: Res<Input<KeyCode>>, | |
+ mut query_camera: Query<&mut OrthographicProjection>, | |
+ mut container: Query<&mut Transform, With<LayerComponent>>, | |
+) { | |
+ let mut proj = query_camera.single_mut(); | |
+ | |
+ if input.pressed(KeyCode::Minus) { | |
+ proj.scale += 0.2; | |
+ } | |
+ | |
+ if input.pressed(KeyCode::Equals) { | |
+ proj.scale -= 0.2; | |
+ } | |
+ | |
+ proj.scale = proj.scale.clamp(0.2, 5.); | |
+ | |
+ for mut transform in container.iter_mut() { | |
+ transform.scale = Vec2::splat(INITIAL_SCALE * proj.scale).extend(1.); | |
+ } | |
+} | |
+ | |
// Send a ParallaxMoveEvent with the desired camera movement speed | |
pub fn move_camera_system( | |
keyboard_input: Res<Input<KeyCode>>, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment