📄
src/bunny.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::{ops::DerefMut, time::Duration}; use bevy::{ app::{App, Startup, Update}, asset::{AssetServer, Assets}, ecs::{ component::Component, entity::Entity, query::{With, Without}, system::{Commands, Query, Res, ResMut, Single}, }, gltf::GltfAssetLabel, math::{Vec3, primitives::Cuboid}, mesh::{Mesh, Mesh3d, MeshBuilder, Meshable}, scene::SceneRoot, time::Time, transform::components::Transform, }; use crate::{dog::Dog, obstacles::Obstacles}; 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, (calculate, jump)) } } #[derive(Component)] struct Bunny; #[derive(Component)] enum JumpState { Jumping { from: Vec3, to: Vec3 }, Cooldown { ready: Duration }, } const DETECTION_DISTANCE: f32 = 3.0; const SCARED_JUMP_DISTANCE: f32 = 2.0; const JUMP_HEIGHT: f32 = 1.0; const JUMP_COOLDOWN: f32 = 0.05; const DESIRED_SPEED: f32 = 10.0; const SPEED: f32 = SCARED_JUMP_DISTANCE / (SCARED_JUMP_DISTANCE / DESIRED_SPEED - JUMP_COOLDOWN); fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut meshes: ResMut<Assets<Mesh>>) { let model = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/cube-pets/animal-bunny.glb")); let mesh = meshes.add(Cuboid::from_length(2.0).mesh().build()); commands.spawn(( SceneRoot(model.clone()), Transform::from_xyz(4.0, 0.0, 0.0), Mesh3d(mesh.clone()), Bunny, )); commands.spawn(( SceneRoot(model.clone()), Transform::from_xyz(-4.0, 0.0, 0.0), Mesh3d(mesh.clone()), Bunny, )); } type IdleBunniesQuery<'w, 's, 't> = Query<'w, 's, (Entity, &'t mut Transform), (With<Bunny>, Without<JumpState>)>; fn calculate( mut commands: Commands, dog: Single<&Transform, (With<Dog>, Without<Bunny>)>, mut bunnies: IdleBunniesQuery, mut obstacles: Obstacles, ) { bunnies.iter_mut().for_each(|(bunny, mut bunny_transform)| { calculate_next_move( &mut commands, &dog, &mut bunny_transform, bunny, &mut obstacles, ); }); } fn calculate_next_move( commands: &mut Commands, dog: &Transform, bunny_transform: &mut Transform, bunny: Entity, obstacles: &mut Obstacles, ) { let direction = bunny_transform.translation - dog.translation; if direction.length_squared() > DETECTION_DISTANCE * DETECTION_DISTANCE { return; } let dog_direction = direction.with_y(0.0).normalize(); let Some(to) = obstacles.avoid( bunny_transform.translation, bunny_transform.translation + dog_direction * SCARED_JUMP_DISTANCE, ) else { return; }; commands.entity(bunny).insert(JumpState::Jumping { from: bunny_transform.translation, to, }); bunny_transform.look_to(-(to - bunny_transform.translation), Vec3::Y); } fn jump( mut commands: Commands, time: Res<Time>, mut bunnies: Query<(Entity, &mut Transform, &mut JumpState), With<Bunny>>, ) { bunnies.iter_mut().for_each( |(bunny, mut bunny_transform, ref mut jump_state)| match jump_state.deref_mut() { JumpState::Jumping { from, to } => { let from = *from; let to = *to; animate_jumping( &time, &mut bunny_transform, jump_state.deref_mut(), from, to, ) } JumpState::Cooldown { ready } => { let ready = *ready; cooldown(&mut commands, &time, bunny, ready); } }, ); } fn animate_jumping( time: &Time, bunny_transform: &mut Transform, jump_state: &mut JumpState, from: Vec3, to: Vec3, ) { let current = bunny_transform.translation.with_y(to.y); let direction = to - current; let delta = SPEED * time.delta_secs(); let (direction, length) = direction.normalize_and_length(); if length <= delta { bunny_transform.translation = to; *jump_state = JumpState::Cooldown { ready: time.elapsed() + Duration::from_secs_f32(JUMP_COOLDOWN), }; return; } let next = current + direction.normalize() * delta; let total_distance = from.distance(to); bunny_transform.translation = next + Vec3::Y * JUMP_HEIGHT * (std::f32::consts::PI * from.distance(next) / total_distance).sin(); } fn cooldown(commands: &mut Commands, time: &Time, bunny: Entity, ready: Duration) { if time.elapsed() >= ready { commands.entity(bunny).remove::<JumpState>(); } }