Optimize instant replay
This commit is contained in:
parent
6147640213
commit
8bb39f9572
@ -257,6 +257,7 @@ pub struct Game {
|
||||
pub goals: HashMap<u8, u16>,
|
||||
|
||||
pub replay_recorder: Option<Gd<ReplayRecorder>>,
|
||||
pub current_replay_frame_start: usize,
|
||||
pub current_replay_playback: Option<Gd<ReplayPlayback>>,
|
||||
pub last_replay_clone: f64,
|
||||
|
||||
@ -361,7 +362,10 @@ impl INode for Game {
|
||||
{
|
||||
self.last_replay_clone += delta;
|
||||
if self.last_replay_clone >= 1. {
|
||||
playback.bind_mut().replay = recorder.bind().replay.clone();
|
||||
playback.bind_mut().replay = recorder
|
||||
.bind()
|
||||
.replay
|
||||
.clone_since(self.current_replay_frame_start);
|
||||
self.last_replay_clone = 0.
|
||||
}
|
||||
}
|
||||
@ -404,7 +408,14 @@ impl Game {
|
||||
pub fn start_playback(&mut self, time_delta: f64, spectated_id: u16) {
|
||||
if let Some(recorder) = &self.replay_recorder {
|
||||
let mut playback = ReplayPlayback::new_alloc();
|
||||
playback.bind_mut().replay = recorder.bind().replay.clone();
|
||||
|
||||
let frame = recorder
|
||||
.bind()
|
||||
.replay
|
||||
.find_frame(recorder.bind().time_elapsed + time_delta);
|
||||
self.current_replay_frame_start = frame;
|
||||
|
||||
playback.bind_mut().replay = recorder.bind().replay.clone_since(frame);
|
||||
playback.bind_mut().current_map = self.current_map.clone();
|
||||
playback.bind_mut().current_time = (recorder.bind().time_elapsed + time_delta).max(0.);
|
||||
playback.bind_mut().fast_forwarding = true;
|
||||
|
||||
@ -508,6 +508,33 @@ impl Default for Replay {
|
||||
}
|
||||
}
|
||||
|
||||
impl Replay {
|
||||
pub fn find_frame(&self, elapsed: f64) -> usize {
|
||||
self.frames
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip_while(|(_, f)| f.time < elapsed)
|
||||
.next()
|
||||
.map(|(i, _)| i)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn clone_since(&self, frame_idx: usize) -> Replay {
|
||||
Replay {
|
||||
map_idx: self.map_idx,
|
||||
players: self.players.clone(),
|
||||
teams: self.teams.clone(),
|
||||
goals: self.goals.clone(),
|
||||
frames: self
|
||||
.frames
|
||||
.iter()
|
||||
.skip(frame_idx)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ReplayTeam {
|
||||
pub name: String,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user