/// Moves an animation component's frames forward based in its frame_time and
/// the time passed since the last update.
Moves an animation component's frames forward based in its frame_time and
the time passed since the last update.
Moves an animation component ' s frames forward based in its frame_time and the time passed since the last update .
fn advance_animation(s: &mut Sprite, a: &mut Animation, dt: f64){
        let schema = match &a.schema {
            Some(schema) => schema.clone(),
            None => return,
        };
        // Calculate how many frames need to be played ( as a float )
        a.frame_progress += (dt/(schema.frame_time as f64)) as f32;
        // Figure out how many WHOLE frames to advance ( as an int )
        let frames_to_adv = a.frame_progress.floor();
        // Remove those from frame progress
        a.frame_progress -= frames_to_adv;
        // Prevent overflow
        if frames_to_adv > 255.0 { 
            s.id = schema.root + (a.frame as u16);
            return; 
        } 
        let mut adv_frames = frames_to_adv as u8;
        while adv_frames > 0 {
             // Try to play forwards
            if !a.is_reversing {
                // Advance all frames, if possible
                if (a.frame as u32 + adv_frames as u32) < schema.frames as u32 {
                    a.frame += adv_frames;
                    break;
                } 
                // Not all frames could be advanced
                else{
                    // Calculate the number of valid frames and advance those
                    let valid_frames = (a.frame + adv_frames) - schema.frames;
                    adv_frames -= valid_frames;
                    a.frame += valid_frames;
                    // Note, there is AT LEAST one frame left now.
                    if schema.reverses { a.is_reversing = true; }
                    else if schema.loops {
                        // Return to the first frame again, costing one frame.
                        adv_frames -= 1;
                        a.frame = 0;
                    }
                    // Return to idle
                    else {
                        Self::sprite_to_idle(s, a);
                        return;
                    }
                }
            }
            // Otherwise, play in reverse
            else {
                // Reverse all the frames, if possible
                if a.frame >= adv_frames {
                    a.frame -= adv_frames;
                    s.id -= adv_frames as u16;
                    break;
                }
                // Not all frames could be reversed
                else {
                    // Reverse back to 0
                    adv_frames -= a.frame;
                    a.frame = 0;
                    // Continue back in forward animation
                    if schema.loops {
                        a.is_reversing = false;
                    }
                    // Return to idle
                    else {
                        Self::sprite_to_idle(s, a);
                        return;
                    }
                }
            }
        }
    }
fn advance_animation ( s : & mut Sprite , a : & mut Animation , dt : f64 ) { let schema = match & a . schema { Some ( schema ) => schema . clone ( ) , None => return , } ; // Calculate how many frames need to be played ( as a float ) a . frame_progress += ( dt / ( schema . frame_time as f64 ) ) as f32 ; // Figure out how many WHOLE frames to advance ( as an int ) let frames_to_adv = a . frame_progress . floor ( ) ; // Remove those from frame progress a . frame_progress -= frames_to_adv ; // Prevent overflow if frames_to_adv > 255.0 { s . id = schema . root + ( a . frame as u16 ) ; return ; } let mut adv_frames = frames_to_adv as u8 ; while adv_frames > 0 { // Try to play forwards if ! a . is_reversing { // Advance all frames, if possible if ( a . frame as u32 + adv_frames as u32 ) < schema . frames as u32 { a . frame += adv_frames ; break ; } // Not all frames could be advanced else { // Calculate the number of valid frames and advance those let valid_frames = ( a . frame + adv_frames ) - schema . frames ; adv_frames -= valid_frames ; a . frame += valid_frames ; // Note, there is AT LEAST one frame left now. if schema . reverses { a . is_reversing = true ; } else if schema . loops { // Return to the first frame again, costing one frame. adv_frames -= 1 ; a . frame = 0 ; } // Return to idle else { Self :: sprite_to_idle ( s , a ) ; return ; } } } // Otherwise, play in reverse else { // Reverse all the frames, if possible if a . frame >= adv_frames { a . frame -= adv_frames ; s . id -= adv_frames as u16 ; break ; } // Not all frames could be reversed else { // Reverse back to 0 adv_frames -= a . frame ; a . frame = 0 ; // Continue back in forward animation if schema . loops { a . is_reversing = false ; } // Return to idle else { Self :: sprite_to_idle ( s , a ) ; return ; } } } } }
Moves an animation component's frames forward based in its frame_time and
the time passed since the last update.
Moves an animation component ' s frames forward based in its frame_time and the time passed since the last update .
s
&mut Sprite
a
&mut Animation
dt
f64