Skip to content

Commit

Permalink
Merge pull request #66 from CarsonBurke/krypa-zooming
Browse files Browse the repository at this point in the history
krypta zooming and LMB readme correction
  • Loading branch information
zaycev authored Sep 3, 2024
2 parents 3a8c250 + 3142053 commit 785253c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cargo run --example krypta
```

- WASD to control camera.
- LMC to place a light source.
- SHIFT+LMC to place a light source.
- RMC to change color of light source.

## TODOs
Expand Down
26 changes: 25 additions & 1 deletion examples/krypta.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::color::palettes;
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy::render::camera::RenderTarget;
use bevy::render::texture::{ImageFilterMode, ImageSamplerDescriptor};
Expand All @@ -16,6 +17,8 @@ pub const Z_BASE_FLOOR: f32 = 100.0; // Base z-coordinate for 2D layers.
pub const Z_BASE_OBJECTS: f32 = 200.0; // Ground object sprites.
pub const SCREEN_SIZE: (f32, f32) = (1280.0, 720.0);
pub const CAMERA_SCALE: f32 = 1.0;
pub const CAMERA_SCALE_BOUNDS: (f32, f32) = (1., 20.);
pub const CAMERA_ZOOM_SPEED: f32 = 3.;

// Misc components.
#[derive(Component)]
Expand Down Expand Up @@ -70,7 +73,7 @@ fn main()
.register_type::<BevyMagicLight2DSettings>()
.register_type::<LightPassParams>()
.add_systems(Startup, setup.after(setup_post_processing_camera))
.add_systems(Update, system_move_camera)
.add_systems(Update, (system_move_camera, system_camera_zoom))
.add_systems(Update, system_control_mouse_light.after(system_move_camera))
.run();
}
Expand Down Expand Up @@ -962,3 +965,24 @@ fn system_move_camera(
camera_transform.translation.y = camera_current.y;
}
}

fn system_camera_zoom(
mut cameras: Query<&mut OrthographicProjection, With<SpriteCamera>>,
time: Res<Time>,
mut scroll_event_reader: EventReader<MouseWheel>,
) {
let mut projection_delta = 0.;

for event in scroll_event_reader.read() {
projection_delta += event.y * CAMERA_ZOOM_SPEED;
}

if projection_delta == 0. {
return;
}

for mut camera in cameras.iter_mut() {
camera.scale = (camera.scale - projection_delta * time.delta_seconds())
.clamp(CAMERA_SCALE_BOUNDS.0, CAMERA_SCALE_BOUNDS.1);
}
}

0 comments on commit 785253c

Please sign in to comment.