random spawn position
This commit is contained in:
parent
f6feef04e5
commit
ed8c679e57
3 changed files with 65 additions and 13 deletions
26
Cargo.lock
generated
26
Cargo.lock
generated
|
@ -1209,6 +1209,7 @@ name = "boids-rs"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2962,6 +2963,15 @@ dependencies = [
|
|||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "presser"
|
||||
version = "0.3.1"
|
||||
|
@ -3013,6 +3023,18 @@ version = "0.8.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
|
@ -3021,6 +3043,9 @@ name = "rand_core"
|
|||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "range-alloc"
|
||||
|
@ -4282,6 +4307,7 @@ version = "0.7.35"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
bevy = "0.14.1"
|
||||
rand = { version = "0.8.5", features = ["small_rng"] }
|
||||
|
||||
# Enable a small amount of optimization in the dev profile.
|
||||
[profile.dev]
|
||||
|
|
51
src/main.rs
51
src/main.rs
|
@ -1,15 +1,26 @@
|
|||
#![allow(unused)]
|
||||
|
||||
mod boid_mesh;
|
||||
mod boundary;
|
||||
|
||||
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
|
||||
use boid_mesh::BoidMesh;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.init_resource::<BoidMesh>()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain())
|
||||
.add_systems(Update, (move_boids, boundary::boid_border_teleport).chain())
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
(seperation, cohesion, alignment),
|
||||
move_boids,
|
||||
boundary::boid_border_teleport,
|
||||
)
|
||||
.chain(),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -43,7 +54,11 @@ struct BoidBundle {
|
|||
cache: BoidAccelerationBundle,
|
||||
}
|
||||
|
||||
fn seperation(mut boids: Query<(&Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
||||
fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
||||
|
||||
fn cohesion(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
||||
|
||||
fn alignment(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
||||
|
||||
fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: Res<Time>) {
|
||||
for (mut transform, velocity) in &mut boids {
|
||||
|
@ -56,18 +71,28 @@ fn setup(
|
|||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
boid_mesh: Res<BoidMesh>,
|
||||
) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
|
||||
let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50));
|
||||
|
||||
commands.spawn(BoidBundle {
|
||||
mesh: MaterialMesh2dBundle {
|
||||
mesh: boid_mesh.0.clone(),
|
||||
material: boid_color,
|
||||
transform: Transform::from_scale(Vec3::splat(2.5)),
|
||||
let mut small_rng = rand::rngs::SmallRng::from_entropy();
|
||||
let lower_bound = -100.;
|
||||
let upper_bound = 100.;
|
||||
|
||||
for i in 0..100 {
|
||||
let x = small_rng.gen_range(lower_bound..=upper_bound);
|
||||
let y = small_rng.gen_range(lower_bound..=upper_bound);
|
||||
|
||||
commands.spawn(BoidBundle {
|
||||
mesh: MaterialMesh2dBundle {
|
||||
mesh: boid_mesh.0.clone(),
|
||||
material: boid_color.clone(),
|
||||
transform: Transform::from_scale(Vec3::splat(2.5))
|
||||
.with_translation(Vec3::new(x, y, 0.)),
|
||||
..default()
|
||||
},
|
||||
velocity: Velocity(Vec2::Y * 100.),
|
||||
..default()
|
||||
},
|
||||
velocity: Velocity(Vec2::Y * 100.),
|
||||
..default()
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue