| Name | Message | Date |
|---|---|---|
| 📁 bunnies | 1 month ago | |
| 📄 confetti.rs | 1 month ago | |
| 📄 dog.rs | 1 month ago | |
| 📄 goal.rs | 1 month ago | |
| 📄 main.rs | 1 month ago | |
| 📄 obstacles.rs | 1 month ago |
📄
src/goal.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
use std::{ ops::{Add, DerefMut, RangeInclusive}, time::Duration, }; use bevy::{ app::{App, Startup, Update}, asset::Assets, color::Color, ecs::{ children, component::Component, hierarchy::Children, query::{With, Without}, system::{Commands, Query, Res, ResMut, Single}, }, log::warn, math::{Dir3, Quat, Vec3, primitives::Circle}, mesh::{Mesh, Mesh3d}, pbr::{MeshMaterial3d, StandardMaterial}, text::{TextFont, TextSpan}, time::Time, transform::components::Transform, ui::widget::Text, }; use rand::RngExt; use crate::{ Rng, bunnies::{Bunny, BunnyLocator}, confetti::SpawnConfetti, }; pub trait GoalSystems { fn add_goal_systems(&mut self) -> &mut Self; } impl GoalSystems for App { fn add_goal_systems(&mut self) -> &mut Self { self.add_systems(Startup, setup).add_systems(Update, update) } } const GOAL_AREA_WIDTH: RangeInclusive<f32> = -10.0..=10.0; const GOAL_AREA_HEIGHT: RangeInclusive<f32> = -10.0..=10.0; const GOAL_RADIUS: f32 = 5.0; const GOAL_DURATION: f32 = 1.0; const CELEBRATION_DURATION: f32 = 3.0; #[derive(Component, Default)] enum Goal { #[default] NotAchieved, InProgress { start: Duration, }, Achieved { start: Duration, }, } #[derive(Component)] struct GoalText; fn setup( mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>, ) { commands.spawn(( Mesh3d(meshes.add(Circle::new(GOAL_RADIUS))), MeshMaterial3d(materials.add(Color::linear_rgb(0.0, 1.0, 0.0))), Transform::from_xyz(10.0, 0.0, 10.0) .with_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), Goal::default(), children![( Mesh3d(meshes.add(Circle::new(GOAL_RADIUS))), MeshMaterial3d(materials.add(Color::linear_rgb(0.0, 0.0, 1.0))), Transform::from_xyz(0.0, 0.0, 0.1).with_scale(Vec3::new(0.0, 0.0, 1.0)), )], )); commands .spawn(( Text::new("Bunnies in goal: "), TextFont { font_size: 42.0, ..Default::default() }, )) .with_child(( TextSpan::default(), TextFont { font_size: 33.0, ..Default::default() }, GoalText, )); } fn update( mut commands: Commands, locator: Res<BunnyLocator>, bunnies: Query<(), With<Bunny>>, time: Res<Time>, mut rng: ResMut<Rng>, mut goal: Single<(&mut Transform, &mut Goal, &Children)>, mut goal_text: Single<&mut TextSpan, With<GoalText>>, mut transforms: Query<&mut Transform, Without<Goal>>, ) { let (transform, goal, children) = goal.deref_mut(); let count = bunnies.count(); let in_goal = locator.get_nearby_count(transform.translation, GOAL_RADIUS); **goal_text = format!("{in_goal}/{count}").into(); match (in_goal == count, goal.as_ref()) { (_, Goal::Achieved { start }) => { if start.add(Duration::from_secs_f32(CELEBRATION_DURATION)) < time.elapsed() { **goal = Goal::NotAchieved; transform.translation = Vec3::new( rng.random_range(GOAL_AREA_WIDTH), 0.0, rng.random_range(GOAL_AREA_HEIGHT), ); } } (true, Goal::NotAchieved) => { **goal = Goal::InProgress { start: time.elapsed(), } } (true, Goal::InProgress { start }) => { let Some(Ok(mut child)) = children.first().map(|c| transforms.get_mut(*c)) else { warn!("Cannot get child of goal"); return; }; if start.add(Duration::from_secs_f32(GOAL_DURATION)) < time.elapsed() { commands.trigger(SpawnConfetti { origin: transform.translation, direction: Dir3::Y, count: 1000, }); **goal = Goal::Achieved { start: time.elapsed(), }; child.scale = Vec3::ZERO; } else { let scale = 1.0 - (start.add(Duration::from_secs_f32(GOAL_DURATION)) - time.elapsed()) .as_secs_f32() / GOAL_DURATION; child.scale = Vec3::new(scale, scale, 1.0); } } (false, Goal::NotAchieved) => {} (false, Goal::InProgress { .. }) => { let Some(Ok(mut child)) = children.first().map(|c| transforms.get_mut(*c)) else { warn!("Cannot get child of goal"); return; }; **goal = Goal::NotAchieved; child.scale = Vec3::new(0.0, 0.0, 1.0); } } }