1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! `bevy-tick-timers` provides a Bevy plugin that enables the use of tick based timers.
//!
//! Tick based timers are timers that operate not on real time, but on the number of state updates
//! that occur. Each state update constitutes a "tick".
//!
//! For any timer that does not update outside of a game session, a tick based timer is preferred.
//! This makes games more consistent and replayable (which also means they are easier to debug).
//!
//! # Example:
//!```no_run
//!use bevy::prelude::*;
//!use bevy_tick_timers::{TimerPlugin, Timers};
//!
//!fn add_timer(
//!    mut timers: ResMut<Timers>,
//!) {
//!    // Timers are closures that take the world as a mutable reference.
//!    timers.after(5, |_| {
//!        println!("timer has gone off!");
//!    });
//!}
//!
//!fn main() {
//!    println!("starting up");
//!    App::build()
//!        .add_plugins(DefaultPlugins)
//!        .add_plugin(TimerPlugin)
//!        .add_startup_system(add_timer.system())
//!        .run();
//!}
//!```
// use bevy::ecs::Stage;
use bevy::prelude::*;
use std::mem;
use std::mem::MaybeUninit;

const MAX_INTERVAL: usize = 64;

type BoxedSystem = Box<dyn FnOnce(&mut World) + Send + Sync>;

struct TimingWheel {
    current_tick: usize,
    ring: [Vec<(usize, BoxedSystem)>; MAX_INTERVAL],
}

impl Default for TimingWheel {
    fn default() -> Self {
        let mut empty = MaybeUninit::<[Vec<_>; MAX_INTERVAL]>::uninit();
        let p: *mut Vec<BoxedSystem> = unsafe { mem::transmute(&mut empty) };
        for i in 0..MAX_INTERVAL {
            unsafe {
                p.add(i).write(vec![]);
            }
        }
        TimingWheel {
            current_tick: 0,
            ring: unsafe { empty.assume_init() },
        }
    }
}

impl TimingWheel {
    /// Insert the timer into the wheel.
    fn schedule(&mut self, offset: usize, ticks: usize, timer: BoxedSystem) {
        self.ring[offset].push((ticks, timer));
    }

    /// Return all the timers that execute on the current tick, and more the clock
    /// forward one.
    fn tick(&mut self) -> Vec<(usize, BoxedSystem)> {
        let timers = mem::take(&mut self.ring[self.current_tick]);
        self.current_tick = (self.current_tick + 1) % MAX_INTERVAL;
        timers
    }
}

/// A Bevy resource that allows for the scheduling of tick based timers.
#[derive(Default)]
pub struct Timers {
    /// One frame at 120 fps.
    level: [TimingWheel; 4],
    // TODO: Add more levels (if you want to).
}

impl Timers {
    /// Schedule a timer to occur after the given number of ticks have elapsed.
    pub fn after<S>(&mut self, after: usize, timer: S)
    where
        S: FnOnce(&mut World) + Send + Sync + 'static,
    {
        let ticks = after
            + self.level[0].current_tick
            + (self.level[1].current_tick << 6)
            + (self.level[2].current_tick << 12)
            + (self.level[3].current_tick << 18);
        let level = if ticks == 0 {
            0
        } else {
            (63 - ticks.leading_zeros()) / 6
        };
        match level {
            0 => self.level[0].schedule(ticks, 0, Box::new(timer)),
            1 => self.level[1].schedule((ticks >> 6) - 1, ticks & 0b111111, Box::new(timer)),
            2 => self.level[2].schedule((ticks >> 12) - 1, ticks & 0b111111111111, Box::new(timer)),
            3 => self.level[3].schedule(
                (ticks >> 18) - 1,
                ticks & 0b111111111111111111,
                Box::new(timer),
            ),
            _ => panic!("timer interval too long"),
        }
    }

    /// Schedule a timer to occur right now.
    pub fn now<S>(&mut self, timer: S)
    where
        S: FnOnce(&mut World) + Send + Sync + 'static,
    {
        self.after(0, timer);
    }

    fn tick(&mut self) -> Vec<BoxedSystem> {
        // Surely there is a better way to do this.
        let v = self.level[0].tick().into_iter().map(|(_, x)| x).collect();
        if self.level[0].current_tick == 63 {
            for (tick, timer) in self.level[1].tick() {
                self.level[0].schedule(tick, 0, timer);
            }
            if self.level[1].current_tick == 63 {
                for (tick, timer) in self.level[2].tick() {
                    self.level[1].schedule((tick >> 6) - 1, tick & 0b111111, timer);
                }
                if self.level[2].current_tick == 63 {
                    for (tick, timer) in self.level[3].tick() {
                        self.level[2].schedule((tick >> 6) - 1, tick & 0b111111111111, timer);
                    }
                }
            }
        }
        v
    }
}

#[derive(Default)]
struct RunTimers;

impl Stage for RunTimers {
    fn run(&mut self, world: &mut World) {
        let timers = world.get_resource_mut::<Timers>().expect("Failed").tick();
        for timer in timers {
            timer(world);
        }
    }
}

/// A Bevy plugin that adds the [Timers] resource and a scheduler to execute timers each
/// game update.
pub struct TimerPlugin;

impl Plugin for TimerPlugin {
    fn build(&self, app: &mut App) {
        app.world.insert_resource(Timers::default());
        app.add_stage("run_timers", RunTimers);
    }
}