pub fn update(&mut self, ts: u32, input: &Input, projectiles: &mut Vec<Projectile>) {
        /// Do movement function. Modifies pos according to input module.
        ///
        /// Also returns if anything acually happened.
        fn do_movement(pos: &mut u32, input: &Input) -> bool {
            if input.left() && input.right() {
                false
            } else if *pos > 0 && input.left() {
                *pos -= 1;
                true
            } else if *pos < 77 && input.right() {
                *pos += 1;
                true
            } else {
                false
            }
        }

        let timer = &mut self.movement_timer;
        let pos = &mut self.pos;
        let shoot_timer = &mut self.shoot_timer;

        // Movement speed (every 80 ms).
        const SPEED: u32 = 80;

        // Match on timer state, and check if new timer is needed.
        // -------------------------------------------------------
        let need_timer = match timer {
            None => {
                // No timer = move now. This is for instant input reaction.
                // Ony if returns true set up a timer.
                // --------------------------------------------------------
                do_movement(pos, input)
            }
            Some(t) => {
                // When there is a timer, only execute movement on timer event. This limits movement speed.
                t.check(ts, |off| {
                    let mut did_something = false;

                    // Also when the deviation is big enough player may need multiple movements at once to correct deviation.
                    for _ in 0..(off + SPEED) / SPEED {
                        did_something = do_movement(pos, &input);
                    }
                    // Check if this did not actually do anything. When true, remove timer.
                    !did_something
                })
                .map(|did_nothing| {
                    if did_nothing {
                        *timer = None;
                    }
                });
                false
            }
        };

        // Set a new timer when requested.
        // -------------------------------
        if need_timer {
            self.movement_timer = Some(Timer::interval(ts, SPEED));
        }

        let need_timer = match shoot_timer {
            None => {
                if input.shoot() {
                    projectiles.push(Projectile::new(*pos + 1));
                    true
                } else {
                    false
                }
            }
            Some(t) => {
                t.check(ts, |_off| {
                    if input.shoot() {
                        projectiles.push(Projectile::new(*pos + 1));
                        false
                    } else {
                        true
                    }
                })
                .map(|remove_timer| {
                    if remove_timer {
                        *shoot_timer = None;
                    }
                });
                false
            }
        };

        // Also set new timer when required.
        // ---------------------------------
        if need_timer {
            self.shoot_timer = Some(Timer::interval(ts, SPEED));
        }
    }
/// Update the payer every game loop. It has its own ticks provided by timers.utils
/// Needs access to projectile vector to spawn new projectiles.
Update the payer every game loop. It has its own ticks provided by timers.utils
Needs access to projectile vector to spawn new projectiles.
Update the payer every game loop . It has its own ticks provided by timers . utils Needs access to projectile vector to spawn new projectiles .
pub fn update(&mut self, ts: u32, input: &Input, projectiles: &mut Vec<Projectile>) {
        /// Do movement function. Modifies pos according to input module.
        ///
        /// Also returns if anything acually happened.
        fn do_movement(pos: &mut u32, input: &Input) -> bool {
            if input.left() && input.right() {
                false
            } else if *pos > 0 && input.left() {
                *pos -= 1;
                true
            } else if *pos < 77 && input.right() {
                *pos += 1;
                true
            } else {
                false
            }
        }
        let timer = &mut self.movement_timer;
        let pos = &mut self.pos;
        let shoot_timer = &mut self.shoot_timer;
        // Movement speed (every 80 ms).
        const SPEED: u32 = 80;
        // Match on timer state, and check if new timer is needed.
        // -------------------------------------------------------
        let need_timer = match timer {
            None => {
                // No timer = move now. This is for instant input reaction.
                // Ony if returns true set up a timer.
                // --------------------------------------------------------
                do_movement(pos, input)
            }
            Some(t) => {
                // When there is a timer, only execute movement on timer event. This limits movement speed.
                t.check(ts, |off| {
                    let mut did_something = false;
                    // Also when the deviation is big enough player may need multiple movements at once to correct deviation.
                    for _ in 0..(off + SPEED) / SPEED {
                        did_something = do_movement(pos, &input);
                    }
                    // Check if this did not actually do anything. When true, remove timer.
                    !did_something
                })
                .map(|did_nothing| {
                    if did_nothing {
                        *timer = None;
                    }
                });
                false
            }
        };
        // Set a new timer when requested.
        // -------------------------------
        if need_timer {
            self.movement_timer = Some(Timer::interval(ts, SPEED));
        }
        let need_timer = match shoot_timer {
            None => {
                if input.shoot() {
                    projectiles.push(Projectile::new(*pos + 1));
                    true
                } else {
                    false
                }
            }
            Some(t) => {
                t.check(ts, |_off| {
                    if input.shoot() {
                        projectiles.push(Projectile::new(*pos + 1));
                        false
                    } else {
                        true
                    }
                })
                .map(|remove_timer| {
                    if remove_timer {
                        *shoot_timer = None;
                    }
                });
                false
            }
        };
        // Also set new timer when required.
        // ---------------------------------
        if need_timer {
            self.shoot_timer = Some(Timer::interval(ts, SPEED));
        }
    }
pub fn update ( & mut self , ts : u32 , input : & Input , projectiles : & mut Vec < Projectile > ) { /// Do movement function. Modifies pos according to input module. /// /// Also returns if anything acually happened. fn do_movement ( pos : & mut u32 , input : & Input ) -> bool { if input . left ( ) && input . right ( ) { false } else if * pos > 0 && input . left ( ) { * pos -= 1 ; true } else if * pos < 77 && input . right ( ) { * pos += 1 ; true } else { false } } let timer = & mut self . movement_timer ; let pos = & mut self . pos ; let shoot_timer = & mut self . shoot_timer ; // Movement speed (every 80 ms). const SPEED : u32 = 80 ; // Match on timer state, and check if new timer is needed. // ------------------------------------------------------- let need_timer = match timer { None => { // No timer = move now. This is for instant input reaction. // Ony if returns true set up a timer. // -------------------------------------------------------- do_movement ( pos , input ) } Some ( t ) => { // When there is a timer, only execute movement on timer event. This limits movement speed. t . check ( ts , | off | { let mut did_something = false ; // Also when the deviation is big enough player may need multiple movements at once to correct deviation. for _ in 0 .. ( off + SPEED ) / SPEED { did_something = do_movement ( pos , & input ) ; } // Check if this did not actually do anything. When true, remove timer. ! did_something } ) . map ( | did_nothing | { if did_nothing { * timer = None ; } } ) ; false } } ; // Set a new timer when requested. // ------------------------------- if need_timer { self . movement_timer = Some ( Timer :: interval ( ts , SPEED ) ) ; } let need_timer = match shoot_timer { None => { if input . shoot ( ) { projectiles . push ( Projectile :: new ( * pos + 1 ) ) ; true } else { false } } Some ( t ) => { t . check ( ts , | _off | { if input . shoot ( ) { projectiles . push ( Projectile :: new ( * pos + 1 ) ) ; false } else { true } } ) . map ( | remove_timer | { if remove_timer { * shoot_timer = None ; } } ) ; false } } ; // Also set new timer when required. // --------------------------------- if need_timer { self . shoot_timer = Some ( Timer :: interval ( ts , SPEED ) ) ; } }
Update the payer every game loop.
Update the payer every game loop .
&mut self
ts
u32
input
&Input
projectiles
&mut Vec<Projectile>
&mut self
ts
u32
input
&Input
projectiles
&mut Vec<Projectile>