use crate::interpolate::Interpolator;
use bevy::prelude::*;
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct Translation {
#[allow(missing_docs)]
pub start: Vec3,
#[allow(missing_docs)]
pub end: Vec3,
pub delta: bool
}
impl Interpolator for Translation {
type Item = Transform;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
if self.delta{
let previous_translation = self.start.lerp(self.end, previous_value);
let next_translation = self.start.lerp(self.end, value);
let translation_delta = next_translation - previous_translation;
item.translation += translation_delta;
}else{
item.translation = self.start.lerp(self.end, value);
}
}
}
pub fn translation(start: Vec3, end: Vec3) -> Translation {
Translation { start, end, delta: false }
}
pub fn translation_to(to: Vec3) -> impl Fn(&mut Vec3) -> Translation {
move |state| {
let start = *state;
let end = to;
*state = to;
translation(start, end)
}
}
pub fn translation_by(by: Vec3) -> impl Fn(&mut Vec3) -> Translation {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
translation(start, end)
}
}
pub fn translation_delta_by(by: Vec3) -> impl Fn(&mut Vec3) -> Translation {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
Translation { start, end, delta: true }
}
}
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct Rotation {
#[allow(missing_docs)]
pub start: Quat,
#[allow(missing_docs)]
pub end: Quat,
pub delta: bool
}
impl Interpolator for Rotation {
type Item = Transform;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
if self.delta{
let previous_rotation = self.start.slerp(self.end, previous_value);
let next_rotation = self.start.slerp(self.end, value);
let rotation_delta = next_rotation - previous_rotation;
item.rotation = item.rotation.mul_quat(rotation_delta);
}else{
item.rotation = self.start.slerp(self.end, value);
}
}
}
pub fn rotation(start: Quat, end: Quat) -> Rotation {
Rotation { start, end, delta: false }
}
pub fn rotation_to(to: Quat) -> impl Fn(&mut Quat) -> Rotation {
move |state| {
let start = *state;
let end = to;
*state = to;
rotation(start, end)
}
}
pub fn rotation_by(by: Quat) -> impl Fn(&mut Quat) -> Rotation {
move |state| {
let start = *state;
let end = *state + by;
*state = state.mul_quat(by);
rotation(start, end)
}
}
pub fn rotation_delta_by(by: Quat) -> impl Fn(&mut Quat) -> Rotation {
move |state| {
let start = *state;
let end = *state + by;
*state = state.mul_quat(by);
Rotation { start, end, delta: true }
}
}
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct Scale {
#[allow(missing_docs)]
pub start: Vec3,
#[allow(missing_docs)]
pub end: Vec3,
pub delta: bool
}
impl Interpolator for Scale {
type Item = Transform;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
if self.delta{
let previous_scale = self.start.lerp(self.end, previous_value);
let next_scale = self.start.lerp(self.end, value);
let scale_delta = next_scale - previous_scale;
item.scale += scale_delta;
}else{
item.scale = self.start.lerp(self.end, value);
}
}
}
pub fn scale(start: Vec3, end: Vec3) -> Scale {
Scale { start, end, delta: false }
}
pub fn scale_to(to: Vec3) -> impl Fn(&mut Vec3) -> Scale {
move |state| {
let start = *state;
let end = to;
*state = to;
scale(start, end)
}
}
pub fn scale_by(by: Vec3) -> impl Fn(&mut Vec3) -> Scale {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
scale(start, end)
}
}
pub fn scale_delta_by(by: Vec3) -> impl Fn(&mut Vec3) -> Scale {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
Scale { start, end, delta: true }
}
}
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct AngleZ {
#[allow(missing_docs)]
pub start: f32,
#[allow(missing_docs)]
pub end: f32,
pub delta: bool
}
impl Interpolator for AngleZ {
type Item = Transform;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
if self.delta{
let previous_angle = (self.end - self.start).mul_add(previous_value, self.start);
let update_angle = (self.end - self.start).mul_add(value, self.start);
let angle_delta_as_quat = Quat::from_rotation_z(update_angle - previous_angle);
item.rotation = item.rotation.mul_quat(angle_delta_as_quat);
}else{
let angle = (self.end - self.start).mul_add(value, self.start);
item.rotation = Quat::from_rotation_z(angle);
}
}
}
pub fn angle_z(start: f32, end: f32) -> AngleZ {
AngleZ { start, end, delta: false }
}
pub fn angle_z_to(to: f32) -> impl Fn(&mut f32) -> AngleZ {
move |state| {
let start = *state;
let end = to;
*state = to;
angle_z(start, end)
}
}
pub fn angle_z_by(by: f32) -> impl Fn(&mut f32) -> AngleZ {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
angle_z(start, end)
}
}
pub fn angle_z_delta_by(by: f32) -> impl Fn(&mut f32) -> AngleZ {
move |state| {
let start = *state;
let end = *state + by;
*state += by;
AngleZ {start, end, delta: true}
}
}