diff --git a/CHANGELOG.md b/CHANGELOG.md index 73f9575c4c263..8b7103327fb4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -92,6 +92,7 @@ current changes on git with [previous release tags][git_tag_comparison]. - [Added `set_minimized` and `set_position` to `Window`][1292] - [Example for 2D Frustum Culling][1503] - [Add remove resource to commands][1478] +- [Random number generation][2355] ### Changed @@ -246,6 +247,7 @@ current changes on git with [previous release tags][git_tag_comparison]. [1703]: https://github.com/bevyengine/bevy/pull/1703 [1728]: https://github.com/bevyengine/bevy/pull/1728 [1762]: https://github.com/bevyengine/bevy/pull/1762 +[2355]: https://github.com/bevyengine/bevy/pull/2355 ## Version 0.4.0 (2020-12-19) diff --git a/Cargo.toml b/Cargo.toml index 9e8551ffc3daf..0a90b93908712 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -181,6 +181,10 @@ path = "examples/3d/z_sort_debug.rs" name = "custom_loop" path = "examples/app/custom_loop.rs" +[[example]] +name = "custom_rng_seeds" +path = "examples/app/custom_rng_seeds.rs" + [[example]] name = "drag_and_drop" path = "examples/app/drag_and_drop.rs" diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 107cead7be36d..9fcf386009f48 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -12,6 +12,9 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT" keywords = ["bevy"] +[features] +default = ["rng"] +rng = ["getrandom", "rand"] [dependencies] # bevy @@ -25,3 +28,5 @@ bevy_utils = { path = "../bevy_utils", version = "0.5.0" } # other bytemuck = "1.5" +getrandom = { version = "0.2", features = ["js"], optional = true } +rand = { version = "0.8", features = ["getrandom", "small_rng", "std_rng"], optional = true } diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 75fd87adb5ebb..bbef8cb887797 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -2,6 +2,8 @@ mod bytes; mod float_ord; mod label; mod name; +#[cfg(feature = "rng")] +mod rng; mod task_pool_options; mod time; @@ -9,10 +11,18 @@ pub use bytes::*; pub use float_ord::*; pub use label::*; pub use name::*; +#[cfg(feature = "rng")] +pub use rng::*; pub use task_pool_options::DefaultTaskPoolOptions; pub use time::*; pub mod prelude { + #[cfg(feature = "rng")] + #[doc(hidden)] + pub use crate::{ + CryptoRng, DefaultRngOptions, InsecureRng, InsecureSeed, Rng, RngCore, SecureRng, + SecureSeed, SliceRandom, + }; #[doc(hidden)] pub use crate::{DefaultTaskPoolOptions, EntityLabels, Labels, Name, Time, Timer}; } @@ -46,6 +56,16 @@ impl Plugin for CorePlugin { .unwrap_or_else(DefaultTaskPoolOptions::default) .create_default_pools(app.world_mut()); + #[cfg(feature = "rng")] + { + // Setup the default bevy random number generators + app.world_mut() + .get_resource::() + .cloned() + .unwrap_or_else(DefaultRngOptions::default) + .create_default_rngs(app.world_mut()); + } + app.init_resource::