Compare commits
2 commits
79cd0a8fe4
...
6d86f6fdef
Author | SHA1 | Date | |
---|---|---|---|
6d86f6fdef | |||
c435d96f33 |
2 changed files with 38 additions and 6 deletions
|
@ -20,7 +20,7 @@ pub struct Chunk {
|
|||
pub struct Rule {
|
||||
pub from: RulePattern,
|
||||
pub to: RulePattern,
|
||||
// enabled: bool
|
||||
pub enabled: bool,
|
||||
// probability: u8
|
||||
// flip:
|
||||
// rotate:
|
||||
|
@ -29,6 +29,7 @@ pub struct Rule {
|
|||
impl Rule {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
from: RulePattern::new(),
|
||||
to: RulePattern::new(),
|
||||
}
|
||||
|
@ -72,6 +73,7 @@ impl Dish {
|
|||
|
||||
rules: vec![
|
||||
Rule {
|
||||
enabled: true,
|
||||
from: RulePattern {
|
||||
width: 1,
|
||||
height: 2,
|
||||
|
@ -84,6 +86,7 @@ impl Dish {
|
|||
},
|
||||
},
|
||||
Rule {
|
||||
enabled: true,
|
||||
from: RulePattern {
|
||||
width: 2,
|
||||
height: 2,
|
||||
|
@ -96,6 +99,7 @@ impl Dish {
|
|||
},
|
||||
},
|
||||
Rule {
|
||||
enabled: true,
|
||||
from: RulePattern {
|
||||
width: 2,
|
||||
height: 2,
|
||||
|
@ -112,10 +116,22 @@ impl Dish {
|
|||
}
|
||||
|
||||
pub fn fire_blindly(&mut self) {
|
||||
assert!(!self.rules.is_empty());
|
||||
if self.rules.is_empty() {
|
||||
return;
|
||||
}
|
||||
let x = random::<usize>() % CHUNK_SIZE;
|
||||
let y = random::<usize>() % CHUNK_SIZE;
|
||||
let rule = random::<usize>() % self.rules.len();
|
||||
let enabled_rules = self
|
||||
.rules
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, r)| r.enabled.then_some(i))
|
||||
.collect::<Vec<_>>();
|
||||
if enabled_rules.is_empty() {
|
||||
return;
|
||||
}
|
||||
let rule = random::<usize>() % enabled_rules.len();
|
||||
let rule = enabled_rules[rule];
|
||||
self.fire_rule(rule, x, y);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ impl UScope {
|
|||
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
|
||||
Self {
|
||||
dish: Dish::new(),
|
||||
speed: 100,
|
||||
speed: 250,
|
||||
brush: Cell(1),
|
||||
celltypes: vec![
|
||||
CellData::new("air", 0, 0, 0),
|
||||
|
@ -51,7 +51,10 @@ impl eframe::App for UScope {
|
|||
}
|
||||
SidePanel::left("left_panel").show(ctx, |ui| {
|
||||
ui.heading("Simulation");
|
||||
ui.label("speed");
|
||||
ui.add(Slider::new(&mut self.speed, 0..=5000));
|
||||
ui.separator();
|
||||
|
||||
ui.heading("Cells");
|
||||
for (i, cell) in self.celltypes.iter_mut().enumerate() {
|
||||
ui.horizontal(|ui| {
|
||||
|
@ -61,6 +64,7 @@ impl eframe::App for UScope {
|
|||
ui.color_edit_button_srgba(&mut cell.color);
|
||||
});
|
||||
}
|
||||
|
||||
if ui.button("add cell").clicked() {
|
||||
let h = random::<f32>();
|
||||
let s = random::<f32>() * 0.5 + 0.5;
|
||||
|
@ -69,9 +73,19 @@ impl eframe::App for UScope {
|
|||
let name = format!("cell #{}", self.celltypes.len());
|
||||
self.celltypes.push(CellData { name, color })
|
||||
}
|
||||
ui.separator();
|
||||
|
||||
ui.heading("Rules");
|
||||
for rule in &mut self.dish.rules {
|
||||
let mut to_remove = None;
|
||||
for (i, rule) in self.dish.rules.iter_mut().enumerate() {
|
||||
rule_editor(ui, rule, &self.celltypes);
|
||||
if ui.button("delete").clicked() {
|
||||
to_remove = Some(i);
|
||||
}
|
||||
ui.separator();
|
||||
}
|
||||
if let Some(i) = to_remove {
|
||||
self.dish.rules.remove(i);
|
||||
}
|
||||
if ui.button("add rule").clicked() {
|
||||
self.dish.rules.push(Rule::new());
|
||||
|
@ -108,8 +122,10 @@ fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) {
|
|||
}
|
||||
|
||||
const CSIZE: f32 = 24.;
|
||||
const OUTLINE: (f32, Color32) = (1., Color32::GRAY);
|
||||
const OUTLINE: (f32, Color32) = (3., Color32::GRAY);
|
||||
fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
|
||||
ui.checkbox(&mut rule.enabled, "enable rule");
|
||||
|
||||
let cells_y = rule.from.height();
|
||||
let cells_x = rule.from.width();
|
||||
let margin = 8.;
|
||||
|
|
Loading…
Reference in a new issue