Rust implementace neměla stavy
This commit is contained in:
parent
d786d39d3c
commit
d3cb4d6039
1 changed files with 27 additions and 20 deletions
|
@ -161,28 +161,10 @@ pub struct Simulation {
|
|||
|
||||
_grid: HashMap<(isize, isize), Vec<Asteroid>>,
|
||||
_cell_size: isize,
|
||||
|
||||
_pushed_states: Vec<(Racer, Vec<bool>)>,
|
||||
}
|
||||
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// let map_path = PathBuf::from("../../maps/test.txt");
|
||||
///
|
||||
/// let mut simulation = Simulation::load(&map_path);
|
||||
///
|
||||
/// let mut tick_result: TickResult = 0;
|
||||
///
|
||||
/// println!("Running simulation until collision...");
|
||||
///
|
||||
/// while tick_result & TickFlag::COLLIDED == 0 {
|
||||
/// tick_result = simulation.tick(Instruction::new(0, MAX_ACCELERATION));
|
||||
///
|
||||
/// println!("{:?}", simulation.racer);
|
||||
/// }
|
||||
///
|
||||
/// println!("Bam!");
|
||||
/// ```
|
||||
///
|
||||
impl Simulation {
|
||||
pub fn new(racer: Racer, asteroids: Vec<Asteroid>, goals: Vec<Goal>, bbox: BoundingBox) -> Self {
|
||||
let reached_goals = vec![false; goals.len()];
|
||||
|
@ -196,6 +178,7 @@ impl Simulation {
|
|||
reached_goals,
|
||||
_grid: HashMap::new(),
|
||||
_cell_size: CELL_SIZE,
|
||||
_pushed_states: Vec::new(),
|
||||
};
|
||||
|
||||
for &asteroid in &simulation.asteroids {
|
||||
|
@ -446,4 +429,28 @@ impl Simulation {
|
|||
|
||||
Self::new(racer, asteroids, goals, bbox)
|
||||
}
|
||||
pub fn push(&mut self) {
|
||||
// Save a copy of the current racer and reached goals
|
||||
self._pushed_states.push((self.racer.clone(), self.reached_goals.clone()));
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) {
|
||||
// Ensure there is a state to pop
|
||||
assert!(!self._pushed_states.is_empty(), "No states to pop!");
|
||||
|
||||
// Restore the last pushed state
|
||||
let (racer, reached_goals) = self._pushed_states.pop().unwrap();
|
||||
self.racer = racer;
|
||||
self.reached_goals = reached_goals;
|
||||
}
|
||||
|
||||
pub fn apply(&mut self) {
|
||||
// Ensure there is a state to apply
|
||||
assert!(!self._pushed_states.is_empty(), "No states to apply!");
|
||||
|
||||
// Apply the last pushed state without removing it
|
||||
let (racer, reached_goals) = self._pushed_states.last().unwrap().clone();
|
||||
self.racer = racer;
|
||||
self.reached_goals = reached_goals;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue