From 88c46d679d683086fcf3cfd6cdbb4cd93169fb24 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 17 Jan 2025 12:09:55 +0200 Subject: [PATCH] feat(camera): add camera controls #34 --- src/camera.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/constants.rs | 6 +++++ src/lib.rs | 18 +++----------- 3 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/camera.rs diff --git a/src/camera.rs b/src/camera.rs new file mode 100644 index 0000000..2701589 --- /dev/null +++ b/src/camera.rs @@ -0,0 +1,64 @@ +use bevy::{input::mouse::MouseWheel, prelude::*}; + +use crate::constants::{BASE_ZOOM_SPEED, DISTANCE_SCALE_FACTOR, MAX_ZOOM, MIN_ZOOM}; + +pub(super) fn plugin(app: &mut App) { + app.add_systems(Update, camera_zoom); +} + +#[derive(Debug, Reflect, Component)] +#[reflect(Component)] +pub struct MainCamera; + +pub fn spawn_camera(mut commands: Commands) { + commands.spawn(( + Name::new("Camera"), + MainCamera, + Camera3d::default(), + Transform::from_xyz(200., 200., 0.).looking_at(Vec3::ZERO, Vec3::Y), + // Render all UI to this camera. + // Not strictly necessary since we only use one camera, + // but if we don't use this component, our UI will disappear as soon + // as we add another camera. This includes indirect ways of adding cameras like using + // [ui node outlines](https://bevyengine.org/news/bevy-0-14/#ui-node-outline-gizmos) + // for debugging. So it's good to have this here for future-proofing. + IsDefaultUiCamera, + )); +} + +fn camera_zoom( + mut query: Query<&mut Transform, With>, + mut scrool_evr: EventReader, + keyboard: Res>, + time: Res