|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
pub struct HelloPlugin;
|
|
|
|
|
|
impl Plugin for HelloPlugin
|
|
|
{
|
|
|
fn build(&self, app: &mut App)
|
|
|
{
|
|
|
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
|
|
|
.add_systems(Startup, add_people)
|
|
|
.add_systems(Update, (hello_world, (update_people, greet_people).chain()));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#[derive(Resource)]
|
|
|
struct GreetTimer(Timer);
|
|
|
|
|
|
#[derive(Component)]
|
|
|
struct Person;
|
|
|
|
|
|
#[derive(Component)]
|
|
|
struct Name(String);
|
|
|
|
|
|
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())));
|
|
|
}
|
|
|
|
|
|
// Just reading the timer in this one since greet_people already ticks it
|
|
|
// We could use ().chain() to force hello_world to happen after greet_people
|
|
|
// so the print out is always on the same frame.
|
|
|
fn hello_world(timer: ResMut<GreetTimer>,)
|
|
|
{
|
|
|
if timer.0.finished()
|
|
|
{
|
|
|
println!("hello world!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>)
|
|
|
{
|
|
|
// update our timer with the time elapsed since the last update
|
|
|
// if that caused the timer to finish, we say hello to everyone
|
|
|
if timer.0.tick(time.delta()).just_finished()
|
|
|
{
|
|
|
for name in &query
|
|
|
{
|
|
|
println!("hello {}!", name.0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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; // We don’t need to change any other names
|
|
|
}
|
|
|
}
|
|
|
}
|