Achieve the final result of entirely generic!!

This commit is contained in:
2023-03-05 10:59:08 +00:00
parent 1f5c94cb82
commit 15c2edde67
12 changed files with 219 additions and 131 deletions
+29
View File
@@ -0,0 +1,29 @@
use rand::Rng;
use crate::system::{GriddedPosition, Position, Storage};
pub trait Sticker<P: Position> {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool;
}
pub struct SimpleSticking;
pub struct ProbabilisticSticking {
pub stick_probability: f32
}
impl<P: GriddedPosition> Sticker<P> for SimpleSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour))
}
}
impl<P: GriddedPosition> Sticker<P> for ProbabilisticSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour) && rng.gen_range(0.0f32..=1.0) < self.stick_probability)
}
}