Update to Bevy 0.16
Also update RNG handling
This commit is contained in:
parent
8a1064138f
commit
b5792648eb
4 changed files with 850 additions and 556 deletions
74
src/main.rs
74
src/main.rs
|
@ -13,9 +13,10 @@ use bevy::{
|
|||
input::common_conditions::input_pressed,
|
||||
prelude::*,
|
||||
};
|
||||
use bevy_rand::prelude::{EntropyPlugin, GlobalEntropy, WyRand};
|
||||
use clap::Parser;
|
||||
use rand::{Rng, SeedableRng, rng};
|
||||
use rand_chacha::ChaCha8Rng;
|
||||
use rand::Rng;
|
||||
use wyrand::WyRand as LocalRng;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about)]
|
||||
|
@ -28,15 +29,22 @@ fn main() -> AppExit {
|
|||
run_app(AppSettings::parse())
|
||||
}
|
||||
|
||||
fn hash_string(string: &String) -> u64 {
|
||||
let mut state = DefaultHasher::new();
|
||||
string.hash(&mut state);
|
||||
state.finish()
|
||||
}
|
||||
|
||||
const BALL_SIZES: Range<f32> = 10.0..25.0;
|
||||
const DIMENSION_SIZES: Range<f32> = 250.0..1000.0;
|
||||
|
||||
#[derive(Clone, Resource)]
|
||||
struct Seed(u64);
|
||||
|
||||
impl From<String> for Seed {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value.parse::<u64>().unwrap_or_else(|_| {
|
||||
let mut state = DefaultHasher::new();
|
||||
value.hash(&mut state);
|
||||
state.finish()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct PlayableArea(f32, f32);
|
||||
|
||||
|
@ -44,13 +52,7 @@ struct PlayableArea(f32, f32);
|
|||
struct PlayerSize(f32);
|
||||
|
||||
fn run_app(settings: AppSettings) -> AppExit {
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(
|
||||
settings
|
||||
.seed
|
||||
.parse::<u64>()
|
||||
.unwrap_or_else(|_| hash_string(&settings.seed)),
|
||||
);
|
||||
|
||||
let seed = Seed::from(settings.seed);
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins.set(WindowPlugin {
|
||||
|
@ -63,18 +65,22 @@ fn run_app(settings: AppSettings) -> AppExit {
|
|||
..default()
|
||||
}),
|
||||
PhysicsPlugins::default().with_length_unit(50.0),
|
||||
EntropyPlugin::<WyRand>::with_seed(seed.0.to_le_bytes()), // TODO: Tried to make and Into<[u8; 8]> for this but it wants to consume the value
|
||||
))
|
||||
.add_systems(Startup, (setup_balls, setup_ui, setup_player, setup_walls))
|
||||
.add_systems(
|
||||
Startup,
|
||||
(
|
||||
setup_ui,
|
||||
setup_pseudo_random,
|
||||
(setup_player, setup_walls, setup_balls).after(setup_pseudo_random),
|
||||
),
|
||||
)
|
||||
.add_systems(
|
||||
Update,
|
||||
(move_player, quit.run_if(input_pressed(KeyCode::KeyQ))),
|
||||
)
|
||||
.insert_resource(Gravity(Vector::ZERO))
|
||||
.insert_resource(PlayerSize(rng.random_range(BALL_SIZES)))
|
||||
.insert_resource(PlayableArea(
|
||||
rng.random_range(DIMENSION_SIZES),
|
||||
rng.random_range(DIMENSION_SIZES),
|
||||
))
|
||||
.insert_resource(seed)
|
||||
.run()
|
||||
}
|
||||
|
||||
|
@ -83,11 +89,11 @@ fn setup_ui(mut commands: Commands) {
|
|||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(Collider, Mesh2d, MeshMaterial2d<ColorMaterial>, Restitution(|| Restitution::new(1.0)), RigidBody, TransformInterpolation, Transform)]
|
||||
#[require(Collider, Mesh2d, MeshMaterial2d<ColorMaterial>, Restitution = Restitution::new(1.0), RigidBody, TransformInterpolation, Transform)]
|
||||
struct GameObject;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(GameObject, RigidBody(|| RigidBody::Dynamic))]
|
||||
#[require(GameObject, RigidBody = RigidBody::Dynamic)]
|
||||
struct Ball;
|
||||
|
||||
fn setup_balls(
|
||||
|
@ -96,7 +102,7 @@ fn setup_balls(
|
|||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
region: Res<PlayableArea>,
|
||||
) {
|
||||
let mut random = rng();
|
||||
let mut random = LocalRng::new(Default::default());
|
||||
for _ in 0..50 {
|
||||
let circle = Circle::new(random.random_range(BALL_SIZES));
|
||||
commands.spawn((
|
||||
|
@ -118,7 +124,7 @@ fn setup_balls(
|
|||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(Ball, RigidBody(|| RigidBody::Dynamic))]
|
||||
#[require(Ball)]
|
||||
struct Player;
|
||||
|
||||
fn setup_player(
|
||||
|
@ -136,8 +142,16 @@ fn setup_player(
|
|||
));
|
||||
}
|
||||
|
||||
fn setup_pseudo_random(mut commands: Commands, mut rng: GlobalEntropy<WyRand>) {
|
||||
commands.insert_resource(PlayerSize(rng.random_range(BALL_SIZES)));
|
||||
commands.insert_resource(PlayableArea(
|
||||
rng.random_range(DIMENSION_SIZES),
|
||||
rng.random_range(DIMENSION_SIZES),
|
||||
));
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(GameObject, RigidBody(|| RigidBody::Static))]
|
||||
#[require(GameObject, RigidBody = RigidBody::Static)]
|
||||
struct Wall;
|
||||
|
||||
fn setup_walls(
|
||||
|
@ -175,10 +189,10 @@ fn move_player(
|
|||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut player: Query<&mut LinearVelocity, With<Player>>,
|
||||
) {
|
||||
) -> Result {
|
||||
let acceleration = 500.0;
|
||||
|
||||
let mut velocity = player.single_mut();
|
||||
let mut velocity = player.single_mut()?;
|
||||
let delta_time = time.delta_secs();
|
||||
|
||||
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
|
||||
|
@ -193,8 +207,10 @@ fn move_player(
|
|||
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
|
||||
velocity.x += acceleration * delta_time;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn quit(mut exit: EventWriter<AppExit>) {
|
||||
exit.send(AppExit::Success);
|
||||
exit.write(AppExit::Success);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue