VICUJP7SGKKCO2ZWO5ACSO3OS4HV4JTXTHQHYSFOADANGUJ5ZDDAC
WQS2WD437EWG2CKPOY5TDWPEBE3US7LGHVAH2HVYQ7RW2L6FHJ5AC
EV477LPQ57FEHMFO6E3LL5MFWZC6NTGER42SKP5CJN73SWXGB5AQC
7FTW5AQKOO4W5AIHYZSAKLQR6LGVTXH7SKMSTA4ER4C4HP54VE4QC
ASIORORAPGGVNLR42J6PZRTG7JKK34GYXBYSKSTJFXSFW6YXBJJQC
DBHCCY3W2DG4WYMXBZ2UCNYVGZ5PS5UUUNWCXVYJ4TMXQDIZT6GAC
7SXAXY6J4QHPDUUHCKZDEZOZ2IUMSBWODICOMIZH2KA2BGV3F54QC
KZU3YXA22IYIPZICQTMZ4XILKCWOI5O6W4H72PDXDUTZDFVMVGVQC
5Y32O2B2GTH2UHFA3NO4ZY673XNHT3W4DQADFMK4LMKLSXMZKHGQC
ZUZLL6PWZE57MLTQJ56MT3HGIQU6NEHQRB63VQYJRY5HNVC7NVMAC
YT2GB32PAGZCSNZGKTQH7MCJ675TK2G5OHS7SY4R2FDCNBKQHXYAC
66PNECCRWS2BPZS555P2355EY2PAHZWHJJEXTDU2WUBOHANRNXFQC
5DFOVYVHNSGK7MPEEY3YZXIRXK6UNLVDVUQZ3SXGCXAU7C2CMJPAC
IZ722OQCK6KLKQF6YKJ7EN77RYIW7CLSKL2E53QSLANSAIG7MKIQC
OXRSNQE47DF5CNSZMLP5JUZPN3S5U4S62OLJCMZSALUYC66HHBJAC
//! Handles moving things *and* them colliding
use bevy::prelude::*;
use crate::movement_pointer;
#[derive(Component)]
pub struct Moveable {
pub velocity: Vec2,
}
impl Default for Moveable {
fn default() -> Self {
Self {
velocity: default(),
}
}
}
pub fn update_transform(
mut query: Query<(
&mut Transform,
&Moveable,
Option<&mut movement_pointer::MovementDirection>,
)>,
time: Res<Time>,
) {
for (mut transform, moveable, maybe_pointer) in query.iter_mut() {
// If we have a movement pointer, update it
if let Some(mut pointer) = maybe_pointer {
pointer.0 = moveable.velocity.normalize_or_zero();
pointer.1 = moveable.velocity.length();
}
// Update transform
transform.translation += moveable.velocity.extend(0.0) * time.delta_seconds();
}
}
.add_systems(
FixedUpdate,
(camera::update_camera).run_if(in_state(GameState::InRun)),
)
.add_systems(FixedUpdate, movement::update_transform)
// configure our fixed timestep schedule to run 60 times per second
.insert_resource(Time::<Fixed>::from_seconds(60.0f64.recip()))
#[derive(Resource)]
pub struct CameraRig {
lag: f32,
/// how long should the lerp take?
pub snap_duration: Duration,
}
impl CameraRig {
/// Camera lag - [0.0, 1.0) lower is snappier, higher is smoother
pub fn lag(&self) -> f32 {
self.lag
}
pub fn set_lag(&mut self, lag: f32) {
self.lag = lag.clamp(0.0, 1.0 - f32::EPSILON);
}
}
impl Default for CameraRig {
fn default() -> Self {
Self {
lag: 0.7,
snap_duration: Duration::from_secs_f32(10f32.recip()),
}
}
}
let diff = goal - source;
let lerped = source + diff * 0.1; //* time.delta_seconds();
camera.subpixel_pos = lerped.xy();
let adjusted_lag = rig.lag;
let lerped = source * adjusted_lag + goal * (1.0 - adjusted_lag);
let lerp_delta = lerped - source;
camera.subpixel_pos +=
lerp_delta.xy() * time.delta_seconds() / rig.snap_duration.as_secs_f32();