assets/models/animal-bunny.glb
+0
-0
diff --git a/assets/models/animal-bunny.glb b/assets/models/animal-bunny.glb
new file mode 100644
index 0000000..e0cc86c
Binary files /dev/null and b/assets/models/animal-bunny.glb differ
docs/screenshot.png
+0
-0
diff --git a/docs/screenshot.png b/docs/screenshot.png
index 1966d3b..d5c50a1 100644
Binary files a/docs/screenshot.png and b/docs/screenshot.png differ
src/bunny.rs
+63
-0
diff --git a/src/bunny.rs b/src/bunny.rs
new file mode 100644
index 0000000..8351de9
@@ -0,0 +1,63 @@
use bevy::{
app::{App, Startup, Update},
asset::AssetServer,
ecs::{
component::Component,
query::{With, Without},
system::{Commands, Query, Res, Single},
},
gltf::GltfAssetLabel,
math::Vec3,
scene::SceneRoot,
time::Time,
transform::components::Transform,
};
use crate::dog::Dog;
pub trait BunnySystems {
fn add_bunny_systems(&mut self) -> &mut Self;
}
impl BunnySystems for App {
fn add_bunny_systems(&mut self) -> &mut Self {
self.add_systems(Startup, setup)
.add_systems(Update, control)
}
}
#[derive(Component, Default)]
struct Bunny;
const DETECTION_DISTANCE: f32 = 2.5;
const SPEED: f32 = 10.0;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let model = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/animal-bunny.glb"));
commands.spawn((
SceneRoot(model.clone()),
Transform::from_xyz(5.0, 0.0, 0.0),
Bunny::default(),
));
commands.spawn((
SceneRoot(model.clone()),
Transform::from_xyz(-5.0, 0.0, 0.0),
Bunny::default(),
));
}
fn control(
time: Res<Time>,
dog: Single<&Transform, (With<Dog>, Without<Bunny>)>,
bunnies: Query<&mut Transform, With<Bunny>>,
) {
for mut bunny in bunnies {
let direction = bunny.translation - dog.translation;
if direction.length_squared() < DETECTION_DISTANCE * DETECTION_DISTANCE {
bunny.look_to(-direction, Vec3::Y);
bunny.translation += direction.normalize() * SPEED * time.delta_secs();
}
}
}
src/dog.rs
+1
-1
diff --git a/src/dog.rs b/src/dog.rs
index 00f86ab..aadfba6 100644
@@ -27,7 +27,7 @@ impl DogSystems for App {
}
#[derive(Component, Default)]
struct Dog;
pub struct Dog;
const SPEED: f32 = 10.0;
src/main.rs
+3
-0
diff --git a/src/main.rs b/src/main.rs
index c49187a..e698c99 100644
@@ -1,3 +1,4 @@
mod bunny;
mod dog;
use bevy::{
@@ -10,6 +11,7 @@ use bevy::{
transform::components::Transform,
};
use crate::bunny::BunnySystems;
use crate::dog::DogSystems;
fn main() {
@@ -17,6 +19,7 @@ fn main() {
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_dog_systems()
.add_bunny_systems()
.run();
}