generated from bevyengine/bevy_github_ci_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
399 lines (356 loc) · 13.2 KB
/
mod.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use crate::creatures::{Creature, TypeCreature};
use bevy::prelude::*;
use bevy::utils::HashMap;
use bevy_inspector_egui::Inspectable;
use std::borrow::BorrowMut;
use std::time::Duration;
pub struct AnimationHandler;
impl Plugin for AnimationHandler {
fn build(&self, app: &mut App) {
app.insert_resource::<VecSceneHandle>(Default::default())
//.register_inspectable::<AnimationEntityLink>()
.add_event::<ChangeAnimation>()
.add_event::<AddAnimation>()
.add_event::<RemoveAnimation>()
.add_system(link_animations)
.add_system(start_animation.after(link_animations))
.add_system_to_stage(CoreStage::PostUpdate, add_animation)
.add_system_to_stage(CoreStage::PostUpdate, remove_animation)
.add_system_to_stage(CoreStage::PostUpdate, update_animation.after(add_animation))
.add_system_to_stage(CoreStage::PostUpdate, checker_animation_duration.after(update_animation))
//.add_system(inspect_animation_clip)
;
}
}
/// Event utilisé par change_animation() pour changer d'animation
/// # Examples
/// ```
/// event_writer.send(
/// ChangeAnimation{
/// target: entity,
/// index: number as usize,
/// repeat: true,
/// }
/// );
/// ```
#[derive(Debug)]
pub struct ChangeAnimation {
pub(crate) target: u32,
pub(crate) index: usize,
pub(crate) repeat: bool,
}
/// Event utilisé pour ajouter une animation
pub struct AddAnimation {
pub scene_handler: SceneHandle,
pub target: Option<u32>,
pub start_animation: bool,
}
/// Event utilisé pour retirer une animation
pub struct RemoveAnimation {
pub entity_id: u32,
}
#[derive(Component)]
pub struct TagPlayerScene;
/// Ressource qui contient un vecteur de SceneHandle
/// qui définit tous les animations des créatures
/// Ajouté au world: app.insert_resource::<VecSceneHandle>(Default::default())
#[derive(Default)]
pub struct VecSceneHandle(pub Vec<SceneHandle>);
/// HashMap contenant un tuple: (duration_animation, handle_animation)
/// La Hashmap est créée dans la fonction spawn de chaque créature
/// Updated par add_animation et remove_animation
/// Utilisée par update_animation
#[derive(Clone, Debug)]
pub struct HashMapAnimationClip(HashMap<usize, (f32, Handle<AnimationClip>)>);
impl HashMapAnimationClip {
pub fn get_pair(&self, ind: usize) -> Option<&(f32, Handle<AnimationClip>)> {
self.0.get(&ind)
}
pub fn new() -> Self {
HashMapAnimationClip(HashMap::new())
}
pub fn insert(
&mut self,
k: usize,
duration: f32,
handle: Handle<AnimationClip>,
) -> Option<(f32, Handle<AnimationClip>)> {
self.0.insert(k, (duration, handle))
}
}
/// utilisé par change_animation() pour mettre à jour la prochaine animation
#[derive(Component, Debug)]
pub struct AnimationStopWatch {
/// if of the entity containing the scene
pub creature_entity_id: u32,
pub index_animation: usize,
pub time: Timer,
pub manual_termination: bool,
}
impl AnimationStopWatch {
fn reset_timer(&mut self) {
self.time.reset();
}
fn manual_is_over(&mut self) -> bool {
if self.manual_termination {
self.manual_termination = false;
true
} else {
self.time.finished()
}
}
fn tick(&mut self, delta: Duration) {
self.time.tick(delta);
}
}
#[derive(Clone, Debug)]
pub struct SceneHandle {
/// handle of the scene
pub handle: Handle<Scene>,
/// vector of AnimationClip
//pub vec_animations: Vec<Handle<AnimationClip>>,
pub vec_animations: HashMapAnimationClip,
/// if of the entity containing the scene
pub creature_entity_id: Option<u32>,
/// type of the creature. Same type can use the same AnimationClip & Scenes
pub type_creature: TypeCreature,
/// To handle mutilple scene for one entity
pub activated: bool,
}
/// Composant qui mis à jour par link_animations()
/// L'entité est l'id de l'AnimationPlayer.
/// en utilisant ces deux queries :
/// ```
/// mut query_player: Query<&mut AnimationPlayer>,
/// mut query_entity: Query<(Entity, &AnimationEntityLink), With<Creature>>,
/// ```
/// On peut retrouver, pour une entité, son AnimationEntityLink, donc l'id de son AnimationPlayer
/// et:
/// ```
/// Ok(player) = query_player.get_mut(animation_link.0)
/// ```
#[derive(Component, Inspectable)]
pub struct AnimationEntityLink(pub Entity);
impl AnimationEntityLink {
fn get(&self) -> Entity {
self.0
}
}
fn get_top_parent(mut curr_entity: Entity, parent_query: &Query<&Parent>) -> Entity {
//Loop up all the way to the top parent
while let Ok(parent) = parent_query.get(curr_entity) {
curr_entity = parent.get();
}
curr_entity
}
/// Fonction qui lie une entité avec son AnimationPlayer par le composant AnimationEntityLink.
/// Voir: https://github.com/bevyengine/bevy/discussions/5564#discussion-4275825
fn link_animations(
player_query: Query<Entity, Added<AnimationPlayer>>,
parent_query: Query<&Parent>,
animations_entity_link_query: Query<&AnimationEntityLink>,
mut commands: Commands,
) {
// Get all the Animation players which can be deep and hidden in the hierarchy
for entity in player_query.iter() {
let skelly_entity = get_top_parent(entity, &parent_query);
debug!("Calling: link_animations. {:#?}", entity);
// If the top parent has an animation config ref then link the player to the config
if animations_entity_link_query.get(skelly_entity).is_ok() {
warn!("Problem with multiple animations players for the same top parent");
warn!(
"{:?} {:?} AnimationLink.{:?}",
entity,
skelly_entity,
animations_entity_link_query.get(skelly_entity).unwrap().0
);
} else {
commands
.entity(skelly_entity)
.insert(AnimationEntityLink(entity));
}
}
}
/// Une fois que link_animations() a ajouté un AnimationEntityLink :
/// Lancer la première animation !
fn start_animation(
query_entity: Query<Entity, (With<Creature>, Added<AnimationEntityLink>)>,
mut writer: EventWriter<ChangeAnimation>,
) {
for entity in query_entity.iter() {
writer.send(ChangeAnimation {
target: entity.id(),
index: 0,
repeat: false,
})
}
}
/// Fonction qui lit un Event ChangeAnimation et :
/// 1. D'après l'id de l'entité à animer (event.target.id())
/// 2. Retrouver l'animationPlayer associé en parcourant les tuples (Entity, &AnimationEntityLink)
/// ```
/// creature.id() == event_creature_à_animer.target
/// ```
/// 3. Une fois le player retrouvé, on cherche les animations dans VecSceneHandle
/// ```
/// scene_handler_random_creature.id() == event_creature_à_animer.id()
/// ```
fn update_animation(
mut events: EventReader<ChangeAnimation>,
scene_handlers: Res<VecSceneHandle>,
mut query_player: Query<&mut AnimationPlayer>,
mut query_entity: Query<(Entity, &AnimationEntityLink, &mut Creature)>,
mut query_stopwatch: Query<&mut AnimationStopWatch>,
) {
for event in events.iter() {
// retrouver l'entity
debug!("Event found! {:#?}", event);
for (entity, animation_link, mut creature) in query_entity.iter_mut() {
if entity.id() == event.target {
// on a retrouvé le player associé à l'entité
debug!(" > entity trouvé!");
for scene_handler in &scene_handlers.0 {
if scene_handler.type_creature == creature.type_creature {
debug!(
" > scene_handler trouvé pour {:#?}",
scene_handler.type_creature
);
if let Ok(mut player) = query_player.get_mut(animation_link.get()) {
let (duration, animation) =
scene_handler.vec_animations.get_pair(event.index).unwrap();
creature.current_animation_index.0 = event.index;
if event.repeat {
player.play(animation.clone_weak()).repeat();
debug!("Playing repeat!");
} else {
player.play(animation.clone_weak());
debug!("Playing!");
}
for mut stopwatch in query_stopwatch.iter_mut() {
if stopwatch.creature_entity_id == entity.id() {
stopwatch.index_animation = event.index;
stopwatch
.time
.set_duration(Duration::from_secs_f32(*duration));
}
}
}
}
}
}
}
}
}
/// Utiliser pour trouver la durée d'une AnimationClip
/// Ne fonctionne que pour le premier élément ajouté dans VecSceneHandle, après 'done' > 2.
/*
fn inspect_animation_clip(
assets_handle: Res<Assets<AnimationClip>>,
scene_handlers: Res<VecSceneHandle>,
mut done: Local<usize>,
) {
if *done > 2 {
return
}
for scene_handler in &scene_handlers.0 {
for ind in 0..scene_handler.vec_animations.0.len() {
let (_, handle) = scene_handler.vec_animations.get_pair(ind).unwrap();
if let Some(anim) = assets_handle.get(handle)
{
let st = format!("Start : {:?}", anim);
let duration_str_i = (st.as_str()).find("duration").unwrap();
let len = st.len();
info!("Anim index {}, {}", ind, st.get(duration_str_i..len-2).unwrap());
*done += 1;
}
}
}
}
*/
/// Récupère les stopwatch
/// Met à jour les ticks des stopwatch
/// Si une stopwacth est terminée :
/// Une animation est terminée
/// Récupérer la créature de l'animation et appeler sa fonction update_animation() pour choisir la prochaine animation
fn checker_animation_duration(
query_entity: Query<(Entity, &Creature)>,
mut query_stopwatch: Query<&mut AnimationStopWatch>,
mut event_writer: EventWriter<ChangeAnimation>,
time: Res<Time>,
) {
for mut stopwatch in query_stopwatch.iter_mut() {
stopwatch.tick(time.delta());
if stopwatch.manual_is_over() {
// play new animation for the current entity
debug!("Timer finished for entity {}", stopwatch.creature_entity_id);
stopwatch.reset_timer(); // en attendant que update_animation vienne faire le travail
for (entity, creature) in query_entity.iter() {
if entity.id() == stopwatch.creature_entity_id {
creature.update_animation(
stopwatch.creature_entity_id,
stopwatch.index_animation,
event_writer.borrow_mut(),
);
}
}
}
}
}
fn add_animation(
mut events: EventReader<AddAnimation>,
mut vec_scene_handlers: ResMut<VecSceneHandle>,
mut commands: Commands,
) {
for event in events.iter() {
debug!(
"AddAnimation: {:?} pour {:?}",
event.scene_handler.creature_entity_id, event.scene_handler.type_creature
);
vec_scene_handlers.0.push(event.scene_handler.clone());
// On ajoute une Stopwatch si on démarre l'animation
if event.start_animation {
let target = event.target.expect(
"Add_Animation a été appelé avec start_animation==true sans entity en target!",
);
spawn_animation_stop_watch(target, 0, commands.borrow_mut());
}
}
}
/// Ajoute une stopwatch au World.
/// Les stopwatch gardent le temps actuel de l'animation en cours pour une créature
/// Mis à jour par checker_animation_duration()
/// creature_entity_id ne doit pas être une Option! Obligatoire.
pub fn spawn_animation_stop_watch(
creature_entity_id: u32,
index_animation: usize,
commands: &mut Commands,
) {
commands
.spawn()
.insert(AnimationStopWatch {
creature_entity_id,
index_animation,
time: Timer::new(Duration::from_secs(1000.0 as u64), false),
manual_termination: false,
})
.insert(Name::new(format!("Stopwatch {}", creature_entity_id)));
}
fn remove_animation(
mut events: EventReader<RemoveAnimation>,
mut vec_scene_handlers: ResMut<VecSceneHandle>,
) {
let mut found;
for event in events.iter() {
found = false;
for i in 0..vec_scene_handlers.0.len() {
if !found && vec_scene_handlers.0[i].creature_entity_id == Some(event.entity_id) {
debug!("Remove_animation: entity found, removing.");
found = true;
vec_scene_handlers.0.swap_remove(i);
}
}
if !found {
warn!("Remove_animation: called with unknown entity")
}
}
}