move boundary code to module
This commit is contained in:
parent
3a3cfccd7d
commit
0009a83b1d
2 changed files with 41 additions and 35 deletions
39
src/boundary.rs
Normal file
39
src/boundary.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use super::Boid;
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub(crate) fn boid_border_teleport(
|
||||
mut boids: Query<&mut Transform, With<Boid>>,
|
||||
windows: Query<&Window>,
|
||||
) {
|
||||
let window = windows.get_single().unwrap();
|
||||
let width = window.resolution.width();
|
||||
let height = window.resolution.height();
|
||||
|
||||
let half_width = width / 2.;
|
||||
let half_height = height / 2.;
|
||||
|
||||
let left_bound = -half_width;
|
||||
let right_bound = half_width;
|
||||
let top_bound = half_height;
|
||||
let bottom_bound = -half_height;
|
||||
|
||||
for mut boid_transform in &mut boids {
|
||||
let translation = &mut boid_transform.translation;
|
||||
|
||||
if translation.y > top_bound {
|
||||
translation.y = bottom_bound;
|
||||
}
|
||||
|
||||
if translation.y < bottom_bound {
|
||||
translation.y = top_bound;
|
||||
}
|
||||
|
||||
if translation.x < left_bound {
|
||||
translation.x = right_bound;
|
||||
}
|
||||
|
||||
if translation.x > right_bound {
|
||||
translation.x = left_bound;
|
||||
}
|
||||
}
|
||||
}
|
37
src/main.rs
37
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
mod boid_mesh;
|
||||
mod boundary;
|
||||
|
||||
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
|
||||
use boid_mesh::BoidMesh;
|
||||
|
@ -8,7 +9,7 @@ fn main() {
|
|||
.init_resource::<BoidMesh>()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain())
|
||||
.add_systems(Update, (move_boids, boid_border_teleport).chain())
|
||||
.add_systems(Update, (move_boids, boundary::boid_border_teleport).chain())
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -31,40 +32,6 @@ fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: R
|
|||
}
|
||||
}
|
||||
|
||||
fn boid_border_teleport(mut boids: Query<&mut Transform, With<Boid>>, windows: Query<&Window>) {
|
||||
let window = windows.get_single().unwrap();
|
||||
let width = window.resolution.width();
|
||||
let height = window.resolution.height();
|
||||
|
||||
let half_width = width / 2.;
|
||||
let half_height = height / 2.;
|
||||
|
||||
let left_bound = -half_width;
|
||||
let right_bound = half_width;
|
||||
let top_bound = half_height;
|
||||
let bottom_bound = -half_height;
|
||||
|
||||
for mut boid_transform in &mut boids {
|
||||
let translation = &mut boid_transform.translation;
|
||||
|
||||
if translation.y > top_bound {
|
||||
translation.y = bottom_bound;
|
||||
}
|
||||
|
||||
if translation.y < bottom_bound {
|
||||
translation.y = top_bound;
|
||||
}
|
||||
|
||||
if translation.x < left_bound {
|
||||
translation.x = right_bound;
|
||||
}
|
||||
|
||||
if translation.x > right_bound {
|
||||
translation.x = left_bound;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
|
|
Loading…
Reference in a new issue