Achieve the final result of entirely generic!!
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user