generated from tipragot/rust
Add a camera system #69
74
Cargo.lock
generated
74
Cargo.lock
generated
|
@ -1317,8 +1317,6 @@ dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy_egui",
|
"bevy_egui",
|
||||||
"noise",
|
"noise",
|
||||||
"num",
|
|
||||||
"partial-min-max",
|
|
||||||
"paste",
|
"paste",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -3019,40 +3017,6 @@ dependencies = [
|
||||||
"winapi",
|
"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]]
|
[[package]]
|
||||||
name = "num-derive"
|
name = "num-derive"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
@ -3064,38 +3028,6 @@ dependencies = [
|
||||||
"syn 1.0.109",
|
"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]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.18"
|
version = "0.2.18"
|
||||||
|
@ -3353,12 +3285,6 @@ dependencies = [
|
||||||
"windows-targets 0.48.5",
|
"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]]
|
[[package]]
|
||||||
name = "paste"
|
name = "paste"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
|
|
@ -4,62 +4,75 @@ use bevy::input::mouse::MouseScrollUnit;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::input::mouse::MouseWheel;
|
use bevy::input::mouse::MouseWheel;
|
||||||
|
|
||||||
/// TODO
|
use crate::CurrentScene;
|
||||||
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.;
|
|
||||||
|
|
||||||
/// TODO
|
/// The camera speed movement.
|
||||||
|
const CAMERRA_SPEED_MOVEMENT: f32 = 10.;
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
|
|
||||||
|
/// 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;
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
No abreviations No abreviations
|
|||||||
|
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;
|
pub struct CameraPlugin;
|
||||||
|
|
||||||
impl Plugin for CameraPlugin {
|
impl Plugin for CameraPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Startup, init_camera)
|
app.add_systems(Startup, init_camera)
|
||||||
.add_systems(Update, handle_movement)
|
.add_systems(Update, movement_system.run_if(in_state(CurrentScene::Game)))
|
||||||
.add_systems(Update, zoom_system);
|
.add_systems(Update, scale_system.run_if(in_state(CurrentScene::Game)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO
|
/// The fonction that initialize the camera.
|
||||||
fn init_camera(mut commands: Commands) {
|
fn init_camera(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_movement(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>,){
|
/// The fonction that move the camera with the keyboard.
|
||||||
for mut camera_transform in query.iter_mut() {
|
fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>,){
|
||||||
let mut target = camera_transform.translation.clone();
|
for mut transform in query.iter_mut() {
|
||||||
if keys.pressed(KeyCode::Z) {
|
|
||||||
target.y += CAMERRA_SPEED;
|
let mut target = transform.translation;
|
||||||
}
|
|
||||||
else if keys.pressed(KeyCode::S) {
|
for key in keys.get_pressed() {
|
||||||
target.y -= CAMERRA_SPEED;
|
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;
|
// Apply the lerp for smoothing the movement of the camera.
|
||||||
}
|
transform.translation = transform
|
||||||
camera_transform.translation = camera_transform
|
|
||||||
.translation
|
.translation
|
||||||
.lerp(target, SMOOTHING);
|
.lerp(target, SMOOTHING);
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Not only... Not only...
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue
This should be a resource and the others too