Add a camera system #69

Merged
CoCo_Sol merged 15 commits from camera into main 2024-03-02 11:18:13 +00:00
2 changed files with 43 additions and 104 deletions
Showing only changes of commit 3b5126c309 - Show all commits

74
Cargo.lock generated
View file

@ -1317,8 +1317,6 @@ dependencies = [
"bevy",
"bevy_egui",
"noise",
"num",
"partial-min-max",
"paste",
]
@ -3019,40 +3017,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.3.3"
@ -3064,38 +3028,6 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d869c01cc0c455284163fd0092f1f93835385ccab5a98a0dcc497b2f8bf055a9"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"
@ -3353,12 +3285,6 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "partial-min-max"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6448add382c60bbbc64f9dab41309a12ec530c05191601042f911356ac09758c"
[[package]]
name = "paste"
version = "1.0.14"

View file

@ -4,62 +4,75 @@ use bevy::input::mouse::MouseScrollUnit;
use bevy::prelude::*;
use bevy::input::mouse::MouseWheel;
/// TODO
const CAMERRA_SPEED: f32 = 10.;
const CAMERRA_SPEED_ZOOM: f32 = 0.1;
const SMOOTHING: f32 = 0.5;
const MIN_ZOOM: f32 = 0.5;
const MAX_ZOOM: f32 = 5.;
use crate::CurrentScene;
/// TODO
/// The camera speed movement.
const CAMERRA_SPEED_MOVEMENT: f32 = 10.;
/// The camera smoothing during the movement.
const SMOOTHING: f32 = 0.5;
/// The camera speed scale.
const CAMERRA_SPEED_SCALE: f32 = 0.1;
/// The camera min and max scale.
const MIN_SCALE: f32 = 0.5;
const MAX_SCALE: f32 = 5.;
/// A bevy plugin for the camera.
/// It ables you to move the camera with the keyboard, and scale with the mouse.
pub struct CameraPlugin;
impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, init_camera)
.add_systems(Update, handle_movement)
.add_systems(Update, zoom_system);
.add_systems(Update, movement_system.run_if(in_state(CurrentScene::Game)))
.add_systems(Update, scale_system.run_if(in_state(CurrentScene::Game)));
}
}
/// TODO
/// The fonction that initialize the camera.
fn init_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn handle_movement(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>,){
for mut camera_transform in query.iter_mut() {
let mut target = camera_transform.translation.clone();
if keys.pressed(KeyCode::Z) {
target.y += CAMERRA_SPEED;
}
else if keys.pressed(KeyCode::S) {
target.y -= CAMERRA_SPEED;
/// The fonction that move the camera with the keyboard.
fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>,){
for mut transform in query.iter_mut() {
let mut target = transform.translation;
for key in keys.get_pressed() {
match key {
KeyCode::Z => target.y += CAMERRA_SPEED_MOVEMENT,
KeyCode::S => target.y -= CAMERRA_SPEED_MOVEMENT,
KeyCode::D => target.x += CAMERRA_SPEED_MOVEMENT,
KeyCode::Q => target.x -= CAMERRA_SPEED_MOVEMENT,
_ => return
}
}
if keys.pressed(KeyCode::D) {
target.x += CAMERRA_SPEED;
}
else if keys.pressed(KeyCode::Q) {
target.x -= CAMERRA_SPEED;
}
camera_transform.translation = camera_transform
// Apply the lerp for smoothing the movement of the camera.
transform.translation = transform
.translation
.lerp(target, SMOOTHING);
}
}
fn zoom_system(mut scroll_evr: EventReader<MouseWheel>, mut query: Query<&mut OrthographicProjection, With<Camera>>){
/// The fonction that scale the camera with the mouse.
fn scale_system(mut scroll_evr: EventReader<MouseWheel>, mut query: Query<&mut OrthographicProjection, With<Camera>>){
for ev in scroll_evr.read() {
for mut camera_projection in query.iter_mut() {
for mut projection in query.iter_mut() {
if ev.unit == MouseScrollUnit::Pixel {
if ev.unit != MouseScrollUnit::Line {
return;
}
if camera_projection.scale + ev.y * CAMERRA_SPEED_ZOOM > MIN_ZOOM && camera_projection.scale + ev.y * CAMERRA_SPEED_ZOOM < MAX_ZOOM {
let future_scale = projection.scale + ev.y * CAMERRA_SPEED_SCALE;
if MIN_SCALE < future_scale && future_scale < MAX_SCALE {
camera_projection.scale += ev.y * CAMERRA_SPEED_ZOOM;
projection.scale = future_scale;
}
}
}