use bevy::{
input::mouse::{MouseScrollUnit, MouseWheel},
prelude::*,
};
use crate::{
client::{
Me,
starfield::{BACK_STARFIELD_SIZE, FRONT_STARFIELD_SIZE, MID_STARFIELD_SIZE},
},
ecs::{MainCamera, StarfieldBack, StarfieldFront, StarfieldMid},
};
pub fn zoom_plugin(app: &mut App) {
app.add_systems(Update, on_scroll);
}
fn on_scroll(
mut scroll_events: EventReader<MouseWheel>,
window: Single<&Window>,
starfield_back: Single<
(&mut Sprite, &mut Transform),
(
With<StarfieldBack>,
Without<StarfieldMid>,
Without<StarfieldFront>,
),
>,
starfield_mid: Single<
(&mut Sprite, &mut Transform),
(
With<StarfieldMid>,
Without<StarfieldBack>,
Without<StarfieldFront>,
),
>,
starfield_front: Single<
(&mut Sprite, &mut Transform),
(
With<StarfieldFront>,
Without<StarfieldBack>,
Without<StarfieldMid>,
),
>,
mut camera: Single<
&mut Transform,
(
With<MainCamera>,
Without<Me>,
Without<StarfieldFront>,
Without<StarfieldMid>,
Without<StarfieldBack>,
),
>,
player: Single<
&Transform,
(
With<Me>,
Without<StarfieldFront>,
Without<StarfieldMid>,
Without<StarfieldBack>,
Without<MainCamera>,
),
>,
) {
let (mut starfield_back, mut starfield_back_pos) = starfield_back.into_inner();
let (mut starfield_mid, mut starfield_mid_pos) = starfield_mid.into_inner();
let (mut starfield_front, mut starfield_front_pos) = starfield_front.into_inner();
for event in scroll_events.read() {
match event.unit {
MouseScrollUnit::Line | MouseScrollUnit::Pixel => {
if event.y > 0.0 {
camera.scale *= 0.97;
} else {
camera.scale *= 1.03;
}
starfield_back.custom_size =
Some(window.size() * camera.scale.z + Vec2::splat(BACK_STARFIELD_SIZE * 2.0));
starfield_mid.custom_size =
Some(window.size() * camera.scale.z + Vec2::splat(MID_STARFIELD_SIZE * 2.0));
starfield_front.custom_size =
Some(window.size() * camera.scale.z + Vec2::splat(FRONT_STARFIELD_SIZE * 2.0));
starfield_back_pos.translation = player.translation
+ (-player.translation / 3.0) % BACK_STARFIELD_SIZE
+ (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
* camera.scale.z
/ 2.0)
% BACK_STARFIELD_SIZE
+ Vec3::new(0.0, BACK_STARFIELD_SIZE, 0.0)
- Vec3::new(0.0, 0.0, 5.0);
starfield_mid_pos.translation = player.translation
+ (-player.translation / 2.5) % MID_STARFIELD_SIZE
+ (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
* camera.scale.z
/ 2.0)
% MID_STARFIELD_SIZE
+ Vec3::new(0.0, MID_STARFIELD_SIZE, 0.0)
- Vec3::new(0.0, 0.0, 4.5);
starfield_front_pos.translation = player.translation
+ (-player.translation / 2.0) % FRONT_STARFIELD_SIZE
+ (Vec3::new(window.resolution.width(), -window.resolution.height(), 0.0)
* camera.scale.z
/ 2.0)
% FRONT_STARFIELD_SIZE
+ Vec3::new(0.0, FRONT_STARFIELD_SIZE, 0.0)
- Vec3::new(0.0, 0.0, 4.0);
}
}
}
}