|
|
|
@ -128,13 +128,57 @@ enum PlayerState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A tape tracking the bird's state every frame
|
|
|
|
// A tape tracking the bird's state every frame
|
|
|
|
#[derive(Component, Default)]
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Tape {
|
|
|
|
struct Tape {
|
|
|
|
linear_velocities: Vec<LinearVelocity>,
|
|
|
|
capacity: usize,
|
|
|
|
angular_velocities: Vec<AngularVelocity>,
|
|
|
|
linear_velocities: VecDeque<LinearVelocity>,
|
|
|
|
external_impulses: Vec<ExternalImpulse>,
|
|
|
|
angular_velocities: VecDeque<AngularVelocity>,
|
|
|
|
positions: Vec<Position>,
|
|
|
|
external_impulses: VecDeque<ExternalImpulse>,
|
|
|
|
rotations: Vec<Rotation>,
|
|
|
|
positions: VecDeque<Position>,
|
|
|
|
|
|
|
|
rotations: VecDeque<Rotation>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Tape {
|
|
|
|
|
|
|
|
fn new_with_capacity(capacity: usize) -> Self {
|
|
|
|
|
|
|
|
Tape {
|
|
|
|
|
|
|
|
capacity,
|
|
|
|
|
|
|
|
linear_velocities: VecDeque::with_capacity(capacity),
|
|
|
|
|
|
|
|
angular_velocities: VecDeque::with_capacity(capacity),
|
|
|
|
|
|
|
|
external_impulses: VecDeque::with_capacity(capacity),
|
|
|
|
|
|
|
|
positions: VecDeque::with_capacity(capacity),
|
|
|
|
|
|
|
|
rotations: VecDeque::with_capacity(capacity),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn push(&mut self, lv: LinearVelocity, av: AngularVelocity, ei: ExternalImpulse, p: Position, r: Rotation) {
|
|
|
|
|
|
|
|
// If we are at capacity, make room
|
|
|
|
|
|
|
|
if self.linear_velocities.len() == self.capacity {
|
|
|
|
|
|
|
|
self.linear_velocities.pop_front().unwrap();
|
|
|
|
|
|
|
|
self.angular_velocities.pop_front().unwrap();
|
|
|
|
|
|
|
|
self.external_impulses.pop_front().unwrap();
|
|
|
|
|
|
|
|
self.positions.pop_front().unwrap();
|
|
|
|
|
|
|
|
self.rotations.pop_front().unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.linear_velocities.push_back(lv);
|
|
|
|
|
|
|
|
self.angular_velocities.push_back(av);
|
|
|
|
|
|
|
|
self.external_impulses.push_back(ei);
|
|
|
|
|
|
|
|
self.positions.push_back(p);
|
|
|
|
|
|
|
|
self.rotations.push_back(r);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn pop(&mut self) -> Option<(LinearVelocity, AngularVelocity, ExternalImpulse, Position, Rotation)> {
|
|
|
|
|
|
|
|
if self.linear_velocities.is_empty() {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let lv = self.linear_velocities.pop_back().unwrap();
|
|
|
|
|
|
|
|
let av = self.angular_velocities.pop_back().unwrap();
|
|
|
|
|
|
|
|
let ei = self.external_impulses.pop_back().unwrap();
|
|
|
|
|
|
|
|
let p = self.positions.pop_back().unwrap();
|
|
|
|
|
|
|
|
let r = self.rotations.pop_back().unwrap();
|
|
|
|
|
|
|
|
Some((lv, av, ei, p, r))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn init_bird(mut commands: Commands, bird_assets: Res<BirdAssets>) {
|
|
|
|
fn init_bird(mut commands: Commands, bird_assets: Res<BirdAssets>) {
|
|
|
|
@ -150,7 +194,10 @@ fn init_bird(mut commands: Commands, bird_assets: Res<BirdAssets>) {
|
|
|
|
MaxLinearSpeed(500.0),
|
|
|
|
MaxLinearSpeed(500.0),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let tape = Tape::default();
|
|
|
|
// 60fps * 60 seconds
|
|
|
|
|
|
|
|
// let tape = Tape::new_with_capacity(60 * 60);
|
|
|
|
|
|
|
|
const REWIND_SECONDS: usize = 5;
|
|
|
|
|
|
|
|
let tape = Tape::new_with_capacity(60 * REWIND_SECONDS);
|
|
|
|
|
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
commands.spawn((
|
|
|
|
name,
|
|
|
|
name,
|
|
|
|
@ -749,11 +796,7 @@ fn record(
|
|
|
|
birds
|
|
|
|
birds
|
|
|
|
.iter_mut()
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|(lv, av, ei, p, r, mut tape)| {
|
|
|
|
.for_each(|(lv, av, ei, p, r, mut tape)| {
|
|
|
|
tape.linear_velocities.push(*lv);
|
|
|
|
tape.push(*lv, *av, *ei, *p, *r);
|
|
|
|
tape.angular_velocities.push(*av);
|
|
|
|
|
|
|
|
tape.external_impulses.push(*ei);
|
|
|
|
|
|
|
|
tape.positions.push(*p);
|
|
|
|
|
|
|
|
tape.rotations.push(*r);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -781,15 +824,15 @@ fn rewind(
|
|
|
|
|
|
|
|
|
|
|
|
birds.iter_mut().for_each(
|
|
|
|
birds.iter_mut().for_each(
|
|
|
|
|(mut lv, mut av, mut ei, mut p, mut r, mut tape)| {
|
|
|
|
|(mut lv, mut av, mut ei, mut p, mut r, mut tape)| {
|
|
|
|
if tape.positions.is_empty() {
|
|
|
|
if let Some((new_lv, new_av, new_ei, new_p, new_r)) = tape.pop() {
|
|
|
|
next.set(PlayerState::Pause);
|
|
|
|
lv.0 = new_lv.0;
|
|
|
|
} else {
|
|
|
|
av.0 = new_av.0;
|
|
|
|
lv.0 = tape.linear_velocities.pop().unwrap().0;
|
|
|
|
ei.set_impulse(new_ei.impulse());
|
|
|
|
av.0 = tape.angular_velocities.pop().unwrap().0;
|
|
|
|
p.0 = new_p.0;
|
|
|
|
ei.set_impulse(tape.external_impulses.pop().unwrap().impulse());
|
|
|
|
*r = new_r;
|
|
|
|
p.0 = tape.positions.pop().unwrap().0;
|
|
|
|
|
|
|
|
*r = tape.rotations.pop().unwrap();
|
|
|
|
|
|
|
|
frames.0 += 1;
|
|
|
|
frames.0 += 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
next.set(PlayerState::Pause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|