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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! When running a pipeline, systems are ran each time [`World::progress()`](crate::core::World::progress) is called.
//! The `flecs_timer` feature addon makes it possible to run systems at a specific time interval or rate.
use core::ops::{Deref, DerefMut};
use flecs_ecs_sys::{self as sys};
use crate::core::{ComponentId, Entity, EntityView, WorldProvider, WorldRef};
use super::super::system::System;
pub trait TimerAPI: Sized {
fn world(&self) -> WorldRef<'_>;
fn world_ptr(&self) -> *const sys::ecs_world_t;
fn world_ptr_mut(&self) -> *mut sys::ecs_world_t;
fn id(&self) -> Entity;
/// Set timer interval.
/// This operation will continuously invoke systems associated with the timer after the interval period expires.
/// If the entity contains an existing timer, the interval value will be reset.
///
/// The timer is synchronous, and is incremented each frame by `delta_time`.
///
/// The `tick_source` entity will be a tick source after this operation.
/// Tick sources can be read by getting the [`flecs::TickSource`](crate::core::flecs::system::TickSource) component.
/// If the tick source ticked this frame, the 'tick' member will be true.
/// When the tick source is a system, the system will tick when the timer ticks.
fn set_interval(self, interval: f32) -> Self {
unsafe { sys::ecs_set_interval(self.world_ptr_mut(), *self.id(), interval) };
self
}
/// Get current interval value for the specified timer.
///
/// This operation returns the value set by [`set_interval()`](crate::addons::timer::TimerAPI::set_interval).
///
/// # Returns
///
/// The interval. If the entity is not a timer, the operation will return 0.
fn interval(&self) -> f32 {
unsafe { sys::ecs_get_interval(self.world_ptr(), *self.id()) }
}
/// Set timer timeout.
/// This operation executes any systems associated with the timer after the specified timeout value.
/// If the entity contains an existing timer, the timeout value will be reset.
/// The timer can be started and stopped with [`start()`](crate::addons::timer::TimerAPI::start) and [`stop()`](crate::addons::timer::TimerAPI::stop).
///
/// The timer is synchronous, and is incremented each frame by `delta_time`.
///
/// The `tick_source` entity will be a tick source after this operation.
/// Tick sources can be read by getting the [`flecs::TickSource`](crate::core::flecs::system::TickSource) component.
/// If the tick source ticked this frame, the 'tick' member will be true.
/// When the tick source is a system, the system will tick when the timer ticks.
fn set_timeout(self, timeout: f32) -> Self {
unsafe { sys::ecs_set_timeout(self.world_ptr_mut(), *self.id(), timeout) };
self
}
/// Get current timeout value for the specified timer.
/// This operation returns the value set by [`set_timeout()`](crate::addons::timer::TimerAPI::set_timeout).
///
/// After the timeout expires the [`flecs::timer::Timer`](crate::core::flecs::timer::Timer) component is removed from the entity.
/// This means that if [`TimerAPI::timeout`] is invoked after the timer is expired, the operation will return 0.
///
/// The timer is synchronous, and is incremented each frame by `delta_time`.
///
/// The `tick_source` entity will be a tick source after this operation.
/// Tick sources can be read by getting the [`flecs::TickSource`](crate::core::flecs::system::TickSource) component.
/// If the tick source ticked this frame, the 'tick' member will be true.
/// When the tick source is a system, the system will tick when the timer ticks.
///
/// # Returns
///
/// The timeout. If no timer is active for this entity, the operation returns 0.
fn timeout(&self) -> f32 {
unsafe { sys::ecs_get_timeout(self.world_ptr(), *self.id()) }
}
/// Set rate filter. Will use the frame tick as tick source,
/// which corresponds with the number of times [`World::progress()`](crate::core::World::progress) is called.
/// This operation initializes a rate filter.
/// Rate filters sample tick sources and tick at a configurable multiple.
/// A rate filter is a tick source itself, which means that rate filters can be chained.
///
/// Rate filters enable deterministic system execution which cannot be achieved with interval timers alone.
/// For example, if timer A has interval 2.0 and timer B has interval 4.0,
/// it is not guaranteed that B will tick at exactly twice the multiple of A.
/// This is partly due to the indeterministic nature of timers, and partly due to floating point rounding errors.
///
/// Rate filters can be combined with timers (or other rate filters)
/// to ensure that a system ticks at an exact multiple of a tick source (which can be another system).
/// If a rate filter is created with a rate of 1 it will tick at the exact same time as its source.
///
/// The `tick_source` entity will be a tick source after this operation.
/// Tick sources can be read by getting the [`flecs::TickSource`](crate::core::flecs::system::TickSource) component.
/// If the tick source ticked this frame, the 'tick' member will be true.
/// When the tick source is a system, the system will tick when the timer ticks.
///
/// # See also
///
/// * [`TimerAPI::set_rate_w_tick_source()`]
fn set_rate(self, rate: i32) -> Self {
unsafe { sys::ecs_set_rate(self.world_ptr_mut(), *self.id(), rate, 0) };
self
}
/// Set rate filter.
/// This operation initializes a rate filter.
/// Rate filters sample tick sources and tick at a configurable multiple.
/// A rate filter is a tick source itself, which means that rate filters can be chained.
///
/// Rate filters enable deterministic system execution which cannot be achieved with interval timers alone.
/// For example, if timer A has interval 2.0 and timer B has interval 4.0,
/// it is not guaranteed that B will tick at exactly twice the multiple of A.
/// This is partly due to the indeterministic nature of timers, and partly due to floating point rounding errors.
///
/// Rate filters can be combined with timers (or other rate filters)
/// to ensure that a system ticks at an exact multiple of a tick source (which can be another system).
/// If a rate filter is created with a rate of 1 it will tick at the exact same time as its source.
///
/// If no tick source is provided (Entity(0)), the rate filter will use the frame tick as source,
/// which corresponds with the number of times [`World::progress()`](crate::core::World::progress) is called.
///
/// The `tick_source` entity will be a tick source after this operation.
/// Tick sources can be read by getting the [`flecs::TickSource`](crate::core::flecs::system::TickSource) component.
/// If the tick source ticked this frame, the 'tick' member will be true.
/// When the tick source is a system, the system will tick when the timer ticks.
/// # See also
///
/// * [`TimerAPI::set_rate()`]
fn set_rate_w_tick_source(self, rate: i32, tick_source: impl Into<Entity>) -> Self {
unsafe { sys::ecs_set_rate(self.world_ptr_mut(), *self.id(), rate, *tick_source.into()) };
self
}
/// Start timer.
/// This operation resets the timer and starts it with the specified timeout.
fn start(&self) {
unsafe { sys::ecs_start_timer(self.world_ptr_mut(), *self.id()) };
}
/// Stop timer.
/// This operation stops a timer from triggering.
fn stop(&self) {
unsafe { sys::ecs_stop_timer(self.world_ptr_mut(), *self.id()) };
}
}
#[derive(Debug, Clone, Copy)]
pub struct Timer<'a> {
entity: EntityView<'a>,
}
impl<'a> Deref for Timer<'a> {
type Target = EntityView<'a>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.entity
}
}
impl DerefMut for Timer<'_> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.entity
}
}
impl From<Timer<'_>> for Entity {
#[inline]
fn from(timer: Timer) -> Self {
timer.id
}
}
impl<'a> Timer<'a> {
pub(crate) fn new(world: impl WorldProvider<'a>) -> Self {
Timer {
entity: EntityView::new(world),
}
}
pub(crate) fn new_from<T: ComponentId>(world: impl WorldProvider<'a>) -> Self {
Timer {
entity: EntityView::new_from(world.world(), T::entity_id(world)),
}
}
}
impl TimerAPI for Timer<'_> {
#[inline(always)]
fn world(&self) -> WorldRef<'_> {
self.entity.world
}
#[inline(always)]
fn world_ptr(&self) -> *const flecs_ecs_sys::ecs_world_t {
self.entity.world_ptr()
}
#[inline(always)]
fn world_ptr_mut(&self) -> *mut flecs_ecs_sys::ecs_world_t {
self.entity.world_ptr_mut()
}
#[inline(always)]
fn id(&self) -> Entity {
self.id
}
}
impl TimerAPI for System<'_> {
#[inline(always)]
fn world(&self) -> WorldRef<'_> {
self.entity.world
}
#[inline(always)]
fn world_ptr(&self) -> *const flecs_ecs_sys::ecs_world_t {
self.entity.world_ptr()
}
#[inline(always)]
fn world_ptr_mut(&self) -> *mut flecs_ecs_sys::ecs_world_t {
self.entity.world_ptr_mut()
}
#[inline(always)]
fn id(&self) -> Entity {
self.id
}
}