diff --git a/src/core.rs b/src/core.rs index d716209..050db9b 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,6 +6,7 @@ use std::{ use bevy::{core::FloatOrd, prelude::*, transform::TransformSystem}; use derive_more::{Deref, DerefMut}; +use rand::prelude::*; #[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)] #[reflect(Component)] @@ -440,6 +441,45 @@ pub struct Pool { pub max: T, } +#[derive(Clone, Debug)] +pub struct RandomTable(Vec) +where + T: Clone; + +impl RandomTable +where + T: Clone, +{ + pub fn add(&mut self, value: T, weight: u32) -> &mut Self { + for _ in 0..weight { + self.0.push(value.clone()); + } + self + } +} + +impl Default for RandomTable +where + T: Clone, +{ + fn default() -> Self { + Self(vec![]) + } +} + +impl Iterator for RandomTable +where + T: Clone, +{ + type Item = T; + + fn next(&mut self) -> Option { + let mut rng = thread_rng(); + self.0.shuffle(&mut rng); + self.0.get(0).cloned() + } +} + fn copy_coordinates_to_transform( config: Res, mut query: Query<(&Coordinates, &mut Transform)>,