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

Improve automata slide.

parent 94898eba
No related branches found
No related tags found
No related merge requests found
......@@ -145,7 +145,7 @@ impl Grid {
let cell_width = 1.0 / self.size_cells as f32;
// Make sure neighboring cells overlap without gaps.
let tolerance = 0.001;
let tolerance = 0.0008;
for y in 0..self.size_cells {
let y_coord = y as f32 / self.size_cells as f32;
for x in 0..self.size_cells {
......
......@@ -40,8 +40,8 @@ enum AutomataState {
},
/// Simulation.
Life {
/// Whether we started expanding the grid yet.
expanding: bool,
/// Whether we inverted the color and turned on fading yet.
rule_changed: bool,
/// Conway iteration counter.
iterations: usize,
},
......@@ -95,17 +95,17 @@ impl Slide for Automata {
*rules = Some(ctx.input().time)
} else {
self.state = AutomataState::Life {
expanding: false,
rule_changed: false,
iterations: 0,
};
}
}
AutomataState::Life { expanding, .. } => {
if *expanding {
AutomataState::Life { rule_changed, .. } => {
if !*rule_changed {
*rule_changed = true;
} else {
// Done with the slide.
return true;
} else {
*expanding = true;
}
}
}
......@@ -146,17 +146,14 @@ impl Slide for Automata {
}
}
AutomataState::Life {
expanding,
rule_changed,
iterations,
} => {
// Iterate grid ~5 times a second.
if self.governor.ready(ui.ctx().input().time, 0.2) {
// Schedule some special events at some iteration counts.
let expanding = *iterations >= 10;
match *iterations {
10 => {
// Automatically begin expansion.
*expanding = true;
}
20 => {
for y in 4..self.life.size_cells - 4 {
// Line on the left.
......@@ -185,8 +182,17 @@ impl Slide for Automata {
}
// 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);
let expansion = (expanding && self.life.size_cells < 64) as usize;
// Implement rule changes.
let (background_color, alive_color, stroke_color, fade) = if *rule_changed {
(Color32::BLACK, Color32::WHITE, Color32::TRANSPARENT, true)
} else {
(Color32::TRANSPARENT, Color32::BLACK, Color32::GRAY, false)
};
self.life.background = background_color;
self.life.stroke = stroke_color;
self.life = conways_game_of_life(&self.life, expansion, alive_color, fade);
*iterations += 1;
}
......@@ -251,7 +257,7 @@ impl Slide for Automata {
/// Run one iteration of Conway's Game of Life on the grid, producing a new 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 {
fn conways_game_of_life(grid: &Grid, expansion: usize, alive_color: Color32, fade: bool) -> Grid {
let mut new = grid.clone();
new.clear_fill();
......@@ -271,15 +277,15 @@ fn conways_game_of_life(grid: &Grid, expansion: usize) -> Grid {
// Don't consider self.
continue;
}
if grid.fill(x, y).a() > 0 {
if grid.fill(x, y).is_opaque() {
neighbors += 1;
}
}
}
let mut alive = grid.fill(cx, cy).a() > 0;
let was_alive = grid.fill(cx, cy).is_opaque();
alive = match (alive, neighbors) {
let alive = match (was_alive, neighbors) {
(false, 3) => true,
(false, _) => false,
(true, 2) | (true, 3) => true,
......@@ -291,9 +297,13 @@ fn conways_game_of_life(grid: &Grid, expansion: usize) -> Grid {
cx + expansion,
cy + expansion,
if alive {
Color32::BLACK
} else {
alive_color
} else if !fade {
Color32::TRANSPARENT
} else {
// Fade out gradually.
let previous_alpha = grid.fill(cx, cy).a();
set_alpha(alive_color, previous_alpha as f32 / (255.0 * 2.0))
},
);
}
......
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