diff --git a/src/explo_state.rs b/src/explo_state.rs index 9962e42..a269bfc 100644 --- a/src/explo_state.rs +++ b/src/explo_state.rs @@ -1,5 +1,8 @@ use bevy::prelude::*; -use std::f32::consts::E; +use bevy::input::mouse::MouseScrollUnit; +use bevy::input::mouse::MouseWheel; + +use std::f32::consts::{E, PI}; use crate::Player; use crate::GameState; @@ -105,6 +108,30 @@ pub fn player_mouse_move ( player.dragging_pos = Some(new_cursor); } +pub fn zoom( + mut evr_scroll: EventReader, + mut projection_query: Query<&mut Projection, With>, +) { + let Ok(mut projection) = projection_query.get_single_mut() else { + return; + }; + + let Projection::Perspective(ref mut perspective) = *projection else { + return; + }; + + for ev in evr_scroll.read() { + match ev.unit { + MouseScrollUnit::Line => { + perspective.fov = (0.6*PI).min((0.02*PI).max(perspective.fov * 0.9_f32.powf(ev.y))); + } + MouseScrollUnit::Pixel => { + println!("Scroll (pixel units): vertical: {}, horizontal: {}", ev.y, ev.x); + } + } + } +} + fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat { let pos_1 = ray_1.get_point(1.0); let pos_2 = ray_2.get_point(1.0); diff --git a/src/main.rs b/src/main.rs index a511a57..62bcb82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,6 +141,7 @@ fn main() { .add_systems(Update, game_state::player_interact.run_if(in_state(GameState::Game))) .add_systems(Update, explo_state::player_mouse_move.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) .add_systems(Update, explo_state::rotate_camera.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) + .add_systems(Update, explo_state::zoom.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) .add_systems(Update, game_state::ui_buttons.run_if(in_state(GameState::Game))) .add_systems(Update, game_state::ui_labels.run_if(in_state(GameState::Game))) .add_systems(OnExit(GameState::Game), despawn_screen::) @@ -207,7 +208,8 @@ fn star_setup( ) { commands.insert_resource(ClearColor(Color::BLACK)); - let stars = get_stars().unwrap(); + let stars: Vec = serde_json::from_str(include_str!("../data/stars.json")).expect("no star json provided"); + let star_mesh = meshes.add(Sphere::new(1.0).mesh().ico(3).unwrap()); @@ -247,14 +249,6 @@ fn star_setup( )); } -fn get_stars() -> std::io::Result> { - let data = include_str!("../data/stars.json"); - - let stars: Vec = serde_json::from_str(&data).unwrap(); - - Ok(stars) -} - fn star_position(star_data: StarData) -> Vec3 { // Convert declination to decimal degrees let text_ra = star_data.ra; @@ -290,15 +284,7 @@ fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 { } fn cons_setup(mut sky: ResMut) { - sky.content = get_cons().unwrap(); -} - -fn get_cons() -> std::io::Result> { - let data = include_str!("../data/constellations.json"); - - let sky_data: Vec = serde_json::from_str(&data).unwrap(); - - Ok(sky_data) + sky.content = serde_json::from_str(include_str!("../data/constellations.json")).expect("no constellation json provided"); } // Generic system that takes a component as a parameter, and will despawn all entities with that component