@@ -10,45 +10,38 @@ pub struct CameraTarget;
1010#[ derive( Component ) ]
1111pub struct MainCamera ;
1212
13- impl Plugin for CameraPlugin {
14- fn build ( & self , app : & mut App ) {
13+ impl Plugin for CameraPlugin {
14+ fn build ( & self , app : & mut App ) {
1515 app. add_systems ( Startup , setup_main_camera) ;
1616 app. add_systems ( Update , ( update_camera, zoom_camera) . chain ( ) ) ;
1717 }
1818}
1919
20-
21- //Sets up a 2d camera and attaches the MainCamera marker to it
22- fn setup_main_camera (
23- mut commands : Commands
24- ) {
25- commands. spawn ( (
26- Camera2dBundle :: default ( ) ,
27- MainCamera
28- ) ) ;
20+ //Sets up a 2d camera and attaches the MainCamera marker to it
21+ fn setup_main_camera ( mut commands : Commands ) {
22+ commands. spawn ( ( Camera2dBundle :: default ( ) , MainCamera ) ) ;
2923}
3024
31-
3225//Update camera function will have our main camera follow any entity with the CameraTarget tag
3326fn update_camera (
34- mut camera_query : Query < & mut Transform , ( With < MainCamera > , Without < CameraTarget > ) > ,
27+ mut camera_query : Query < & mut Transform , ( With < MainCamera > , Without < CameraTarget > ) > ,
3528 target_query : Query < & Transform , ( With < CameraTarget > , Without < MainCamera > ) > ,
36- ) {
29+ ) {
3730 //getting camera from query
38- let Ok ( mut camera) = camera_query. get_single_mut ( ) else {
31+ let Ok ( mut camera) = camera_query. get_single_mut ( ) else {
3932 return ;
4033 } ;
4134
4235 //getting target from query
43- let Ok ( target) = target_query. get_single ( ) else {
36+ let Ok ( target) = target_query. get_single ( ) else {
4437 return ;
4538 } ;
4639
4740 //Gets the x and y coordinates of our target
48- let Vec3 { x , y , .. } = target. translation ;
41+ let Vec3 { x , y , .. } = target. translation ;
4942
5043 //Stores the target x y with our camera z cuz our camera z not changing
51- let direction = Vec3 :: new ( x, y , camera. translation . z ) ;
44+ let direction = Vec3 :: new ( x, y , camera. translation . z ) ;
5245
5346 //Debug statements if we want to use them :)
5447 //info!("Camera: {:?}", camera);
@@ -60,31 +53,28 @@ fn update_camera(
6053
6154//Camera Zooming Function, uses MouseWheel Scrolling up/down for zooming in/out
6255fn zoom_camera (
63- mut scroll_event : EventReader < MouseWheel > ,
64- mut camera : Query < & mut OrthographicProjection , With < MainCamera > >
65- ) {
66-
67-
56+ mut scroll_event : EventReader < MouseWheel > ,
57+ mut camera : Query < & mut OrthographicProjection , With < MainCamera > > ,
58+ ) {
6859 //Zoom Sensitivity
6960 let zoom_factor = 0.1 ;
7061
7162 //Amount to zoom in this update cycle
7263 let mut zoom_amount = 0.0 ;
7364
74-
7565 //For every event read in this cycle increase the zoom amount by 1 event * sensitivity
76- for event in scroll_event. read ( ) {
66+ for event in scroll_event. read ( ) {
7767 zoom_amount += -event. y * zoom_factor
7868 }
7969
8070 //If we have had any mouse scroll events in this update then apply it to the camera projection
81- if zoom_amount != 0.0 {
71+ if zoom_amount != 0.0 {
8272 //extracting the type of projection from camera, only ever going to be Orthographic since 2d but I don't know a better way lol
83- for mut projection in camera. iter_mut ( ) {
73+ for mut projection in camera. iter_mut ( ) {
8474 projection. scale += zoom_amount;
8575
86- //This statement makes sure that the amount of zoom is capped, can change if need be
87- projection. scale = projection. scale . clamp ( 0.1 , 5.0 ) ;
76+ //This statement makes sure that the amount of zoom is capped, can change if need be
77+ projection. scale = projection. scale . clamp ( 0.1 , 5.0 ) ;
8878 }
8979 }
9080}
0 commit comments