| Name | Message | Date |
|---|---|---|
| 📄 bunny.rs | 1 month ago | |
| 📄 heart.rs | 1 month ago | |
| 📄 locator.rs | 1 month ago | |
| 📄 mod.rs | 1 month ago |
📄
src/bunnies/heart.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
use std::time::Duration; use bevy::{ animation::{ AnimatedBy, AnimationClip, AnimationPlayer, AnimationTargetId, animated_field, animation_curves::{AnimatableCurve, AnimatedField}, graph::{AnimationGraph, AnimationGraphHandle}, }, app::{App, Update}, asset::{AssetServer, Assets}, color::Color, ecs::{ component::Component, entity::Entity, hierarchy::Children, name::Name, observer::On, query::With, system::{Commands, In, Query, Res, ResMut}, }, gltf::GltfAssetLabel, math::{Quat, Vec3, curve::UnevenSampleAutoCurve}, pbr::{MeshMaterial3d, StandardMaterial}, scene::{SceneInstanceReady, SceneRoot}, time::Time, transform::components::Transform, }; pub(super) fn add_heart_system(app: &mut App) -> &mut App { app.add_observer(heart_ready); app.add_systems(Update, destroy_heart) } pub(super) fn spawn_heart( In((mid_point, time_to_live)): In<(Vec3, Duration)>, mut commands: Commands, time: Res<Time>, asset_server: Res<AssetServer>, mut materials: ResMut<Assets<StandardMaterial>>, mut animations: ResMut<Assets<AnimationClip>>, mut graphs: ResMut<Assets<AnimationGraph>>, ) { let model = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/3D Leap Land/glb/heart.glb")); let name = Name::new("heart"); let mut animation = AnimationClip::default(); let animation_target_id = AnimationTargetId::from_name(&name); animation.add_curve_to_target( animation_target_id, AnimatableCurve::new( animated_field!(Transform::translation), UnevenSampleAutoCurve::new([ (0.0, mid_point.with_y(2.0)), (0.5, mid_point.with_y(3.0)), (1.0, mid_point.with_y(2.0)), ]) .unwrap(), ), ); animation.add_curve_to_target( animation_target_id, AnimatableCurve::new( animated_field!(Transform::rotation), UnevenSampleAutoCurve::new([ (0.0, Quat::IDENTITY), ( 0.25, Quat::from_axis_angle(Vec3::Y, std::f32::consts::PI * 0.5), ), ( 0.50, Quat::from_axis_angle(Vec3::Y, std::f32::consts::PI * 1.0), ), ( 0.75, Quat::from_axis_angle(Vec3::Y, std::f32::consts::PI * 1.5), ), (1.0, Quat::IDENTITY), ]) .unwrap(), ), ); let (graph, animation_index) = AnimationGraph::from_clip(animations.add(animation)); let mut player = AnimationPlayer::default(); player.play(animation_index).repeat().set_speed(0.5); let mut entity = commands.spawn_empty(); entity.insert(( SceneRoot(model), MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.0))), Transform::from_scale(Vec3::splat(0.01)), name, AnimationGraphHandle(graphs.add(graph)), player, animation_target_id, AnimatedBy(entity.id()), Heart { destroy_time: time.elapsed() + time_to_live, }, )); } #[derive(Component)] struct Heart { destroy_time: Duration, } fn heart_ready( scene_ready: On<SceneInstanceReady>, children: Query<&Children>, hearts: Query<(), With<Heart>>, mesh_materials: Query<&MeshMaterial3d<StandardMaterial>>, mut asset_materials: ResMut<Assets<StandardMaterial>>, ) { let Ok(()) = hearts.get(scene_ready.entity) else { return; }; for descendant in children.iter_descendants(scene_ready.entity) { let Ok(id) = mesh_materials.get(descendant) else { continue; }; let Some(material) = asset_materials.get_mut(id.id()) else { continue; }; material.base_color = Color::linear_rgb(1.0, 0.0, 0.0); } } fn destroy_heart(mut commands: Commands, time: Res<Time>, hearts: Query<(Entity, &Heart)>) { hearts.iter().for_each(|(heart, Heart { destroy_time })| { if time.elapsed() < *destroy_time { return; } commands.entity(heart).despawn(); }); }