diff --git a/camera/src/lib.rs b/camera/src/lib.rs index 4fc909d..97f1198 100644 --- a/camera/src/lib.rs +++ b/camera/src/lib.rs @@ -1,13 +1,22 @@ use bevy::{ app::{PostStartup, Update}, - ecs::system::ResMut, + asset::Assets, + ecs::{ + bundle::Bundle, + component::Component, + system::{Commands, Res, ResMut}, + }, gizmos::{gizmos::Gizmos, GizmoConfig}, - math::Vec2, + math::{Vec2, Vec3}, prelude::{ - App, Bundle, Camera2dBundle, Component, Entity, Plugin, PostUpdate, Query, Transform, With, - Without, + default, App, Camera2dBundle, Entity, Plugin, PostUpdate, Query, Time, Timer, TimerMode, + Transform, With, Without, + }, + render::{ + color::Color, + mesh::{shape, Mesh}, }, - render::color::Color, + sprite::{ColorMaterial, MaterialMesh2dBundle}, }; #[derive(Component)] @@ -17,17 +26,37 @@ pub struct Target; 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) -> Self { - Self { target, dead_zone } + 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), } } } @@ -39,11 +68,8 @@ pub struct CameraBundle { } impl CameraBundle { - pub fn new(target: Entity, bundle: Camera2dBundle) -> Self { - Self { - camera: Camera::new_default(target), - bundle, - } + pub fn new(camera: Camera, bundle: Camera2dBundle) -> Self { + Self { camera, bundle } } pub fn new_with_default_bundle(target: Entity) -> Self { @@ -64,10 +90,10 @@ impl Plugin for CameraPlugin { } fn center( - mut query_camera: Query<(&mut Transform, &Camera), Without>, + mut query_camera: Query<(&mut Transform, &mut Camera), Without>, query_targets: Query<(&Transform, Entity), With>, ) { - for (mut camera_transform, camera) in &mut query_camera { + for (mut camera_transform, mut camera) in &mut query_camera { for (target_transform, target_entity) in &query_targets { if camera.target != target_entity { continue; @@ -77,6 +103,8 @@ fn center( 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; } } @@ -84,39 +112,58 @@ fn center( } fn cameraman( - mut query_camera: Query<(&mut Transform, &Camera), Without>, + mut query_camera: Query<(&mut Transform, &mut Camera), Without>, query_targets: Query<(&Transform, Entity), With>, + time: Res