From 6afc2d0b0445807c1d49cbadcf0b2252e56e1d77 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 25 Jul 2025 11:33:05 -0700 Subject: [PATCH] Holy shit rewind. --- src/bin/flappy/main.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 3d174c1..02ea03c 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -31,6 +31,8 @@ fn main() { ( // Only flap when we press the space key flap.run_if(input_just_pressed(KeyCode::Space)), + // Rewinding systems + record.run_if(any_component_changed::), ) .run_if(in_state(PlayerState::Alive)), // Rewinding systems @@ -51,6 +53,13 @@ enum PlayerState { Dead, } +// A tape tracking the bird's state every frame +#[derive(Component, Default)] +struct Tape { + translations: Vec, + rotations: Vec, +} + fn init_bird( mut commands: Commands, server: Res, @@ -74,7 +83,9 @@ fn init_bird( let force = ExternalForce::default().with_persistence(false); - commands.spawn((name, mesh, material, mass, t, Bird, force)); + let tape = Tape::default(); + + commands.spawn((name, mesh, material, mass, t, Bird, force, tape)); } fn init_ui( @@ -133,17 +144,33 @@ fn toggle_rewind(keycode: Res>, mut next: ResMut>, + mut birds: Query<(&Transform, &mut Tape), With>, +) { + debug_assert!( + matches!(state.get(), PlayerState::Alive), + "Only record in the alive state" + ); + + birds.iter_mut().for_each(|(transform, mut tape)| { + tape.translations.push(transform.translation); + tape.rotations.push(transform.rotation); + }); +} + fn rewind( #[cfg(debug_assertions)] state: Res>, - birds: Query<&Transform, With>, + mut birds: Query<(&mut Transform, &mut Tape), With>, ) { debug_assert!( matches!(state.get(), PlayerState::Rewind), "Only rewind in the rewinding state" ); - birds.iter().for_each(|bird| { - info!("Position: {:?}", *bird); + birds.iter_mut().for_each(|(mut transform, mut tape)| { + transform.translation = tape.translations.pop().unwrap(); + transform.rotation = tape.rotations.pop().unwrap(); }); }