key mesh and squishy animation

This commit is contained in:
2025-03-13 22:11:30 +01:00
parent c358108c56
commit 7281a46521
4 changed files with 27 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
use crate::player::Player;
use crate::{billboards::Billboard, player::Player, squish_animation::SquishAnimation};
use avian3d::prelude::*;
use bevy::prelude::*;
use std::f32::consts::PI;
@@ -18,19 +18,14 @@ pub fn plugin(app: &mut App) {
app.add_observer(on_spawn);
}
fn on_spawn(
trigger: Trigger<KeySpawn>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
fn on_spawn(trigger: Trigger<KeySpawn>, mut commands: Commands, asset_server: Res<AssetServer>) {
let KeySpawn(position) = trigger.event();
let mesh = meshes.add(Cylinder::default());
//TODO: randomize
let spawn_dir = Quat::from_rotation_y(PI / 2.) * Vec3::new(0.5, 0.6, 0.).normalize();
let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/key.glb"));
commands
.spawn((
Name::new("key"),
@@ -43,12 +38,7 @@ fn on_spawn(
RigidBody::Dynamic,
Restitution::new(0.6),
))
.with_child((
Transform::from_scale(Vec3::new(2.2, 0.4, 2.2))
.with_rotation(Quat::from_rotation_x(PI / 2.)),
Mesh3d(mesh),
MeshMaterial3d(materials.add(Color::srgb(0., 0., 0.9))),
));
.with_child((Billboard, SquishAnimation(2.6), SceneRoot(mesh)));
}
fn collect_key(

View File

@@ -11,6 +11,7 @@ mod npc;
mod platforms;
mod player;
mod shooting;
mod squish_animation;
mod tb_entities;
use avian3d::PhysicsPlugins;
@@ -84,6 +85,7 @@ fn main() {
app.add_plugins(shooting::plugin);
app.add_plugins(npc::plugin);
app.add_plugins(keys::plugin);
app.add_plugins(squish_animation::plugin);
app.insert_resource(AmbientLight {
color: Color::WHITE,

20
src/squish_animation.rs Normal file
View File

@@ -0,0 +1,20 @@
use bevy::prelude::*;
use ops::sin;
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct SquishAnimation(pub f32);
pub fn plugin(app: &mut App) {
app.add_systems(Update, update);
}
fn update(mut query: Query<(&mut Transform, &SquishAnimation)>, time: Res<Time>) {
for (mut transform, keymesh) in query.iter_mut() {
transform.scale = Vec3::new(
keymesh.0,
keymesh.0 + (sin(time.elapsed_secs() * 6.) * 0.2),
keymesh.0,
);
}
}