* sucessfull experiment with ways to animate world level objects, using
the same logic as the blueprint animations
 * made required changes
This commit is contained in:
kaosat.dev 2024-03-22 11:11:45 +01:00
parent 0b64de79da
commit 27201e5cd6
4 changed files with 159 additions and 38 deletions

View File

@ -202,6 +202,7 @@ pub(crate) fn spawn_from_blueprints(
Option<&Library>, Option<&Library>,
Option<&AddToGameWorld>, Option<&AddToGameWorld>,
Option<&Name>, Option<&Name>,
Option<&Animations>
), ),
( (
With<BlueprintAssetsLoaded>, With<BlueprintAssetsLoaded>,
@ -227,6 +228,7 @@ pub(crate) fn spawn_from_blueprints(
library_override, library_override,
add_to_world, add_to_world,
name, name,
animations,
) in spawn_placeholders.iter() ) in spawn_placeholders.iter()
{ {
debug!( debug!(
@ -279,13 +281,17 @@ pub(crate) fn spawn_from_blueprints(
transform: transforms, transform: transforms,
..Default::default() ..Default::default()
}, },
Animations {
named_animations: gltf.named_animations.clone(),
},
Spawned, Spawned,
OriginalChildren(original_children), OriginalChildren(original_children),
)); ));
// only insert the animations if they are not present already: TODO ideally we want to be merging animations, though it could lead to clashes
if animations.is_none() {
commands.entity(entity).insert(Animations {
named_animations: gltf.named_animations.clone(),
});
}
if add_to_world.is_some() { if add_to_world.is_some() {
let world = game_world let world = game_world
.get_single_mut() .get_single_mut()

View File

@ -2947,6 +2947,28 @@
"type": "object", "type": "object",
"typeInfo": "Struct" "typeInfo": "Struct"
}, },
"bevy_example::game::Marker1": {
"additionalProperties": false,
"isComponent": true,
"isResource": false,
"properties": {},
"required": [],
"short_name": "Marker1",
"title": "bevy_example::game::Marker1",
"type": "object",
"typeInfo": "Struct"
},
"bevy_example::game::Marker2": {
"additionalProperties": false,
"isComponent": true,
"isResource": false,
"properties": {},
"required": [],
"short_name": "Marker2",
"title": "bevy_example::game::Marker2",
"type": "object",
"typeInfo": "Struct"
},
"bevy_example::test_components::AComponentWithAnExtremlyExageratedOrMaybeNotButCouldBeNameOrWut": { "bevy_example::test_components::AComponentWithAnExtremlyExageratedOrMaybeNotButCouldBeNameOrWut": {
"additionalProperties": false, "additionalProperties": false,
"isComponent": true, "isComponent": true,

View File

@ -3,7 +3,7 @@ use std::{
collections::HashMap, fs, time::Duration collections::HashMap, fs, time::Duration
}; };
use bevy_gltf_blueprints::{Animated, AnimationPlayerLink, Animations, BlueprintName, BlueprintsList}; use bevy_gltf_blueprints::{Animated, AnimationPlayerLink, Animations, BlueprintName, BlueprintsList, GltfBlueprintsSet};
pub use in_game::*; pub use in_game::*;
use bevy::{ use bevy::{
@ -138,22 +138,23 @@ fn setup_main_scene_animations(
commands.insert_resource(AnimTest(asset_server.load("models/World.glb"))); commands.insert_resource(AnimTest(asset_server.load("models/World.glb")));
} }
fn animations(
foo:Query<(Entity, &Name, &AnimationPlayer),(Added<AnimationPlayer>)>,
bla:Query<(Entity, &Name, &Animated),(Added<Animated>, Without<AnimationPlayerLink>)>,
blurp: Res<AnimTest>,
asset_server: Res<AssetServer>, fn animations(
added_animation_players:Query<(Entity, &Name, &AnimationPlayer)>,
mut addded_animateds:Query<(Entity, &Name, &Animated),(Added<Animated>, Without<AnimationPlayerLink>)>,
animtest: Res<AnimTest>,
mut commands: Commands, mut commands: Commands,
assets_gltf: Res<Assets<Gltf>>, assets_gltf: Res<Assets<Gltf>>,
parents: Query<&Parent>,
names: Query<&Name>,
) { ) {
for (entity, name, animated) in addded_animateds.iter() {
for (entity, name, animated) in bla.iter() {
// println!("animated stuf {:?} on entity {}", animated, name); // println!("animated stuf {:?} on entity {}", animated, name);
let gltf = assets_gltf.get(&animtest.0).unwrap();
let gltf = assets_gltf.get(&blurp.0).unwrap();
let animations_list = animated; let animations_list = animated;
let mut matching_data = true; let mut matching_data = true;
@ -166,43 +167,135 @@ fn animations(
if matching_data { if matching_data {
println!("inserting Animations components into {} ({:?})", name, entity); println!("inserting Animations components into {} ({:?})", name, entity);
println!("Found match {:?}", gltf.named_animations); println!("Found match {:?}", gltf.named_animations);
commands.entity(entity).remove::<Animations>(); // commands.entity(entity).remove::<Animations>();
commands.entity(entity).insert(( // FIXME: for some reason this does NOT overwrite the component ??
commands.entity(entity).insert(
Animations { Animations {
named_animations: gltf.named_animations.clone() named_animations: gltf.named_animations.clone(),
},
);
//animations.named_animations = gltf.named_animations.clone();
for ancestor in parents.iter_ancestors(entity) {
if added_animation_players.contains(ancestor) {
println!("found match with animationPlayer !! {:?}",names.get(ancestor));
commands.entity(entity).insert(AnimationPlayerLink(ancestor));
}
// info!("{:?} is an ancestor of {:?}", ancestor, player);
}
} }
));
} }
} }
/*for bla in foo.iter() { fn play_animations(
let mut counter = 0; animated_marker1: Query<(&AnimationPlayerLink, &Animations), (With<Animated>, With<Marker1>)>,
counter +=1; animated_marker2: Query<(&AnimationPlayerLink, &Animations), (With<Animated>, With<Marker2>)>,
println!("found some animations {} {}", counter, bla.1);
if bla.1.to_string() == "Collection".to_string(){ mut animation_players: Query<&mut AnimationPlayer>,
/*commands.insert_resource(Animations(vec![ keycode: Res<ButtonInput<KeyCode>>,
asset_server.load("models/World.glb#Animation0"),
asset_server.load("models/World.glb#Animation1"), ) {
]));*/ if keycode.just_pressed(KeyCode::KeyM) {
/*commands.entity(bla.0).insert(Animations { for (link, animations) in animated_marker1.iter() {
named_animations: println!("animations {:?}", animations.named_animations);
})*/ let mut animation_player = animation_players.get_mut(link.0).unwrap();
let anim_name = "Blueprint1_move";
animation_player
.play_with_transition(
animations
.named_animations
.get(anim_name)
.expect("animation name should be in the list")
.clone(),
Duration::from_secs(5),
)
.repeat();
} }
}*/
} }
if keycode.just_pressed(KeyCode::KeyJ) {
for (link, animations) in animated_marker1.iter() {
println!("animations {:?}", animations.named_animations);
let mut animation_player = animation_players.get_mut(link.0).unwrap();
let anim_name = "Blueprint1_jump";
animation_player
.play_with_transition(
animations
.named_animations
.get(anim_name)
.expect("animation name should be in the list")
.clone(),
Duration::from_secs(5),
)
.repeat();
}
}
if keycode.just_pressed(KeyCode::KeyA) {
for (link, animations) in animated_marker2.iter() {
println!("animations {:?}", animations.named_animations);
let mut animation_player = animation_players.get_mut(link.0).unwrap();
let anim_name = "Blueprint1_move";
animation_player
.play_with_transition(
animations
.named_animations
.get(anim_name)
.expect("animation name should be in the list")
.clone(),
Duration::from_secs(5),
)
.repeat();
}
}
if keycode.just_pressed(KeyCode::KeyB) {
for (link, animations) in animated_marker2.iter() {
println!("animations {:?}", animations.named_animations);
let mut animation_player = animation_players.get_mut(link.0).unwrap();
let anim_name = "Blueprint1_jump";
animation_player
.play_with_transition(
animations
.named_animations
.get(anim_name)
.expect("animation name should be in the list")
.clone(),
Duration::from_secs(5),
)
.repeat();
}
}
}
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
/// flag component for testing
pub struct Marker1;
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
/// flag component for testing
pub struct Marker2;
pub struct GamePlugin; pub struct GamePlugin;
impl Plugin for GamePlugin { impl Plugin for GamePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, (spawn_test).run_if(in_state(GameState::InGame))) app.register_type::<Marker1>()
.register_type::<Marker2>()
.add_systems(Update, (spawn_test).run_if(in_state(GameState::InGame)))
.add_systems(Update, validate_export) .add_systems(Update, validate_export)
.add_systems(OnEnter(AppState::MenuRunning), start_game) .add_systems(OnEnter(AppState::MenuRunning), start_game)
.add_systems(OnEnter(AppState::AppRunning), setup_game) .add_systems(OnEnter(AppState::AppRunning), setup_game)
.add_systems(OnEnter(AppState::MenuRunning), setup_main_scene_animations) .add_systems(OnEnter(AppState::MenuRunning), setup_main_scene_animations)
.add_systems(Update, animations
.add_systems(Update, animations.run_if(in_state(AppState::AppRunning))) .run_if(in_state(AppState::AppRunning))
.after(GltfBlueprintsSet::AfterSpawn)
)
.add_systems(Update, play_animations)
/* .add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once /* .add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once
.add_systems( .add_systems(
Update, Update,