Add placeholder alien with animations (#1)

This commit is contained in:
GitGhillie
2025-03-02 18:31:08 +01:00
committed by GitHub
parent b4ecd469fa
commit bd50c1f2c2
3 changed files with 90 additions and 0 deletions

Binary file not shown.

86
src/alien.rs Normal file
View File

@@ -0,0 +1,86 @@
use std::time::Duration;
use bevy::prelude::*;
const FOX_PATH: &str = "models/alien_naked.glb";
#[derive(Resource)]
struct Animations {
animations: Vec<AnimationNodeIndex>,
graph: Handle<AnimationGraph>,
}
pub fn plugin(app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, (setup_once_loaded, toggle_animation));
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut graphs: ResMut<Assets<AnimationGraph>>,
) {
// Build the animation graph
let (graph, node_indices) = AnimationGraph::from_clips([
asset_server.load(GltfAssetLabel::Animation(1).from_asset(FOX_PATH)),
asset_server.load(GltfAssetLabel::Animation(0).from_asset(FOX_PATH)),
]);
// Insert a resource with the current scene information
let graph_handle = graphs.add(graph);
commands.insert_resource(Animations {
animations: node_indices,
graph: graph_handle,
});
// Fox
commands
.spawn(SceneRoot(
asset_server.load(GltfAssetLabel::Scene(0).from_asset(FOX_PATH)),
))
.insert(Name::from("Alien"));
}
fn setup_once_loaded(
mut commands: Commands,
animations: Res<Animations>,
mut players: Query<(Entity, &mut AnimationPlayer), Added<AnimationPlayer>>,
) {
for (entity, mut player) in &mut players {
let mut transitions = AnimationTransitions::new();
// Make sure to start the animation via the `AnimationTransitions`
// component. The `AnimationTransitions` component wants to manage all
// the animations and will get confused if the animations are started
// directly via the `AnimationPlayer`.
transitions
.play(&mut player, animations.animations[1], Duration::ZERO)
.repeat();
commands
.entity(entity)
.insert(AnimationGraphHandle(animations.graph.clone()))
.insert(transitions);
}
}
fn toggle_animation(
animations: Res<Animations>,
mut transitions: Query<(&mut AnimationTransitions, &mut AnimationPlayer)>,
keys: Res<ButtonInput<KeyCode>>,
mut animation_index: Local<u32>,
) {
if keys.just_pressed(KeyCode::KeyE) {
for (mut transition, mut player) in &mut transitions {
transition
.play(
&mut player,
animations.animations[*animation_index as usize],
Duration::from_secs(1),
)
.repeat();
}
*animation_index ^= 1; // Toggle between 0 and 1
}
}

View File

@@ -9,6 +9,8 @@ use bevy::render::view::ColorGrading;
use bevy_flycam::prelude::*;
use bevy_trenchbroom::prelude::*;
mod alien;
#[derive(SolidClass, Component, Reflect)]
#[reflect(Component)]
#[geometry(GeometryProvider::new().convex_collider().smooth_by_default_angle().render())]
@@ -74,6 +76,8 @@ fn main() {
// bevy_flycam setup so we can get a closer look at the scene, mainly for debugging
app.add_plugins(PlayerPlugin);
app.add_plugins(alien::plugin);
app.insert_resource(MovementSettings {
sensitivity: 0.00005,
speed: 12.,