Skip to content

Commit

Permalink
feat: improve automatically viewport zoom when switching levels
Browse files Browse the repository at this point in the history
Window size is also a consideration.
  • Loading branch information
ShenMian committed Jan 23, 2024
1 parent 3f2a2c9 commit 371471c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/systems/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn spawn_board(
mut commands: Commands,
database: Res<Database>,
mut camera: Query<(&mut Transform, &mut MainCamera)>,
window: Query<&Window>,
board: Query<Entity, With<Board>>,
level_id: Res<LevelId>,
asset_server: Res<AssetServer>,
Expand All @@ -76,7 +77,16 @@ pub fn spawn_board(
let (mut transform, mut main_camera) = camera.single_mut();
transform.translation.x = (board_size.x - tile_size.x) / 2.0;
transform.translation.y = (board_size.y + tile_size.y) / 2.0;
main_camera.target_scale = 0.2 * (cmp::max(level.dimensions.x, level.dimensions.y) as f32);

let window = window.single();
let width_scale = board_size.x / window.resolution.width();
let height_scale = board_size.y / window.resolution.height();
let scale = if width_scale > height_scale {
width_scale
} else {
height_scale
};
main_camera.target_scale = scale / 0.9;

// despawn the previous `Board`
commands.entity(board.single()).despawn_recursive();
Expand Down

0 comments on commit 371471c

Please sign in to comment.