gui_pb/drawing/
replay_manager.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![allow(dead_code)]

use crate::replay::Replay;
use core_pb::grid::standard_grid::StandardGrid;
use core_pb::pacbot_rs::game_state::GameState;
use nalgebra::Isometry2;
use std::time::SystemTime;

/// The public interface for recording and replaying GUI data
pub struct ReplayManager {
    /// The current replay, which may be recording or playing back
    pub replay: Replay,
    /// When current_frame was played; used to determine when to advance the replay
    pub playback_time: SystemTime,
    /// Whether playback is paused
    pub playback_paused: bool,
    /// Speed of playback - 0 is stopped, 1 is normal forwards
    pub playback_speed: f32,
}

impl Default for ReplayManager {
    fn default() -> Self {
        Self {
            replay: Replay::default(),
            playback_time: SystemTime::now(),
            playback_paused: true,
            playback_speed: 1.0,
        }
    }
}

#[allow(dead_code)]
impl ReplayManager {
    /// Create a new ReplayManager; assumes that it is starting in recording mode
    ///
    /// Note: pacman_state is copied once to initialize the replay
    pub fn new(
        filename: String,
        standard_grid: StandardGrid,
        pacman_state: GameState,
        pacbot_location: Isometry2<f32>,
    ) -> ReplayManager {
        let replay = Replay::new(
            filename.to_owned(),
            standard_grid,
            pacman_state,
            pacbot_location,
        );
        ReplayManager {
            replay,
            playback_time: SystemTime::now(),
            playback_paused: true,
            playback_speed: 1.0,
        }
    }

    pub fn reset_replay(
        &mut self,
        selected_grid: StandardGrid,
        pacman_state: &GameState,
        pacbot_pos: Isometry2<f32>,
    ) {
        self.replay = Replay::new(
            "replay".to_string(),
            selected_grid,
            pacman_state.to_owned(),
            pacbot_pos,
        );
    }

    fn update_with_replay(&mut self, _pacman_state: &mut GameState) {
        // let mut pacman_state_new = self.replay.get_pacman_state();
        //
        // pacman_state_new.pause();
        // *pacman_state = pacman_state_new;
    }
}

// /// Play back or save frames as necessary
// ///
// /// When not in Playback mode, update_replay_playback has no effect
// pub fn update_replay_manager_system(
//     pacman_state: Res<PacmanGameState>,
//     phys_render: Res<LightPhysicsInfo>,
//     replay_manager: ResMut<ReplayManager>,
//     settings: Res<UserSettings>,
// ) {
//     if let Err(e) = update_replay_manager(pacman_state, phys_render, replay_manager, settings) {
//         eprintln!("Update replay manager failed: {:?}", e);
//     }
// }

// fn update_replay_manager(
//     pacman_state: Res<PacmanGameState>,
//     phys_render: Res<LightPhysicsInfo>,
//     mut replay_manager: ResMut<ReplayManager>,
//     settings: Res<UserSettings>,
// ) -> Result<(), Error> {
//     if pacman_state.is_changed() && settings.mode != AppMode::Playback {
//         // if we aren't recording the physics position, we should record the game position
//         if !settings.replay_save_location {
//             replay_manager
//                 .replay
//                 .record_pacman_location(Isometry2::from_parts(
//                     Translation::new(
//                         pacman_state.0.get_state().pacman_loc.row as f32,
//                         pacman_state.0.get_state().pacman_loc.col as f32,
//                     ),
//                     Rotation::new(match pacman_state.0.get_state().pacman_loc.dir {
//                         pacbot_rs::location::RIGHT => std::f32::consts::FRAC_PI_2,
//                         pacbot_rs::location::UP => std::f32::consts::PI,
//                         pacbot_rs::location::LEFT => std::f32::consts::FRAC_PI_2 * 3.0,
//                         pacbot_rs::location::DOWN => 0.0,
//                         _ => 0.0,
//                     }),
//                 ))?;
//         }
//         replay_manager.replay.record_pacman_state(&pacman_state.0)?;
//     }
//
//     if settings.mode != AppMode::Playback && settings.replay_save_location {
//         // save physics position
//         if let Some(position) = phys_render.real_pos {
//             replay_manager.replay.record_pacman_location(position)?;
//         }
//     }
//
//     if replay_manager.playback_paused {
//         // When playback is paused, constantly set this to now so that it starts up correctly
//         replay_manager.playback_time = SystemTime::now();
//         return Ok(());
//     }
//
//     if replay_manager.replay.is_at_end() {
//         replay_manager.playback_paused = true;
//         // we have reached the end of the replay
//         return Ok(());
//     }
//
//     let now = SystemTime::now();
//
//     if replay_manager.playback_speed >= 0.0 {
//         loop {
//             let time_to_next = replay_manager.replay.time_to_next().as_secs_f32();
//             let should_step_replay = time_to_next / replay_manager.playback_speed
//                 < now
//                     .duration_since(replay_manager.playback_time)?
//                     .as_secs_f32();
//
//             if !should_step_replay {
//                 break;
//             }
//
//             replay_manager.replay.step_forwards();
//             let speed = replay_manager.playback_speed;
//             replay_manager.playback_time += Duration::from_secs_f32(time_to_next / speed);
//         }
//     } else {
//         loop {
//             let time_to_previous = replay_manager.replay.time_to_previous().as_secs_f32();
//             let should_step_replay = time_to_previous / -replay_manager.playback_speed
//                 < now
//                     .duration_since(replay_manager.playback_time)?
//                     .as_secs_f32();
//
//             if !should_step_replay {
//                 break;
//             }
//
//             replay_manager.replay.step_back();
//             let speed = replay_manager.playback_speed;
//             replay_manager.playback_time += Duration::from_secs_f32(time_to_previous / -speed);
//         }
//     }
//
//     Ok(())
// }
//
// pub fn replay_playback(
//     mut pacman_state: ResMut<PacmanGameState>,
//     mut replay_manager: ResMut<ReplayManager>,
//     settings: Res<UserSettings>,
// ) {
//     if settings.mode == AppMode::Playback {
//         replay_manager.update_with_replay(&mut pacman_state.0);
//     }
// }