Add RandomTable
for spawns.
This commit is contained in:
parent
20692919dc
commit
c41f5470c9
40
src/core.rs
40
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<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(
|
||||
config: Res<CoreConfig>,
|
||||
mut query: Query<(&Coordinates, &mut Transform)>,
|
||||
|
|
Loading…
Reference in New Issue
Block a user