Add a camera system #69

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

View file

@ -19,7 +19,7 @@ const MAX_SCALE: f32 = 5.;
/// The keys movement of the camera. /// The keys movement of the camera.
#[derive(Resource)] #[derive(Resource)]
pub struct KeysMovementSettings{ pub struct KeysMovementSettings {
/// Key to move the camera up. /// Key to move the camera up.
pub up: KeyCode, pub up: KeyCode,
@ -40,9 +40,9 @@ 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(Startup, init_key_movement) .add_systems(Startup, init_key_movement)
.add_systems(Update, movement_system.run_if(in_state(CurrentScene::Game))) .add_systems(Update, movement_system.run_if(in_state(CurrentScene::Game)))
.add_systems(Update, scale_system.run_if(in_state(CurrentScene::Game))); .add_systems(Update, scale_system.run_if(in_state(CurrentScene::Game)));
} }
} }
@ -52,8 +52,8 @@ fn init_camera(mut commands: Commands) {
} }
/// The fonction that initialize the key movement settings. /// The fonction that initialize the key movement settings.
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,
down: KeyCode::S, down: KeyCode::S,
right: KeyCode::D, right: KeyCode::D,
@ -62,9 +62,12 @@ fn init_key_movement(mut commands: Commands){
} }
/// The fonction that move the camera with the keyboard. /// The fonction that move the camera with the keyboard.
fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Input<KeyCode>>, keys_settings: Res<KeysMovementSettings>) { 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() { for mut transform in query.iter_mut() {
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 {
@ -74,28 +77,27 @@ fn movement_system(mut query: Query<&mut Transform, With<Camera>>, keys: Res<Inp
v if v == keys_settings.left => target.x -= CAMERRA_SPEED_MOVEMENT, v if v == keys_settings.left => target.x -= CAMERRA_SPEED_MOVEMENT,
_ => continue, _ => continue,
} }
} }
transform.translation += target; transform.translation += target;
} }
} }
/// The fonction that scale the camera with the mouse. /// The fonction that scale the camera with the mouse.
fn scale_system(mut scroll_evr: EventReader<MouseWheel>, mut query: Query<&mut OrthographicProjection, With<Camera>>){ 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 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(CAMERRA_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;
} }
} }
} }
} }

View file

@ -2,9 +2,9 @@
use bevy::prelude::*; use bevy::prelude::*;
pub mod camera;
pub mod map; pub mod map;
pub mod scenes; pub mod scenes;
pub mod camera;
/// The current scene of the game. /// The current scene of the game.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]

View file

@ -1,7 +1,8 @@
//! The main entry point of the game. //! The main entry point of the game.
use bevy::prelude::*; use bevy::prelude::*;
use border_wars::{camera::CameraPlugin, scenes::ScenesPlugin}; use border_wars::camera::CameraPlugin;
use border_wars::scenes::ScenesPlugin;
fn main() { fn main() {
App::new() App::new()
@ -13,8 +14,8 @@ fn main() {
} }
fn init_shap(mut commands: Commands, assets_server: Res<AssetServer>) { fn init_shap(mut commands: Commands, assets_server: Res<AssetServer>) {
commands.spawn(SpriteBundle{ commands.spawn(SpriteBundle {
texture: assets_server.load("caca.png"), texture: assets_server.load("caca.png"),
..Default::default() ..Default::default()
}); });
} }