| Name | Message | Date |
|---|---|---|
| 📁 bunnies | 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
use bevy::{ app::{App, Startup, Update}, asset::Assets, color::Color, ecs::{ component::Component, query::With, system::{Commands, Query, Res, ResMut, Single}, }, math::{Quat, primitives::Circle}, mesh::{Mesh, Mesh3d}, pbr::{MeshMaterial3d, StandardMaterial}, text::{TextFont, TextSpan}, transform::components::Transform, ui::widget::Text, }; use rand::RngExt; use crate::{ Rng, bunnies::{Bunny, BunnyLocator}, }; 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_RADIUS: f32 = 5.0; #[derive(Component)] struct Goal; #[derive(Component)] struct GoalText; fn setup( mut rng: ResMut<Rng>, 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( rng.random_range(-25.0..=25.0), 0.0, rng.random_range(-25.0..=25.0), ) .with_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), Goal, )); 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( locator: Res<BunnyLocator>, bunnies: Query<(), With<Bunny>>, goal: Single<&Transform, With<Goal>>, mut goal_text: Single<&mut TextSpan, With<GoalText>>, ) { let count = bunnies.count(); let in_goal = locator.get_nearby_count(goal.translation, GOAL_RADIUS); **goal_text = format!("{in_goal}/{count}").into(); }