use nadir_ecs::World;
use nadir_input::{ActionSet, ActionState, Input, InputError, InputPlayer};
use nadir_scene::{ReflectContext, TypeRegistry};
use crate::app::LatchFn;
pub fn world_hash(world: &World) -> u64 {
let registry = world.resource::<TypeRegistry>();
registry.hash_world(world, &mut ReflectContext::new("nadir::world_hash"))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Replay {
Playing {
ticks: u64,
},
Ended {
ticks: u64,
},
Failed {
ticks: u64,
error: InputError,
},
}
pub(crate) trait PlayReplay {
fn latch(&mut self, input: &mut Input, world: &World) -> u64;
fn ticks(&self) -> u64;
fn ahead(&self) -> &Result<bool, InputError>;
}
pub(crate) struct ReplayInput<A: ActionSet> {
player: InputPlayer<'static, A>,
next: ActionState<A>,
ahead: Result<bool, InputError>,
ticks: u64,
live: LatchFn,
}
impl<A: ActionSet> ReplayInput<A> {
pub(crate) fn new(mut player: InputPlayer<'static, A>, live: LatchFn) -> Self {
let mut next = ActionState::new();
let ahead = player.next_tick(&mut next);
Self {
player,
next,
ahead,
ticks: 0,
live,
}
}
}
impl<A: ActionSet> PlayReplay for ReplayInput<A> {
fn latch(&mut self, input: &mut Input, world: &World) -> u64 {
(self.live)(input, world);
std::mem::swap(&mut *world.resource_mut::<ActionState<A>>(), &mut self.next);
self.ticks += 1;
self.ahead = self.player.next_tick(&mut self.next);
self.ticks
}
fn ticks(&self) -> u64 {
self.ticks
}
fn ahead(&self) -> &Result<bool, InputError> {
&self.ahead
}
}