use crate::{SpringValue, simple_spring_damper_exact_quat, simple_spring_damper_exact_vec};
use bevy_app::{AnimationSystems, App, Plugin, PostUpdate};
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use bevy_time::{Time, Virtual};
use bevy_transform::{TransformSystems, components::Transform};
use glam::{Quat, Vec2, Vec3, Vec3A, Vec4};
#[derive(Resource, Reflect, Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpringClock {
#[default]
Virtual,
Manual,
}
#[derive(Resource, Reflect, Default, Debug, Clone, Copy)]
pub struct SpringDelta(pub f32);
#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpringSystems;
#[derive(Component, Reflect, Debug, Clone, Copy)]
pub struct TranslationSpring {
pub goal: Vec3,
pub velocity: Vec3,
pub halflife: f32,
}
#[derive(Component, Reflect, Debug, Clone, Copy)]
pub struct RotationSpring {
pub goal: Quat,
pub angular_velocity: Vec3,
pub halflife: f32,
}
#[derive(Component, Reflect, Debug, Clone, Copy)]
pub struct ScaleSpring {
pub goal: Vec3,
pub velocity: Vec3,
pub halflife: f32,
}
#[derive(Component, Reflect, Debug, Clone, Copy)]
pub struct Spring<T: SpringValue + Send + Sync + 'static> {
pub value: T,
pub velocity: T,
pub goal: T,
pub halflife: f32,
}
pub struct SpringPlugin;
impl Plugin for SpringPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<SpringClock>()
.init_resource::<SpringDelta>()
.register_type::<SpringClock>()
.register_type::<SpringDelta>()
.register_type::<TranslationSpring>()
.register_type::<RotationSpring>()
.register_type::<ScaleSpring>()
.register_type::<Spring<f32>>()
.register_type::<Spring<Vec2>>()
.register_type::<Spring<Vec3>>()
.register_type::<Spring<Vec3A>>()
.register_type::<Spring<Vec4>>()
.configure_sets(
PostUpdate,
SpringSystems
.after(AnimationSystems)
.before(TransformSystems::Propagate),
)
.add_systems(
PostUpdate,
(
update_transforms,
update_value::<f32>,
update_value::<Vec2>,
update_value::<Vec3>,
update_value::<Vec3A>,
update_value::<Vec4>,
)
.in_set(SpringSystems),
);
}
}
pub fn add_spring_type<T: SpringValue + Send + Sync + 'static>(app: &mut App) {
app.add_systems(PostUpdate, update_value::<T>.in_set(SpringSystems));
}
fn delta(clock: &SpringClock, manual: &SpringDelta, time: Option<&Time<Virtual>>) -> f32 {
let dt = match clock {
SpringClock::Manual => manual.0,
SpringClock::Virtual => time.map_or(0.0, Time::delta_secs),
};
if dt.is_finite() { dt.max(0.0) } else { 0.0 }
}
type TransformSprings<'w, 's> = Query<
'w,
's,
(
&'static mut Transform,
Option<&'static mut TranslationSpring>,
Option<&'static mut RotationSpring>,
Option<&'static mut ScaleSpring>,
),
Or<(
With<TranslationSpring>,
With<RotationSpring>,
With<ScaleSpring>,
)>,
>;
fn update_transforms(
clock: Res<SpringClock>,
manual: Res<SpringDelta>,
time: Option<Res<Time<Virtual>>>,
mut query: TransformSprings,
) {
let dt = delta(&clock, &manual, time.as_deref());
if dt <= 0.0 {
return;
}
for (mut transform, translation, rotation, scale) in &mut query {
if let Some(mut s) = translation {
let goal = s.goal;
let h = s.halflife;
simple_spring_damper_exact_vec(
&mut transform.translation,
&mut s.velocity,
goal,
h,
dt,
);
}
if let Some(mut s) = rotation {
let goal = s.goal;
let h = s.halflife;
simple_spring_damper_exact_quat(
&mut transform.rotation,
&mut s.angular_velocity,
goal,
h,
dt,
);
}
if let Some(mut s) = scale {
let goal = s.goal;
let h = s.halflife;
simple_spring_damper_exact_vec(&mut transform.scale, &mut s.velocity, goal, h, dt);
}
}
}
fn update_value<T: SpringValue + Send + Sync + 'static>(
clock: Res<SpringClock>,
manual: Res<SpringDelta>,
time: Option<Res<Time<Virtual>>>,
mut query: Query<&mut Spring<T>>,
) {
let dt = delta(&clock, &manual, time.as_deref());
if dt <= 0.0 {
return;
}
for mut spring in &mut query {
let s = &mut *spring;
simple_spring_damper_exact_vec(&mut s.value, &mut s.velocity, s.goal, s.halflife, dt);
}
}