From 36771801bc88023900fedf4805a09cdbd9abf5bb Mon Sep 17 00:00:00 2001 From: Fabien JUIF Date: Wed, 29 Nov 2023 15:28:28 +0100 Subject: [PATCH] :heavy_plus_sign: bevy_cameraman --- Cargo.lock | 6 +- Cargo.toml | 2 +- camera/Cargo.toml | 7 -- camera/src/lib.rs | 246 ---------------------------------------------- src/main.rs | 2 +- src/player.rs | 4 +- 6 files changed, 8 insertions(+), 259 deletions(-) delete mode 100644 camera/Cargo.toml delete mode 100644 camera/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ff3ec6c..d77283a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,8 +449,10 @@ dependencies = [ ] [[package]] -name = "bevy_camera" +name = "bevy_cameraman" version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8e2e577fc7831b1b3b637cae39b14ef73289977c1654dacd9fb9ad9d08173b" dependencies = [ "bevy", ] @@ -1776,7 +1778,7 @@ name = "game" version = "0.1.0" dependencies = [ "bevy", - "bevy_camera", + "bevy_cameraman", "bevy_rapier2d", "blake3", "rand", diff --git a/Cargo.toml b/Cargo.toml index f5c7c51..725fd3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,8 @@ edition = "2021" bevy = "0.12.0" bevy_rapier2d = "0.23.0" rand = "0.8.5" -bevy_camera = { path = "camera" } blake3 = { version = "1.5", features=["pure"] } +bevy_cameraman = "0.1.0" # Enable a small amount of optimization in debug mode [profile.dev] diff --git a/camera/Cargo.toml b/camera/Cargo.toml deleted file mode 100644 index 197bd04..0000000 --- a/camera/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "bevy_camera" -version = "0.1.0" -edition = "2021" - -[dependencies] -bevy = "0.12.0" \ No newline at end of file diff --git a/camera/src/lib.rs b/camera/src/lib.rs deleted file mode 100644 index 97f1198..0000000 --- a/camera/src/lib.rs +++ /dev/null @@ -1,246 +0,0 @@ -use bevy::{ - app::{PostStartup, Update}, - asset::Assets, - ecs::{ - bundle::Bundle, - component::Component, - system::{Commands, Res, ResMut}, - }, - gizmos::{gizmos::Gizmos, GizmoConfig}, - math::{Vec2, Vec3}, - prelude::{ - default, App, Camera2dBundle, Entity, Plugin, PostUpdate, Query, Time, Timer, TimerMode, - Transform, With, Without, - }, - render::{ - color::Color, - mesh::{shape, Mesh}, - }, - sprite::{ColorMaterial, MaterialMesh2dBundle}, -}; - -#[derive(Component)] -pub struct Target; - -#[derive(Component)] -pub struct Camera { - target: Entity, // TODO: find a way to have multiple targets per camera, but also being able to have multi cameras (n-n) - dead_zone: Vec2, - target_prev_translation: Vec3, - // look at this position, this is the player + velocity + factor - // it allow us to place the camera a bit ahead of time - look_at: Vec3, - ahead_factor: Vec3, - traveling: bool, - center_after: Timer, -} - -impl Camera { - pub fn new(target: Entity, dead_zone: Vec2, ahead_factor: Vec3) -> Self { - Self { - target, - dead_zone, - target_prev_translation: Vec3::ZERO, - look_at: Vec3::ZERO, - ahead_factor, - traveling: false, - center_after: Timer::from_seconds(0.4, TimerMode::Once), - } - } - - pub fn new_default(target: Entity) -> Self { - Self { - target, - dead_zone: Vec2::new(30.0, 15.0), - target_prev_translation: Vec3::ZERO, - look_at: Vec3::ZERO, - ahead_factor: Vec3::ONE, - traveling: false, - center_after: Timer::from_seconds(0.4, TimerMode::Once), - } - } -} - -#[derive(Bundle)] -pub struct CameraBundle { - camera: Camera, - bundle: Camera2dBundle, -} - -impl CameraBundle { - pub fn new(camera: Camera, bundle: Camera2dBundle) -> Self { - Self { camera, bundle } - } - - pub fn new_with_default_bundle(target: Entity) -> Self { - Self { - camera: Camera::new_default(target), - bundle: Camera2dBundle::default(), - } - } -} - -pub struct CameraPlugin; - -impl Plugin for CameraPlugin { - fn build(&self, app: &mut App) { - app.add_systems(PostStartup, center) - .add_systems(PostUpdate, cameraman); - } -} - -fn center( - mut query_camera: Query<(&mut Transform, &mut Camera), Without>, - query_targets: Query<(&Transform, Entity), With>, -) { - for (mut camera_transform, mut camera) in &mut query_camera { - for (target_transform, target_entity) in &query_targets { - if camera.target != target_entity { - continue; - } - - // TODO: for now we follow the first target but we could think of doing an average positions of all the targets - if camera.target == target_entity { - camera_transform.translation.x = target_transform.translation.x; - camera_transform.translation.y = target_transform.translation.y; - camera.target_prev_translation = target_transform.translation; - camera.look_at = target_transform.translation; - break; - } - } - } -} - -fn cameraman( - mut query_camera: Query<(&mut Transform, &mut Camera), Without>, - query_targets: Query<(&Transform, Entity), With>, - time: Res