Added reverse animation

master
Mark 2024-01-04 21:41:15 -08:00
parent 8dc040cf08
commit 001c74ba6a
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
3 changed files with 32 additions and 10 deletions

View File

@ -20,8 +20,8 @@ file = "projectile/blaster.png"
file = "ship/gypsum.png" file = "ship/gypsum.png"
[sprite."ship::peregrine"] [sprite."ship::peregrine"]
duration = 1 duration = 1.3
repeat = "repeat" repeat = "reverse"
frames = [ frames = [
"ship/peregrine/01.png", "ship/peregrine/01.png",
"ship/peregrine/02.png", "ship/peregrine/02.png",

View File

@ -44,6 +44,10 @@ pub enum RepeatMode {
/// After the first frame, jump to the last frame /// After the first frame, jump to the last frame
#[serde(rename = "repeat")] #[serde(rename = "repeat")]
Repeat, Repeat,
/// Play this animation in reverse after the last frame
#[serde(rename = "reverse")]
Reverse,
} }
impl RepeatMode { impl RepeatMode {
@ -51,8 +55,9 @@ impl RepeatMode {
/// Used to pass this enum into shaders /// Used to pass this enum into shaders
pub fn as_int(&self) -> u32 { pub fn as_int(&self) -> u32 {
match self { match self {
Self::Once => 0, Self::Repeat => 0,
Self::Repeat => 1, Self::Once => 1,
Self::Reverse => 2,
} }
} }
} }

View File

@ -10,16 +10,33 @@ fn animate(instance: InstanceInput, age: f32) -> u32 {
let fps = sprites.data[idx].fps; let fps = sprites.data[idx].fps;
var frame: u32 = u32(0); var frame: u32 = u32(0);
if rep == u32(1) { // Repeat
let x = age / fps; // Repeat
let m = f32(len); if rep == u32(1) {
// x fmod m
frame = u32(x - floor(x / m) * m);
} else { // Once
frame = u32(min( frame = u32(min(
(age / fps), (age / fps),
f32(len) - 1.0 f32(len) - 1.0
)); ));
// Reverse
} else if rep == u32(2) {
let x = age / fps;
let m = f32(len) * 2.0 - 1.0;
// x fmod m
frame = u32(x - floor(x / m) * m);
if frame >= len {
frame = len + len - frame - u32(1);
}
// Repeat (default)
} else {
let x = age / fps;
let m = f32(len);
// x fmod m
frame = u32(x - floor(x / m) * m);
} }
return frame + sprites.data[idx].first_frame; return frame + sprites.data[idx].first_frame;