Skip to content

Commit

Permalink
timer with type param
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Dec 22, 2020
1 parent 3b2c6ce commit 36f9579
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Plugin for CorePlugin {
.init_resource::<FixedTimesteps>()
.register_type::<Option<String>>()
.register_type::<Range<f32>>()
.register_type::<Timer>()
.register_type::<Timer<()>>()
.add_system_to_stage(stage::FIRST, time_system.system())
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system.system());
}
Expand Down
25 changes: 20 additions & 5 deletions crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,34 @@ use bevy_utils::Duration;
/// Repeating timers will only be in the finished state on each tick `duration` is reached or exceeded, and can still be reset at any given point.
///
/// Paused timers will not have elapsed time increased.
#[derive(Clone, Debug, Default, Reflect)]
#[derive(Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Timer {
pub struct Timer<T: Send + Sync + 'static = ()> {
elapsed: f32,
duration: f32,
finished: bool,
/// Will only be true on the tick `duration` is reached or exceeded.
just_finished: bool,
paused: bool,
repeating: bool,
marker: std::marker::PhantomData<T>,
}

impl Timer {
impl<T: Send + Sync + 'static> Default for Timer<T> {
fn default() -> Self {
Self {
elapsed: 0.,
duration: 0.,
finished: false,
just_finished: false,
paused: false,
repeating: false,
marker: std::marker::PhantomData::default(),
}
}
}

impl<T: Send + Sync + 'static> Timer<T> {
pub fn new(duration: Duration, repeating: bool) -> Self {
Timer {
duration: duration.as_secs_f32(),
Expand Down Expand Up @@ -144,7 +159,7 @@ mod tests {

#[test]
fn test_non_repeating() {
let mut t = Timer::from_seconds(10.0, false);
let mut t = Timer::<()>::from_seconds(10.0, false);
// Tick once, check all attributes
t.tick(0.25);
assert_eq!(t.elapsed(), 0.25);
Expand Down Expand Up @@ -183,7 +198,7 @@ mod tests {

#[test]
fn test_repeating() {
let mut t = Timer::from_seconds(2.0, true);
let mut t = Timer::<()>::from_seconds(2.0, true);
// Tick once, check all attributes
t.tick(0.75);
assert_eq!(t.elapsed(), 0.75);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_diagnostic/src/print_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct PrintDiagnosticsPlugin {

/// State used by the [PrintDiagnosticsPlugin]
pub struct PrintDiagnosticsState {
timer: Timer,
timer: Timer<PrintDiagnosticsPlugin>,
filter: Option<Vec<DiagnosticId>>,
}

Expand Down
46 changes: 46 additions & 0 deletions crates/bevy_reflect/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,49 @@ impl dyn Reflect {
self.any_mut().downcast_mut::<T>()
}
}

impl<T: Send + Sync + 'static> Reflect for std::marker::PhantomData<T> {
fn type_name(&self) -> &str {
todo!()
}

fn any(&self) -> &dyn std::any::Any {
todo!()
}

fn any_mut(&mut self) -> &mut dyn std::any::Any {
todo!()
}

fn apply(&mut self, value: &dyn Reflect) {
todo!()
}

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
todo!()
}

fn reflect_ref(&self) -> ReflectRef {
todo!()
}

fn reflect_mut(&mut self) -> ReflectMut {
todo!()
}

fn clone_value(&self) -> Box<dyn Reflect> {
todo!()
}

fn reflect_hash(&self) -> Option<u64> {
todo!()
}

fn reflect_partial_eq(&self, _value: &dyn Reflect) -> Option<bool> {
todo!()
}

fn serializable(&self) -> Option<crate::serde::Serializable> {
todo!()
}
}
7 changes: 5 additions & 2 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ fn setup(

sel.order.shuffle(&mut rnd);

commands.spawn((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
commands.spawn((Timer::<SelectTimer>::from_seconds(
SHOWCASE_TIMER_SECS,
true,
),));

commands
.spawn((ContributorDisplay,))
Expand Down Expand Up @@ -132,7 +135,7 @@ fn select_system(
mut materials: ResMut<Assets<ColorMaterial>>,
mut sel: ResMut<ContributorSelection>,
mut dq: Query<Mut<Text>, With<ContributorDisplay>>,
mut tq: Query<Mut<Timer>, With<SelectTimer>>,
mut tq: Query<Mut<Timer<SelectTimer>>>,
mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>,
time: Res<Time>,
) {
Expand Down
6 changes: 5 additions & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ fn main() {
fn animate_sprite_system(
time: Res<Time>,
texture_atlases: Res<Assets<TextureAtlas>>,
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
mut query: Query<(
&mut Timer<()>,
&mut TextureAtlasSprite,
&Handle<TextureAtlas>,
)>,
) {
for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
timer.tick(time.delta_seconds());
Expand Down
2 changes: 1 addition & 1 deletion examples/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Plugin for PrintMessagePlugin {

struct PrintMessageState {
message: String,
timer: Timer,
timer: Timer<()>,
}

fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct MyEvent {
}

struct EventTriggerState {
event_timer: Timer,
event_timer: Timer<()>,
}

impl Default for EventTriggerState {
Expand Down
8 changes: 4 additions & 4 deletions examples/ecs/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fn main() {
}

pub struct Countdown {
pub percent_trigger: Timer,
pub main_timer: Timer,
pub percent_trigger: Timer<()>,
pub main_timer: Timer<()>,
}

impl Countdown {
Expand All @@ -32,12 +32,12 @@ impl Default for Countdown {

fn setup_system(commands: &mut Commands) {
// Add an entity to the world with a timer
commands.spawn((Timer::from_seconds(5.0, false),));
commands.spawn((Timer::<()>::from_seconds(5.0, false),));
}

/// This system ticks all the `Timer` components on entities within the scene
/// using bevy's `Time` resource to get the delta between each update.
fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
fn timer_system(time: Res<Time>, mut query: Query<&mut Timer<()>>) {
for mut timer in query.iter_mut() {
if timer.tick(time.delta_seconds()).just_finished() {
info!("Entity timer just finished")
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
struct State {
atlas_count: u32,
handle: Handle<Font>,
timer: Timer,
timer: Timer<()>,
}

impl Default for State {
Expand Down

0 comments on commit 36f9579

Please sign in to comment.