generated from tipragot/rust
Add a camera system #69
|
@ -1,24 +1,38 @@
|
|||
//! TODO
|
||||
//! The file that contains all camera's systems for movement and scale.
|
||||
|
||||
use bevy::input::mouse::MouseScrollUnit;
|
||||
use bevy::input::mouse::{MouseScrollUnit, MouseWheel};
|
||||
use bevy::prelude::*;
|
||||
use bevy::input::mouse::MouseWheel;
|
||||
|
||||
use crate::CurrentScene;
|
||||
|
||||
/// The camera speed movement.
|
||||
/// The speed of the camera 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.
|
||||
/// The speed of the camera scaling.
|
||||
const CAMERRA_SPEED_SCALE: f32 = 0.1;
|
||||
|
||||
/// The camera min and max scale.
|
||||
/// The min of the camera scale.
|
||||
const MIN_SCALE: f32 = 0.5;
|
||||
|
||||
/// The max of the camera scale.
|
||||
const MAX_SCALE: f32 = 5.;
|
||||
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
No abreviations No abreviations
|
||||
/// The keys movement of the camera.
|
||||
#[derive(Resource)]
|
||||
pub struct KeysMovementSettings{
|
||||
/// Key to move the camera up.
|
||||
pub up: KeyCode,
|
||||
|
||||
/// Key to move the camera down.
|
||||
pub down: KeyCode,
|
||||
|
||||
/// Key to move the camera right.
|
||||
pub right: KeyCode,
|
||||
|
||||
/// Key to move the camera left.
|
||||
pub left: KeyCode,
|
||||
}
|
||||
|
||||
/// A bevy plugin for the camera.
|
||||
/// It ables you to move the camera with the keyboard, and scale with the mouse.
|
||||
pub struct CameraPlugin;
|
||||
|
@ -26,6 +40,7 @@ pub struct CameraPlugin;
|
|||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, init_camera)
|
||||
.add_systems(Startup, init_key_movement)
|
||||
.add_systems(Update, movement_system.run_if(in_state(CurrentScene::Game)))
|
||||
.add_systems(Update, scale_system.run_if(in_state(CurrentScene::Game)));
|
||||
}
|
||||
|
@ -36,27 +51,33 @@ fn init_camera(mut commands: Commands) {
|
|||
commands.spawn(Camera2dBundle::default());
|
||||
}
|
||||
|
||||
/// The fonction that initialize the key movement settings.
|
||||
fn init_key_movement(mut commands: Commands){
|
||||
commands.insert_resource(KeysMovementSettings{
|
||||
up: KeyCode::Z,
|
||||
down: KeyCode::S,
|
||||
right: KeyCode::D,
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Not only... Not only...
|
||||
left: KeyCode::Q,
|
||||
});
|
||||
}
|
||||
|
||||
/// The fonction that move the camera with the keyboard.
|
||||
fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>,){
|
||||
fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>, keys_settings: Res<KeysMovementSettings>) {
|
||||
for mut transform in query.iter_mut() {
|
||||
|
||||
let mut target = transform.translation;
|
||||
|
||||
let mut target = Vec3::ZERO;
|
||||
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
|
||||
match *key {
|
||||
v if v == keys_settings.up => target.y += CAMERRA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.down => target.y -= CAMERRA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.right => target.x += CAMERRA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.left => target.x -= CAMERRA_SPEED_MOVEMENT,
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Apply the lerp for smoothing the movement of the camera.
|
||||
transform.translation = transform
|
||||
.translation
|
||||
.lerp(target, SMOOTHING);
|
||||
transform.translation += target;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,7 +90,7 @@ fn scale_system(mut scroll_evr: EventReader<MouseWheel>, mut query: Query<&mut O
|
|||
return;
|
||||
}
|
||||
|
||||
let future_scale = projection.scale + ev.y * CAMERRA_SPEED_SCALE;
|
||||
let future_scale = ev.y.mul_add(CAMERRA_SPEED_SCALE, projection.scale);
|
||||
if MIN_SCALE < future_scale && future_scale < MAX_SCALE {
|
||||
|
||||
projection.scale = future_scale;
|
||||
|
|
|
@ -14,7 +14,7 @@ fn main() {
|
|||
|
||||
fn init_shap(mut commands: Commands, assets_server: Res<AssetServer>) {
|
||||
commands.spawn(SpriteBundle{
|
||||
texture: assets_server.load("caca.png").into(),
|
||||
texture: assets_server.load("caca.png"),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue
This should be a resource and the others too