Bevy 0.16 upgrade (#24)
This commit is contained in:
63
src/utils/trail.rs
Normal file
63
src/utils/trail.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::GameState;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Trail {
|
||||
points: Vec<Vec3>,
|
||||
col: LinearRgba,
|
||||
}
|
||||
|
||||
impl Trail {
|
||||
pub fn new(pos: Vec3, col: LinearRgba) -> Self {
|
||||
let mut v = Vec::with_capacity(12);
|
||||
v.push(pos);
|
||||
Self { points: v, col }
|
||||
}
|
||||
|
||||
pub fn add(&mut self, pos: Vec3) {
|
||||
if self.points.len() >= self.points.capacity() {
|
||||
self.points.pop();
|
||||
}
|
||||
self.points.insert(0, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
update_trail.run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
}
|
||||
|
||||
fn update_trail(
|
||||
mut query: Query<(&mut Trail, &Gizmo, &GlobalTransform, &ChildOf)>,
|
||||
global_transform: Query<&GlobalTransform>,
|
||||
mut gizmo_assets: ResMut<Assets<GizmoAsset>>,
|
||||
) -> Result<(), BevyError> {
|
||||
for (mut trail, gizmo, pos, parent) in query.iter_mut() {
|
||||
trail.add(pos.translation());
|
||||
|
||||
let parent_transform = global_transform.get(parent.parent())?;
|
||||
|
||||
let Some(gizmo) = gizmo_assets.get_mut(gizmo.handle.id()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
for window in trail.points.windows(2) {
|
||||
let [a, b] = window else {
|
||||
continue;
|
||||
};
|
||||
let a = GlobalTransform::from_translation(*a)
|
||||
.reparented_to(parent_transform)
|
||||
.translation;
|
||||
let b = GlobalTransform::from_translation(*b)
|
||||
.reparented_to(parent_transform)
|
||||
.translation;
|
||||
|
||||
gizmo.line(a, b, trail.col);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user