boids-rs/src/boundary.rs

40 lines
982 B
Rust
Raw Normal View History

2024-09-02 06:20:10 +00:00
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;
}
}
}