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::input::mouse::{MouseScrollUnit, MouseWheel};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::CurrentScene;
|
use crate::CurrentScene;
|
||||||
|
|
||||||
/// The speed of the camera movement.
|
/// The speed of camera movement.
|
||||||
const CAMERRA_SPEED_MOVEMENT: f32 = 10.;
|
const CAMERA_SPEED_MOVEMENT: f32 = 10.;
|
||||||
|
|
||||||
/// The speed of the camera scaling.
|
/// The speed of camera scaling.
|
||||||
const CAMERRA_SPEED_SCALE: f32 = 0.1;
|
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;
|
const MIN_SCALE: f32 = 0.5;
|
||||||
|
|
||||||
/// The max of the camera scale.
|
/// The maximum scale of the camera.
|
||||||
const MAX_SCALE: f32 = 5.;
|
const MAX_SCALE: f32 = 5.;
|
||||||
|
|
||||||
/// The keys movement of the camera.
|
/// Key settings for camera movement.
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct KeysMovementSettings {
|
pub struct KeysMovementSettings {
|
||||||
/// Key to move the camera up.
|
/// Key to move the camera up.
|
||||||
|
@ -33,8 +33,8 @@ pub struct KeysMovementSettings {
|
||||||
pub left: KeyCode,
|
pub left: KeyCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A bevy plugin for the camera.
|
/// A Bevy plugin for the camera.
|
||||||
/// It ables you to move the camera with the keyboard, and scale with the mouse.
|
/// Allows camera movement with the keyboard and scaling with the mouse.
|
||||||
pub struct CameraPlugin;
|
pub struct CameraPlugin;
|
||||||
|
|
||||||
impl Plugin for 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) {
|
fn init_camera(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
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) {
|
fn init_key_movement(mut commands: Commands) {
|
||||||
commands.insert_resource(KeysMovementSettings {
|
commands.insert_resource(KeysMovementSettings {
|
||||||
up: KeyCode::Z,
|
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(
|
fn movement_system(
|
||||||
mut query: Query<&mut Transform, With<Camera>>,
|
mut query: Query<&mut Transform, With<Camera>>,
|
||||||
keys: Res<Input<KeyCode>>,
|
keys: Res<Input<KeyCode>>,
|
||||||
|
@ -71,10 +71,10 @@ fn movement_system(
|
||||||
let mut target = Vec3::ZERO;
|
let mut target = Vec3::ZERO;
|
||||||
for key in keys.get_pressed() {
|
for key in keys.get_pressed() {
|
||||||
match *key {
|
match *key {
|
||||||
v if v == keys_settings.up => target.y += CAMERRA_SPEED_MOVEMENT,
|
v if v == keys_settings.up => target.y += CAMERA_SPEED_MOVEMENT,
|
||||||
v if v == keys_settings.down => target.y -= CAMERRA_SPEED_MOVEMENT,
|
v if v == keys_settings.down => target.y -= CAMERA_SPEED_MOVEMENT,
|
||||||
v if v == keys_settings.right => target.x += CAMERRA_SPEED_MOVEMENT,
|
v if v == keys_settings.right => target.x += CAMERA_SPEED_MOVEMENT,
|
||||||
v if v == keys_settings.left => target.x -= CAMERRA_SPEED_MOVEMENT,
|
v if v == keys_settings.left => target.x -= CAMERA_SPEED_MOVEMENT,
|
||||||
_ => continue,
|
_ => 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(
|
fn scale_system(
|
||||||
mut scroll_evr: EventReader<MouseWheel>,
|
mut scroll_ev: EventReader<MouseWheel>,
|
||||||
mut query: Query<&mut OrthographicProjection, With<Camera>>,
|
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() {
|
for mut projection in query.iter_mut() {
|
||||||
if ev.unit != MouseScrollUnit::Line {
|
if ev.unit != MouseScrollUnit::Line {
|
||||||
return;
|
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);
|
||||||
if MIN_SCALE < future_scale && future_scale < MAX_SCALE {
|
if MIN_SCALE < future_scale && future_scale < MAX_SCALE {
|
||||||
projection.scale = future_scale;
|
projection.scale = future_scale;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue