Skip to content

Commit

Permalink
refactor: Rename components/traits to shorter names (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Dec 14, 2024
1 parent 1305482 commit c70143f
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 211 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Gonçalo Rica Pais da Silva <[email protected]>"]
edition = "2021"
repository = "https://github.com/Bluefinger/bevy_rand"
license = "MIT OR Apache-2.0"
version = "0.8.1"
version = "0.9.0"
rust-version = "1.76.0"

[workspace.dependencies]
Expand Down Expand Up @@ -49,7 +49,7 @@ wyrand = ["bevy_prng/wyrand"]
bevy_app.workspace = true
bevy_ecs.workspace = true
bevy_reflect.workspace = true
bevy_prng = { path = "bevy_prng", version = "0.8" }
bevy_prng = { path = "bevy_prng", version = "0.9" }

# others
getrandom = "0.2"
Expand All @@ -63,10 +63,10 @@ serde_derive = { workspace = true, optional = true }
# cannot be out of step with bevy_rand due to dependencies on traits
# and implementations between the two crates.
[target.'cfg(any())'.dependencies]
bevy_prng = { path = "bevy_prng", version = "=0.8" }
bevy_prng = { path = "bevy_prng", version = "=0.9" }

[dev-dependencies]
bevy_prng = { path = "bevy_prng", version = "0.8", features = ["rand_chacha", "wyrand"] }
bevy_prng = { path = "bevy_prng", version = "0.9", features = ["rand_chacha", "wyrand"] }
rand = "0.8"
ron = { version = "0.8.0", features = ["integer128"] }

Expand Down
4 changes: 4 additions & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ As the `wyrand` dependency has been updated and contains a breaking output chang
## Migrating from v0.7 to v0.8

`GlobalRngSeed` has been removed, instead being rolled into `GlobalEntropy`. This will allow better reflection tracking of the global rng source, and will allow for automatic reseeding without any custom system needing to be provided. Use the `reseed` method to reinstantiate the internal RNG source with the new seed, and `get_seed` to return a reference to the initial starting seed for the source. The serialized format of `GlobalEntropy` has changed and previously serialized instances are no longer compatible.

## Migrating from v0.8 to v0.9

`EntropyComponent` has been renamed to `Entropy`, and the trait `SeedableEntropySource` has been renamed to `EntropySource`. The change to `Entropy` also changes the `TypePath` definition, so this will change the serialised format of the component.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ DO **NOT** use `bevy_rand` for actual security purposes, as this requires much m

### Registering a PRNG for use with Bevy Rand

Before a PRNG can be used via `GlobalEntropy` or `EntropyComponent`, it must be registered via the plugin.
Before a PRNG can be used via `GlobalEntropy` or `Entropy`, it must be registered via the plugin.

```rust
use bevy_ecs::prelude::*;
Expand Down Expand Up @@ -69,7 +69,7 @@ fn print_random_value(mut rng: ResMut<GlobalEntropy<WyRand>>) {

### Forking RNGs

For seeding `EntropyComponent`s from a global source, it is best to make use of forking instead of generating the seed value directly. `GlobalEntropy` can only exist as a singular instance, so when forking normally, it will always fork as `EntropyComponent` instances.
For seeding `Entropy`s from a global source, it is best to make use of forking instead of generating the seed value directly. `GlobalEntropy` can only exist as a singular instance, so when forking normally, it will always fork as `Entropy` instances.

```rust
use bevy_ecs::prelude::*;
Expand All @@ -88,12 +88,12 @@ fn setup_source(mut commands: Commands, mut global: ResMut<GlobalEntropy<WyRand>
}
```

`EntropyComponent`s can be seeded/forked from other `EntropyComponent`s as well.
`Entropy`s can be seeded/forked from other `Entropy`s as well.

```rust
use bevy_ecs::prelude::*;
use bevy_prng::WyRand;
use bevy_rand::prelude::{EntropyComponent, ForkableRng};
use bevy_rand::prelude::{Entropy, ForkableRng};

#[derive(Component)]
struct Npc;
Expand All @@ -103,7 +103,7 @@ struct Source;

fn setup_npc_from_source(
mut commands: Commands,
mut q_source: Query<&mut EntropyComponent<WyRand>, (With<Source>, Without<Npc>)>,
mut q_source: Query<&mut Entropy<WyRand>, (With<Source>, Without<Npc>)>,
) {
let mut source = q_source.single_mut();
for _ in 0..2 {
Expand Down Expand Up @@ -132,7 +132,7 @@ fn setup_npc_from_source(

| `bevy` | `bevy_rand` |
| ------ | ------------ |
| v0.15 | v0.8 |
| v0.15 | v0.8 - v0.9 |
| v0.14 | v0.7 |
| v0.13 | v0.5 - v0.6 |
| v0.12 | v0.4 |
Expand All @@ -143,7 +143,7 @@ The versions of `rand_core`/`rand` that `bevy_rand` is compatible with is as fol

| `bevy_rand` | `rand_core` | `rand` |
| ------------ | ----------- | ------ |
| v0.1 -> v0.8 | v0.6 | v0.8 |
| v0.1 -> v0.9 | v0.6 | v0.8 |

## Migrations

Expand Down
2 changes: 1 addition & 1 deletion bevy_prng/src/chacha.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{newtype::newtype_prng, SeedableEntropySource};
use crate::{newtype::newtype_prng, EntropySource};

use bevy_reflect::{Reflect, ReflectFromReflect};
use rand_core::{RngCore, SeedableRng};
Expand Down
10 changes: 5 additions & 5 deletions bevy_prng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use xoshiro::*;
/// A marker trait to define the required trait bounds for a seedable PRNG to
/// integrate into `EntropyComponent` or `GlobalEntropy`. This is a sealed trait.
#[cfg(feature = "serialize")]
pub trait SeedableEntropySource:
pub trait EntropySource:
RngCore
+ SeedableRng<Seed: Typed>
+ Clone
Expand All @@ -54,7 +54,7 @@ pub trait SeedableEntropySource:
{
}

/// Marker trait for a suitable seed for [`SeedableEntropySource`]. This is an auto trait which will
/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
/// apply to all suitable types that meet the trait criteria.
#[cfg(feature = "serialize")]
pub trait EntropySeed:
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<
/// A marker trait to define the required trait bounds for a seedable PRNG to
/// integrate into `EntropyComponent` or `GlobalEntropy`. This is a sealed trait.
#[cfg(not(feature = "serialize"))]
pub trait SeedableEntropySource:
pub trait EntropySource:
RngCore
+ SeedableRng<Seed: Typed>
+ Clone
Expand All @@ -108,7 +108,7 @@ pub trait SeedableEntropySource:
}

#[cfg(not(feature = "serialize"))]
/// Marker trait for a suitable seed for [`SeedableEntropySource`]. This is an auto trait which will
/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
/// apply to all suitable types that meet the trait criteria.
pub trait EntropySeed:
Debug
Expand Down Expand Up @@ -145,5 +145,5 @@ impl<
mod private {
pub trait SealedSeedable {}

impl<T: super::SeedableEntropySource> SealedSeedable for T {}
impl<T: super::EntropySource> SealedSeedable for T {}
}
4 changes: 2 additions & 2 deletions bevy_prng/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ macro_rules! newtype_prng {
}
}

impl SeedableEntropySource for $newtype {}
impl EntropySource for $newtype {}
};
}

Expand Down Expand Up @@ -150,7 +150,7 @@ macro_rules! newtype_prng_remote {
}
}

impl SeedableEntropySource for $newtype {}
impl EntropySource for $newtype {}
};
}

Expand Down
2 changes: 1 addition & 1 deletion bevy_prng/src/pcg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{newtype::newtype_prng, SeedableEntropySource};
use crate::{newtype::newtype_prng, EntropySource};

use bevy_reflect::{Reflect, ReflectFromReflect};
use rand_core::{RngCore, SeedableRng};
Expand Down
2 changes: 1 addition & 1 deletion bevy_prng/src/wyrand.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{newtype::newtype_prng, SeedableEntropySource};
use crate::{newtype::newtype_prng, EntropySource};

use bevy_reflect::{Reflect, ReflectFromReflect};
use rand_core::{RngCore, SeedableRng};
Expand Down
2 changes: 1 addition & 1 deletion bevy_prng/src/xoshiro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
newtype::{newtype_prng, newtype_prng_remote},
SeedableEntropySource,
EntropySource,
};

use bevy_reflect::{reflect_remote, std_traits::ReflectDefault, Reflect, ReflectFromReflect};
Expand Down
11 changes: 4 additions & 7 deletions examples/turn_based_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_prng::ChaCha8Rng;
use bevy_rand::prelude::{EntropyComponent, EntropyPlugin, ForkableRng, GlobalEntropy};
use bevy_rand::prelude::{Entropy, EntropyPlugin, ForkableRng, GlobalEntropy};
use rand::prelude::{IteratorRandom, Rng};

#[derive(Component, PartialEq, Eq)]
Expand Down Expand Up @@ -97,7 +97,7 @@ fn setup_enemies(mut commands: Commands, mut rng: ResMut<GlobalEntropy<ChaCha8Rn
}

fn determine_attack_order(
mut q_entities: Query<(Entity, &mut EntropyComponent<ChaCha8Rng>), With<Kind>>,
mut q_entities: Query<(Entity, &mut Entropy<ChaCha8Rng>), With<Kind>>,
) -> Vec<Entity> {
// No matter the order of entities in the query, because they have their own RNG instance,
// it will always result in a deterministic output due to being seeded from a single global
Expand All @@ -121,7 +121,7 @@ fn attack_turn(
&Defense,
&Name,
&mut Health,
&mut EntropyComponent<ChaCha8Rng>,
&mut Entropy<ChaCha8Rng>,
)>,
) {
// Establish list of enemy entities for player to attack
Expand Down Expand Up @@ -173,10 +173,7 @@ fn attack_turn(
}

fn buff_entities(
mut q_entities: Query<
(&Name, &Buff, &mut Health, &mut EntropyComponent<ChaCha8Rng>),
With<Kind>,
>,
mut q_entities: Query<(&Name, &Buff, &mut Health, &mut Entropy<ChaCha8Rng>), With<Kind>>,
) {
// Query iteration order is not stable, but entities having their own RNG source side-steps this
// completely, so the result is always deterministic.
Expand Down
Loading

0 comments on commit c70143f

Please sign in to comment.