gui_pb::replay

Struct Replay

Source
pub struct Replay {
    start_time: SystemTime,
    standard_grid: StandardGrid,
    pub label: String,
    frames: Vec<ReplayFrame>,
    current_frame: usize,
    pacman_state_frame: usize,
    location_frame: usize,
}
Expand description

A collection of frames representing a full replay, along with associated metadata

Fields§

§start_time: SystemTime

The time when recording started

§standard_grid: StandardGrid

The StandardGrid the recording uses

§label: String

The name/label given to this replay (usually matches the file name)

§frames: Vec<ReplayFrame>

The data of the replay

§current_frame: usize

Index of the most recently recorded or played frame

§pacman_state_frame: usize

Index of the most recently recorded or played pacman state frame

§location_frame: usize

Index of the most recently recorded or played pacman location frame

Implementations§

Source§

impl Replay

Source

pub fn new( label: String, standard_grid: StandardGrid, _pacman_state: GameState, pacbot_location: Isometry2<f32>, ) -> Self

Start a new Replay

Note: pacman_state is copied once

§Examples
use pacbot_rs::game_engine::GameEngine;
use rapier2d::math::Isometry;
use rapier2d::na::Vector2;
use mdrc_pacbot_util::replay::Replay;
use mdrc_pacbot_util::grid::standard_grids::StandardGrid;

let mut replay = Replay::new(
    "My First Replay".to_string(),
    StandardGrid::Pacman,
    GameEngine::default(),
    StandardGrid::Pacman.get_default_pacbot_isometry()
);

assert_eq!(replay.frame_count(), 2);
assert_eq!(replay.current_frame(), 1);
Source

pub fn starting_at(other: &Replay) -> Self

Create a new Replay starting at the current frame in the given Replay

All frames are updated so that the most recent one matches the current time

May return Err if the given replay frame is in the future

§Examples
use pacbot_rs::game_engine::GameEngine;
use rand::rngs::ThreadRng;
use rapier2d::math::Isometry;
use rapier2d::na::Vector2;
use mdrc_pacbot_util::replay::Replay;
use mdrc_pacbot_util::grid::standard_grids::StandardGrid;

let mut pacman_state = GameEngine::default();
let mut rng = ThreadRng::default();

let mut replay = Replay::new(
    "My First Replay".to_string(),
    StandardGrid::Pacman,
    pacman_state.to_owned(),
    StandardGrid::Pacman.get_default_pacbot_isometry()
);

assert_eq!(replay.frame_count(), 2);
assert_eq!(replay.current_frame(), 1);

for i in 0..3 {
    pacman_state.step();
    replay.record_pacman_state(pacman_state.to_owned()).unwrap();
}

replay.step_back();
replay.step_back();

assert!(!replay.is_at_end());
assert_eq!(replay.frame_count(), 5);
assert_eq!(replay.current_frame(), 2);

let replay = Replay::starting_at(&replay);

assert!(replay.is_at_end());
assert_eq!(replay.frame_count(), 3);
assert_eq!(replay.current_frame(), 2);
Source

pub fn from_bytes(bytes: &[u8]) -> Result<(Replay, usize), DecodeError>

Create a new Replay using bytes from a file

§Examples
use mdrc_pacbot_util::replay::Replay;

let replay = Replay::default();
let replay_bytes = replay.to_bytes().expect("Failed to serialize replay!");
let replay2 = Replay::from_bytes(&replay_bytes).expect("Failed to deserialize replay!");
Source

pub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>

Get the bytes associated with the Replay

§Examples
use mdrc_pacbot_util::replay::Replay;

let replay = Replay::default();
let replay_bytes = replay.to_bytes().expect("Failed to serialize replay!");
let replay2 = Replay::from_bytes(&replay_bytes).expect("Failed to deserialize replay!");
Source

pub fn is_at_end(&self) -> bool

Returns whether the replay has played its last frame

§Examples
use pacbot_rs::game_engine::GameEngine;
use mdrc_pacbot_util::replay::Replay;

let mut replay = Replay::default();
assert!(replay.is_at_end());
replay.record_pacman_state(GameEngine::default()).unwrap();
assert!(replay.is_at_end());
replay.step_back();
assert!(!replay.is_at_end());
replay.step_forwards();
assert!(replay.is_at_end());
Source

pub fn is_at_beginning(&self) -> bool

Returns whether the replay is at the beginning

§Examples
use pacbot_rs::game_engine::GameEngine;
use mdrc_pacbot_util::replay::Replay;

let mut replay = Replay::default();
assert!(replay.is_at_beginning());
replay.record_pacman_state(GameEngine::default()).unwrap();
assert!(!replay.is_at_beginning());
replay.step_back();
assert!(replay.is_at_beginning());
replay.step_forwards();
assert!(!replay.is_at_beginning());
Source

pub fn get_pacbot_location(&self) -> Isometry2<f32>

Returns the most recent PacbotLocation

§Examples
use rapier2d::na::{Isometry2, Vector2};
use mdrc_pacbot_util::replay::Replay;

let mut replay = Replay::default();

let isometry_1 = Isometry2::new(Vector2::new(1.0, 1.0), 1.0);
replay.record_pacman_location(isometry_1.to_owned()).unwrap();
let isometry_2 = Isometry2::new(Vector2::new(2.0, 2.0), 2.0);
replay.record_pacman_location(isometry_2.to_owned()).unwrap();

assert_eq!(isometry_2, replay.get_pacbot_location());
replay.step_back();
assert_eq!(isometry_1, replay.get_pacbot_location());
Source

fn update_current_frames(&mut self)

Source

pub fn step_forwards(&mut self)

Moves to the next frame, if it exists

Source

pub fn step_back(&mut self)

Moves to the previous frame, if it exists

Source

pub fn go_to_beginning(&mut self)

Go back to the beginning of the recording

Source

pub fn go_to_end(&mut self)

Go to the end of the recording

Source

pub fn step_forwards_until_pacman_state(&mut self)

Step forwards until a PacmanState frame is reached

§Examples
use pacbot_rs::game_engine::GameEngine;
use rapier2d::na::{Isometry2, Vector2};
use mdrc_pacbot_util::replay::Replay;

let mut replay = Replay::default();

replay.record_pacman_state(GameEngine::default()).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(1.0, 1.0), 1.0)).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(2.0, 2.0), 2.0)).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(3.0, 3.0), 3.0)).unwrap();
replay.record_pacman_state(GameEngine::default()).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(4.0, 4.0), 4.0)).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(5.0, 5.0), 5.0)).unwrap();
replay.record_pacman_location(Isometry2::new(Vector2::new(6.0, 6.0), 6.0)).unwrap();
replay.record_pacman_state(GameEngine::default()).unwrap();

assert_eq!(replay.get_pacbot_location(), Isometry2::new(Vector2::new(6.0, 6.0), 6.0));
replay.step_backwards_until_pacman_state();
assert_eq!(replay.get_pacbot_location(), Isometry2::new(Vector2::new(4.0, 4.0), 4.0));
replay.step_backwards_until_pacman_state();
assert_eq!(replay.get_pacbot_location(), Isometry2::new(Vector2::new(1.0, 1.0), 1.0));
replay.step_forwards_until_pacman_state();
assert_eq!(replay.get_pacbot_location(), Isometry2::new(Vector2::new(3.0, 3.0), 3.0));
replay.step_forwards_until_pacman_state();
assert_eq!(replay.get_pacbot_location(), Isometry2::new(Vector2::new(6.0, 6.0), 6.0));
Source

pub fn step_backwards_until_pacman_state(&mut self)

Step backwards until a PacmanState frame is reached

See Replay::step_forwards_until_pacman_state for example

Source

pub fn record_pacman_location( &mut self, location: Isometry2<f32>, ) -> Result<(), Error>

Add a pacman location to the end of the replay

Returns err if the current frame is not the last frame

§Examples

use pacbot_rs::game_engine::GameEngine;
use mdrc_pacbot_util::replay::Replay;

let mut replay = Replay::default();

assert!(replay.record_pacman_state(GameEngine::default()).is_ok());
replay.step_back();
// Don't add frames to a replay that isn't at the end
assert!(replay.record_pacman_state(GameEngine::default()).is_err());
Source

pub fn current_frame(&self) -> usize

Get the index of the current frame

While this can be a good estimation of the progress through a replay, there is no guarantee that step_forwards() or step_back(), or any other methods, will hit any specific frame

Source

pub fn frame_count(&self) -> usize

Get the number of frames

While this can be used to get a good estimation of the progress through a replay, there is no guarantee that step_forwards() or step_back(), or any other methods, will hit any specific frame

Source

pub fn time_to_next(&self) -> Duration

Get the amount of time until the next frame

If at the end, returns Duration::MAX

Source

pub fn time_to_previous(&self) -> Duration

Get the amount of time between the current and previous frame

If at the end, returns Duration::MAX

Trait Implementations§

Source§

impl Clone for Replay

Source§

fn clone(&self) -> Replay

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Replay

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Replay

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Replay

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Replay

§

impl RefUnwindSafe for Replay

§

impl Send for Replay

§

impl Sync for Replay

§

impl Unpin for Replay

§

impl UnwindSafe for Replay

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T

§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,