From 0009a83b1d9179313be6ea8203f218f13fdca42c Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Sun, 1 Sep 2024 23:20:10 -0700 Subject: [PATCH] move boundary code to module --- src/boundary.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 37 ++----------------------------------- 2 files changed, 41 insertions(+), 35 deletions(-) create mode 100644 src/boundary.rs diff --git a/src/boundary.rs b/src/boundary.rs new file mode 100644 index 0000000..b181b9a --- /dev/null +++ b/src/boundary.rs @@ -0,0 +1,39 @@ +use super::Boid; +use bevy::prelude::*; + +pub(crate) fn boid_border_teleport( + mut boids: Query<&mut Transform, With>, + 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; + } + } +} diff --git a/src/main.rs b/src/main.rs index c6dca12..6c2c357 100644 --- a/src/main.rs +++ b/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::() .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>, time: R } } -fn boid_border_teleport(mut boids: Query<&mut Transform, With>, 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>,