-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
171fa4a
commit 709234b
Showing
4 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
( | ||
duration_millis: 125, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::time::Duration; | ||
|
||
use bevy::prelude::*; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use crate::core::UpdateSet; | ||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<(ConfigHandle<StepConfig>, StepTimer, Step)>(); | ||
} | ||
|
||
#[derive(Asset, Reflect, Serialize, Deserialize)] | ||
struct StepConfig { | ||
duration_millis: u64, | ||
} | ||
|
||
impl Config for StepConfig { | ||
const PATH: &'static str = "config/step.ron"; | ||
|
||
const EXTENSION: &'static str = "step.ron"; | ||
|
||
fn apply(&self, world: &mut World) { | ||
world | ||
.resource_mut::<StepTimer>() | ||
.0 | ||
.set_duration(Duration::from_millis(self.duration_millis)); | ||
} | ||
} | ||
|
||
#[derive(Resource, Reflect)] | ||
#[reflect(Resource)] | ||
pub struct StepTimer(pub Timer); | ||
|
||
impl Configure for StepTimer { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
app.init_resource::<Self>(); | ||
app.add_systems(Update, tick_step_timer.in_set(UpdateSet::TickTimers)); | ||
} | ||
} | ||
|
||
impl Default for StepTimer { | ||
fn default() -> Self { | ||
Self(Timer::new(Duration::from_millis(125), TimerMode::Repeating)) | ||
} | ||
} | ||
|
||
fn tick_step_timer(time: Res<Time>, mut step_timer: ResMut<StepTimer>) { | ||
step_timer.0.tick(time.delta()); | ||
} | ||
|
||
#[derive(Resource, Reflect, Default)] | ||
#[reflect(Resource)] | ||
pub struct Step(pub usize); | ||
|
||
impl Configure for Step { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
app.init_resource::<Self>(); | ||
app.add_systems(Update, update_step.in_set(UpdateSet::SyncEarly)); | ||
app.add_systems( | ||
Update, | ||
(|step: Res<Step>| println!("Step: {}", step.0)) | ||
.in_set(UpdateSet::Update) | ||
.run_if(on_step(2)), | ||
); | ||
} | ||
} | ||
|
||
fn update_step(step_timer: Res<StepTimer>, mut step: ResMut<Step>) { | ||
step.0 += step_timer.0.times_finished_this_tick() as usize; | ||
} | ||
|
||
/// A run condition to run a system every `n` steps. | ||
pub fn on_step(n: usize) -> impl Fn(Res<StepTimer>, Res<Step>) -> bool { | ||
move |step_timer, step| { | ||
let hi = step.0; | ||
let lo = hi - step_timer.0.times_finished_this_tick() as usize; | ||
hi / n > lo / n | ||
} | ||
} |