fn tick_stream() -> impl Send+Unpin+Stream<Item=VectorEvent> {
    generator_stream(|yield_value| async move {
        // Set up the clock
        let start_time          = Instant::now();
        let mut last_time       = Duration::from_millis(0);

        // We limit to a certain number of ticks per callback (in case the task is suspended or stuck for a prolonged period of time)
        let max_ticks_per_call  = 5;

        // Ticks are generated 60 times a second
        let tick_length         = Duration::from_nanos(1_000_000_000 / 60);

        loop {
            // Time that has elapsed since the last tick
            let elapsed         = start_time.elapsed() - last_time;

            // Time remaining
            let mut remaining   = elapsed;
            let mut num_ticks   = 0;
            while remaining >= tick_length {
                if num_ticks < max_ticks_per_call {
                    // Generate the tick
                    yield_value(VectorEvent::Tick).await;
                    num_ticks += 1;
                }

                // Remove from the remaining time, and update the last tick time
                remaining -= tick_length;
                last_time += tick_length;
            }

            // Wait for half a tick before generating more ticks
            let next_time = tick_length - remaining;
            let wait_time = Duration::min(tick_length / 2, next_time);

            Delay::new(wait_time).await;
        }
    }.boxed())
}
///
/// A stream that generates a 'tick' event every time the game state should update
///
A stream that generates a 'tick' event every time the game state should update
A stream that generates a ' tick ' event every time the game state should update
fn tick_stream() -> impl Send+Unpin+Stream<Item=VectorEvent> {
    generator_stream(|yield_value| async move {
        // Set up the clock
        let start_time          = Instant::now();
        let mut last_time       = Duration::from_millis(0);
        // We limit to a certain number of ticks per callback (in case the task is suspended or stuck for a prolonged period of time)
        let max_ticks_per_call  = 5;
        // Ticks are generated 60 times a second
        let tick_length         = Duration::from_nanos(1_000_000_000 / 60);
        loop {
            // Time that has elapsed since the last tick
            let elapsed         = start_time.elapsed() - last_time;
            // Time remaining
            let mut remaining   = elapsed;
            let mut num_ticks   = 0;
            while remaining >= tick_length {
                if num_ticks < max_ticks_per_call {
                    // Generate the tick
                    yield_value(VectorEvent::Tick).await;
                    num_ticks += 1;
                }
                // Remove from the remaining time, and update the last tick time
                remaining -= tick_length;
                last_time += tick_length;
            }
            // Wait for half a tick before generating more ticks
            let next_time = tick_length - remaining;
            let wait_time = Duration::min(tick_length / 2, next_time);
            Delay::new(wait_time).await;
        }
    }.boxed())
}
fn tick_stream ( ) -> impl Send + Unpin + Stream < Item = VectorEvent > { generator_stream ( | yield_value | async move { // Set up the clock let start_time = Instant :: now ( ) ; let mut last_time = Duration :: from_millis ( 0 ) ; // We limit to a certain number of ticks per callback (in case the task is suspended or stuck for a prolonged period of time) let max_ticks_per_call = 5 ; // Ticks are generated 60 times a second let tick_length = Duration :: from_nanos ( 1_000_000_000 / 60 ) ; loop { // Time that has elapsed since the last tick let elapsed = start_time . elapsed ( ) - last_time ; // Time remaining let mut remaining = elapsed ; let mut num_ticks = 0 ; while remaining >= tick_length { if num_ticks < max_ticks_per_call { // Generate the tick yield_value ( VectorEvent :: Tick ) . await ; num_ticks += 1 ; } // Remove from the remaining time, and update the last tick time remaining -= tick_length ; last_time += tick_length ; } // Wait for half a tick before generating more ticks let next_time = tick_length - remaining ; let wait_time = Duration :: min ( tick_length / 2 , next_time ) ; Delay :: new ( wait_time ) . await ; } } . boxed ( ) ) }
A stream that generates a 'tick' event every time the game state should update
A stream that generates a ' tick ' event every time the game state should update