Skip to content
Snippets Groups Projects
Commit d8decbde authored by Finn Bear's avatar Finn Bear
Browse files

Improve automata slide.

parent fd35df4c
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,12 @@ enum AutomataState {
rules: Option<f64>,
},
/// Simulation.
Life,
Life {
/// Whether we started expanding the grid yet.
expanding: bool,
/// Conway iteration counter.
iterations: usize,
},
}
impl Default for Automata {
......@@ -88,10 +93,20 @@ impl Slide for Automata {
} else if rules.is_none() {
*rules = Some(ctx.input().time)
} else {
self.state = AutomataState::Life;
self.state = AutomataState::Life {
expanding: false,
iterations: 0,
};
}
}
AutomataState::Life { expanding, .. } => {
if *expanding {
// Done with the slide.
return true;
} else {
*expanding = true;
}
}
AutomataState::Life => return true,
}
false
}
......@@ -126,19 +141,41 @@ impl Slide for Automata {
// How much grid goes left and rules go right.
const HORIZONTAL_OFFSET: f32 = 250.0;
match &self.state {
match &mut self.state {
AutomataState::Empty => {}
AutomataState::Static { rules, .. } => {
if let &Some(rules) = rules {
if let Some(rules) = *rules {
let elapsed = ui.ctx().input().time - rules;
self.life.offset_x = (elapsed as f32 * -400.0).max(-HORIZONTAL_OFFSET);
rules_fade = Some(rules);
}
}
AutomataState::Life => {
AutomataState::Life {
expanding,
iterations,
} => {
// Iterate grid ~5 times a second.
if self.governor.ready(ui.ctx().input().time, 0.2) {
self.life = conways_game_of_life(&self.life);
if *iterations == 10 {
// Automatically begin expansion.
*expanding = true;
} else if *iterations == 20 {
// Draw a line to create a cool pattern.
for y in 4..self.life.size_cells - 4 {
self.life.set_fill(2, y, Color32::BLACK);
}
} else if *iterations == 30 {
// Draw another line to create a cool pattern.
for x in 4..self.life.size_cells - 4 {
self.life
.set_fill(x, self.life.size_cells - 2, Color32::BLACK);
}
}
// Boolean to int cast yields 1 if true, 0 otherwise.
let expansion = (*expanding && self.life.size_cells < 64) as usize;
self.life = conways_game_of_life(&self.life, expansion);
*iterations += 1;
}
// Kludge to render rules with full opacity.
......@@ -198,10 +235,15 @@ impl Slide for Automata {
}
/// Run one iteration of Conway's Game of Life on the grid, producing a new grid.
fn conways_game_of_life(grid: &Grid) -> Grid {
///
/// A non-zero expansion parameter expands the grid on each side by that many squares.
fn conways_game_of_life(grid: &Grid, expansion: usize) -> Grid {
let mut new = grid.clone();
new.clear_fill();
// Allocate more room on left, right, top and bottom.
new.size_cells += expansion * 2;
for cy in 0..grid.size_cells {
for cx in 0..grid.size_cells {
let mut neighbors = 0;
......@@ -230,9 +272,10 @@ fn conways_game_of_life(grid: &Grid) -> Grid {
(true, _) => false,
};
// Write back to the middle of the expanded area to avoid shift.
new.set_fill(
cx,
cy,
cx + expansion,
cy + expansion,
if alive {
Color32::BLACK
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment