Skip to content

Commit 8effd48

Browse files
committed
use required component + remove some unused imports or obsolete comments
1 parent 88d8cb3 commit 8effd48

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

examples/interpolate_custom_schedule.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use bevy::{
1414
color::palettes::{
15-
css::{ORANGE, RED, WHITE},
15+
css::WHITE,
1616
tailwind::{CYAN_400, RED_400},
1717
},
1818
ecs::schedule::ScheduleLabel,
@@ -59,8 +59,6 @@ fn main() {
5959
interpolation_plugin,
6060
));
6161

62-
// Set the fixed timestep to just 5 Hz for demonstration purposes.
63-
6462
// Setup the scene and UI, and update text in `Update`.
6563
app.add_systems(Startup, (setup, setup_text)).add_systems(
6664
bevy::app::prelude::RunFixedMainLoop,
@@ -71,8 +69,6 @@ fn main() {
7169
),
7270
);
7371

74-
// This runs every frame to poll if our task was done.
75-
7672
app.add_systems(
7773
bevy::app::prelude::RunFixedMainLoop,
7874
(ease_translation_lerp, ease_rotation_slerp, ease_scale_lerp)
@@ -82,6 +78,7 @@ fn main() {
8278
// Run the app.
8379
app.run();
8480
}
81+
8582
/// Eases the translations of entities with linear interpolation.
8683
fn ease_translation_lerp(
8784
mut query: Query<(&mut Transform, &TranslationEasingState)>,
@@ -147,8 +144,8 @@ fn setup(
147144
let mesh = meshes.add(Rectangle::from_length(60.0));
148145

149146
commands.spawn((
150-
TaskToRenderTime::default(),
151147
Timestep {
148+
// Set the fixed timestep to just 5 Hz for demonstration purposes.
152149
timestep: Duration::from_secs_f32(0.5),
153150
},
154151
TaskResults::<TaskWorkerTraitImpl>::default(),
@@ -293,7 +290,6 @@ pub mod task_user {
293290

294291
use bevy::prelude::*;
295292
use bevy_transform_interpolation::background_fixed_schedule::TaskWorkerTrait;
296-
use rand::{thread_rng, Rng};
297293

298294
#[derive(Debug, Clone, Default)]
299295
pub struct TaskWorkerTraitImpl;
@@ -309,9 +305,8 @@ pub mod task_user {
309305
substep_count: u32,
310306
) -> Vec<(Entity, Transform, LinearVelocity, AngularVelocity)> {
311307
let simulated_time = timestep * substep_count;
312-
let to_simulate = simulated_time.as_millis() as u64;
313308
// Simulate an expensive task
314-
std::thread::sleep(Duration::from_millis(thread_rng().gen_range(200..201)));
309+
std::thread::sleep(Duration::from_millis(200));
315310

316311
// Move entities in a fixed amount of time. The movement should appear smooth for interpolated entities.
317312
flip_movement_direction(

src/background_fixed_schedule.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
use bevy::ecs::schedule::{LogLevel, ScheduleBuildSettings, ScheduleLabel};
2-
use bevy::ecs::world;
3-
use bevy::log::tracing_subscriber::fmt::time;
42
use bevy::prelude::*;
53
use bevy::tasks::AsyncComputeTaskPool;
6-
use bevy::{log::trace, prelude::World, time::Time};
4+
use bevy::{prelude::World, time::Time};
75
use crossbeam_channel::Receiver;
8-
use rand::{thread_rng, Rng};
9-
use std::default;
10-
use std::slice::IterMut;
116
use std::{collections::VecDeque, time::Duration};
127

138
///
@@ -67,7 +62,7 @@ pub struct BackgroundFixedUpdatePlugin<T: TaskWorkerTrait> {
6762
impl<T: TaskWorkerTrait> Plugin for BackgroundFixedUpdatePlugin<T> {
6863
fn build(&self, app: &mut App) {
6964
app.add_systems(
70-
bevy::app::prelude::RunFixedMainLoop, // TODO: use a specific schedule for this, à la bevy's FixedMainLoop
65+
bevy::app::prelude::RunFixedMainLoop,
7166
FixedMain::run_schedule::<T>,
7267
);
7368

@@ -120,14 +115,24 @@ pub struct TaskToRenderTime {
120115
}
121116

122117
/// Difference between tasks and rendering time
123-
#[derive(Component, Default, Reflect, Clone)]
118+
#[derive(Component, Reflect, Clone)]
124119
pub struct Timestep {
125120
pub timestep: Duration,
126121
}
127122

123+
impl Default for Timestep {
124+
fn default() -> Self {
125+
Self {
126+
timestep: Duration::from_secs_f64(1.0 / 64.0),
127+
}
128+
}
129+
}
130+
128131
/// Struct to be able to configure what the task should do.
129-
/// TODO: extract first, then do work.
132+
// TODO: This should also require `TaskResults` and `WorkTask`
133+
// but their type parameter not enforcing `Default` makes the require macro fail. This should be a bevy issue.
130134
#[derive(Clone, Component)]
135+
#[require(TaskToRenderTime, Timestep)]
131136
pub struct TaskWorker<T: TaskWorkerTrait> {
132137
pub worker: T,
133138
}

src/interpolation.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use crate::*;
88
use bevy::{
9-
app::FixedMain,
109
ecs::schedule::{InternedScheduleLabel, ScheduleLabel},
1110
prelude::*,
1211
};

0 commit comments

Comments
 (0)