added tweening engine

This commit is contained in:
Nickiel12 2024-07-21 23:27:52 +00:00
parent 10719fbcd4
commit 22cd544e72

View file

@ -19,11 +19,11 @@ impl Timer {
/// Returns true if the time_start + timer_len > Instant::now
/// or if the timer is stopped
pub fn is_complete(&mut self) -> bool {
pub fn is_complete(&self) -> bool {
if let Some(start) = self.timer_start {
start + self.timer_len > Instant::now()
} else {
false
true
}
}
@ -39,4 +39,64 @@ impl Timer {
}
}
}
// Get the percentage complete. Returns 100.0 if the timer is complete or
// hasn't been started
pub fn percentage_complete(&self) -> f32 {
if self.is_complete() || self.timer_start.is_none() {
100.0
} else {
if let Some(start) = self.timer_start {
(start.elapsed().as_secs_f32() / self.timer_len.as_secs_f32()) * 100.0
} else {
100.0
}
}
}
}
pub struct TweeningEngine {
timer: Timer,
start_value: f32,
end_value: f32,
}
impl TweeningEngine {
pub fn new(start_value: f32, end_value: f32) -> Self {
TweeningEngine {
timer: Timer::default(),
start_value,
end_value,
}
}
pub fn starting(mut self, start_value: f32) -> Self {
self.start_value = start_value;
self
}
pub fn ending(mut self, end_value: f32) -> Self {
self.end_value = end_value;
self
}
pub fn duration(mut self, dur: Duration) -> Self {
self.timer.set_len(dur);
self
}
pub fn chain_start(mut self) -> Self {
self.timer.start();
self
}
pub fn start(&mut self) {
self.timer.start();
}
pub fn is_complete(&self) -> bool {
self.timer.is_complete()
}
pub fn get_value(&self) -> f32 {
self.start_value + (self.end_value - self.start_value) * self.timer.percentage_complete()
}
}