generated from tipragot/rust
Add a camera system #69
|
@ -1,23 +1,23 @@
|
|||
//! The file that contains all camera's systems for movement and scale.
|
||||
//! This module contains the camera systems responsible for movement and scaling.
|
||||
|
||||
use bevy::input::mouse::{MouseScrollUnit, MouseWheel};
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::CurrentScene;
|
||||
|
||||
/// The speed of the camera movement.
|
||||
const CAMERRA_SPEED_MOVEMENT: f32 = 10.;
|
||||
/// The speed of camera movement.
|
||||
const CAMERA_SPEED_MOVEMENT: f32 = 10.;
|
||||
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
|
||||
/// The speed of the camera scaling.
|
||||
const CAMERRA_SPEED_SCALE: f32 = 0.1;
|
||||
/// The speed of camera scaling.
|
||||
const CAMERA_SPEED_SCALE: f32 = 0.1;
|
||||
|
||||
/// The min of the camera scale.
|
||||
/// The minimum scale of the camera.
|
||||
const MIN_SCALE: f32 = 0.5;
|
||||
|
||||
/// The max of the camera scale.
|
||||
/// The maximum scale of the camera.
|
||||
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.
|
||||
/// Key settings for camera movement.
|
||||
#[derive(Resource)]
|
||||
pub struct KeysMovementSettings {
|
||||
/// Key to move the camera up.
|
||||
|
@ -33,8 +33,8 @@ pub struct KeysMovementSettings {
|
|||
pub left: KeyCode,
|
||||
}
|
||||
|
||||
/// A bevy plugin for the camera.
|
||||
/// It ables you to move the camera with the keyboard, and scale with the mouse.
|
||||
/// A Bevy plugin for the camera.
|
||||
/// Allows camera movement with the keyboard and scaling with the mouse.
|
||||
pub struct CameraPlugin;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
|
@ -46,12 +46,12 @@ impl Plugin for CameraPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
/// The fonction that initialize the camera.
|
||||
/// Initializes the camera.
|
||||
fn init_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
}
|
||||
|
||||
/// The fonction that initialize the key movement settings.
|
||||
/// Initializes the key movement settings for the camera.
|
||||
fn init_key_movement(mut commands: Commands) {
|
||||
commands.insert_resource(KeysMovementSettings {
|
||||
up: KeyCode::Z,
|
||||
|
@ -61,7 +61,7 @@ fn init_key_movement(mut commands: Commands) {
|
|||
});
|
||||
}
|
||||
|
||||
/// The fonction that move the camera with the keyboard.
|
||||
/// Moves the camera with keyboard input.
|
||||
fn movement_system(
|
||||
mut query: Query<&mut Transform, With<Camera>>,
|
||||
keys: Res<Input<KeyCode>>,
|
||||
|
@ -71,10 +71,10 @@ fn movement_system(
|
|||
let mut target = Vec3::ZERO;
|
||||
for key in keys.get_pressed() {
|
||||
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,
|
||||
v if v == keys_settings.up => target.y += CAMERA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.down => target.y -= CAMERA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.right => target.x += CAMERA_SPEED_MOVEMENT,
|
||||
v if v == keys_settings.left => target.x -= CAMERA_SPEED_MOVEMENT,
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
@ -83,18 +83,18 @@ fn movement_system(
|
|||
}
|
||||
}
|
||||
|
||||
/// The fonction that scale the camera with the mouse.
|
||||
/// Scales the camera with mouse input.
|
||||
fn scale_system(
|
||||
mut scroll_evr: EventReader<MouseWheel>,
|
||||
mut scroll_ev: EventReader<MouseWheel>,
|
||||
mut query: Query<&mut OrthographicProjection, With<Camera>>,
|
||||
) {
|
||||
for ev in scroll_evr.read() {
|
||||
for ev in scroll_ev.read() {
|
||||
for mut projection in query.iter_mut() {
|
||||
if ev.unit != MouseScrollUnit::Line {
|
||||
return;
|
||||
}
|
||||
|
||||
let future_scale = ev.y.mul_add(CAMERRA_SPEED_SCALE, projection.scale);
|
||||
let future_scale = ev.y.mul_add(CAMERA_SPEED_SCALE, projection.scale);
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
The view not the camera The view not the camera
|
||||
if MIN_SCALE < future_scale && future_scale < MAX_SCALE {
|
||||
projection.scale = future_scale;
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Don't use abreviations: https://youtu.be/-J3wNP6u5YU Don't use abreviations: https://youtu.be/-J3wNP6u5YU
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue
This should be a resource and the others too