outfly/src/main.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2024-03-16 14:00:31 +00:00
use bevy::prelude::*;
fn main() {
2024-03-16 14:00:31 +00:00
App::new()
2024-03-16 14:13:00 +00:00
.add_plugins((DefaultPlugins, HelloPlugin))
2024-03-16 14:00:31 +00:00
.run();
}
2024-03-16 14:21:51 +00:00
#[derive(Resource)]
struct GreetTimer(Timer);
2024-03-16 14:00:31 +00:00
#[derive(Component)]
struct Name(String);
#[derive(Component)]
struct Person;
fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("Elaina Proctor".to_string())));
commands.spawn((Person, Name("Renzo Hume".to_string())));
commands.spawn((Person, Name("Zayna Nieves".to_string())));
}
2024-03-16 14:13:00 +00:00
fn update_people(mut query: Query<&mut Name, With<Person>>) {
for mut name in &mut query {
if name.0 == "Elaina Proctor" {
name.0 = "Elaina Hume".to_string();
break;
}
}
}
2024-03-16 14:21:51 +00:00
fn greet_people(query: Query<&Name, With<Person>>, time:Res<Time>, mut timer: ResMut<GreetTimer>) {
if timer.0.tick(time.delta()).just_finished() {
for name in &query {
println!("hello {}!", name.0);
}
2024-03-16 14:00:31 +00:00
}
}
fn hello_world() {
println!("hello world!");
}
2024-03-16 14:13:00 +00:00
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
2024-03-16 14:21:51 +00:00
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
.add_systems(Startup, add_people)
2024-03-16 14:13:00 +00:00
.add_systems(Update, (hello_world, (update_people, greet_people).chain()));
}
}