boids-rs/src/boundary.rs

42 lines
1.0 KiB
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.;
2024-09-02 07:39:14 +00:00
const PADDING: f32 = 50.;
let left_bound = -half_width - PADDING;
let right_bound = half_width + PADDING;
let top_bound = half_height + PADDING;
let bottom_bound = -half_height - PADDING;
2024-09-02 06:20:10 +00:00
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;
}
}
}