Add RandomTable for spawns.

This commit is contained in:
Nolan Darilek 2021-06-02 11:40:40 -05:00
parent 20692919dc
commit c41f5470c9

View File

@ -6,6 +6,7 @@ use std::{
use bevy::{core::FloatOrd, prelude::*, transform::TransformSystem}; use bevy::{core::FloatOrd, prelude::*, transform::TransformSystem};
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use rand::prelude::*;
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)] #[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)]
#[reflect(Component)] #[reflect(Component)]
@ -440,6 +441,45 @@ pub struct Pool<T> {
pub max: T, pub max: T,
} }
#[derive(Clone, Debug)]
pub struct RandomTable<T>(Vec<T>)
where
T: Clone;
impl<T> RandomTable<T>
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<T> Default for RandomTable<T>
where
T: Clone,
{
fn default() -> Self {
Self(vec![])
}
}
impl<T> Iterator for RandomTable<T>
where
T: Clone,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let mut rng = thread_rng();
self.0.shuffle(&mut rng);
self.0.get(0).cloned()
}
}
fn copy_coordinates_to_transform( fn copy_coordinates_to_transform(
config: Res<CoreConfig>, config: Res<CoreConfig>,
mut query: Query<(&Coordinates, &mut Transform)>, mut query: Query<(&Coordinates, &mut Transform)>,