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
impl Replay
Sourcepub fn new(
label: String,
standard_grid: StandardGrid,
_pacman_state: GameState,
pacbot_location: Isometry2<f32>,
) -> Self
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);
Sourcepub fn starting_at(other: &Replay) -> Self
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);
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<(Replay, usize), DecodeError>
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!");
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>
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!");
Sourcepub fn is_at_end(&self) -> bool
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());
Sourcepub fn is_at_beginning(&self) -> bool
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());
Sourcepub fn get_pacbot_location(&self) -> Isometry2<f32>
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());
fn update_current_frames(&mut self)
Sourcepub fn step_forwards(&mut self)
pub fn step_forwards(&mut self)
Moves to the next frame, if it exists
Sourcepub fn go_to_beginning(&mut self)
pub fn go_to_beginning(&mut self)
Go back to the beginning of the recording
Sourcepub fn step_forwards_until_pacman_state(&mut self)
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));
Sourcepub fn step_backwards_until_pacman_state(&mut self)
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
Sourcepub fn record_pacman_location(
&mut self,
location: Isometry2<f32>,
) -> Result<(), Error>
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());
Sourcepub fn current_frame(&self) -> usize
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
Sourcepub fn frame_count(&self) -> usize
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
Sourcepub fn time_to_next(&self) -> Duration
pub fn time_to_next(&self) -> Duration
Get the amount of time until the next frame
If at the end, returns Duration::MAX
Sourcepub fn time_to_previous(&self) -> Duration
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<'de> Deserialize<'de> for Replay
impl<'de> Deserialize<'de> for Replay
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> NoneValue for Twhere
T: Default,
impl<T> NoneValue for Twhere
T: Default,
type NoneType = T
§fn null_value() -> T
fn null_value() -> T
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.