Compare commits
5 Commits
0803b9d611
...
0009a83b1d
Author | SHA1 | Date |
---|---|---|
Zynh Ludwig | 0009a83b1d | |
Zynh Ludwig | 3a3cfccd7d | |
Zynh Ludwig | 901a936e18 | |
Zynh Ludwig | 5561191864 | |
Zynh Ludwig | a5400fafaa |
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue