Compare commits

..

5 Commits

Author SHA1 Message Date
Zynh Ludwig 0009a83b1d move boundary code to module 2024-09-01 23:20:10 -07:00
Zynh Ludwig 3a3cfccd7d copy goes brr ig 2024-09-01 21:12:49 -07:00
Zynh Ludwig 901a936e18 boundary conditions 2024-09-01 21:04:22 -07:00
Zynh Ludwig 5561191864 vec2 velocity thank goodness 2024-09-01 16:03:54 -07:00
Zynh Ludwig a5400fafaa value tuning 2024-09-01 15:16:09 -07:00
3 changed files with 45 additions and 5 deletions

View File

@ -19,7 +19,7 @@ pub fn build_boid_mesh(mut meshes: ResMut<Assets<Mesh>>, mut boid_mesh: ResMut<B
pub fn make_boid_mesh() -> Mesh { pub fn make_boid_mesh() -> Mesh {
const TRIANGLE_HEIGHT: f32 = 10.; const TRIANGLE_HEIGHT: f32 = 10.;
const TRIANGLE_WIDTH: f32 = 5.; const TRIANGLE_WIDTH: f32 = 5.;
const STROKE_WIDTH: f32 = 0.25; const STROKE_WIDTH: f32 = 0.5;
const HALF_HEIGHT: f32 = TRIANGLE_HEIGHT / 2.0; const HALF_HEIGHT: f32 = TRIANGLE_HEIGHT / 2.0;
const HALF_WIDTH: f32 = TRIANGLE_WIDTH / 2.0; const HALF_WIDTH: f32 = TRIANGLE_WIDTH / 2.0;

39
src/boundary.rs Normal file
View 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;
}
}
}

View File

@ -1,4 +1,5 @@
mod boid_mesh; mod boid_mesh;
mod boundary;
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
use boid_mesh::BoidMesh; use boid_mesh::BoidMesh;
@ -8,7 +9,7 @@ fn main() {
.init_resource::<BoidMesh>() .init_resource::<BoidMesh>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain()) .add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain())
.add_systems(Update, move_boids) .add_systems(Update, (move_boids, boundary::boid_border_teleport).chain())
.run(); .run();
} }
@ -16,7 +17,7 @@ fn main() {
struct Boid; struct Boid;
#[derive(Component, Default)] #[derive(Component, Default)]
struct Velocity(Vec3); struct Velocity(Vec2);
#[derive(Bundle, Default)] #[derive(Bundle, Default)]
struct BoidBundle { struct BoidBundle {
@ -27,7 +28,7 @@ struct BoidBundle {
fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: Res<Time>) { fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: Res<Time>) {
for (mut transform, velocity) in &mut boids { for (mut transform, velocity) in &mut boids {
transform.translation += velocity.0 * time.delta_seconds(); transform.translation += velocity.0.extend(0.) * time.delta_seconds();
} }
} }
@ -47,7 +48,7 @@ fn setup(
transform: Transform::from_scale(Vec3::splat(2.5)), transform: Transform::from_scale(Vec3::splat(2.5)),
..default() ..default()
}, },
velocity: Velocity(Vec3::Y * 25.), velocity: Velocity(Vec2::Y * 100.),
..default() ..default()
}); });
} }