simple black window with Q key to exit

This commit is contained in:
yuni 2024-03-16 16:12:35 +01:00
parent 0b6b03a9c1
commit e8dbe717cd

View file

@ -2,51 +2,16 @@ use bevy::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins((DefaultPlugins, HelloPlugin)) .add_systems(Update, handle_input)
.add_plugins((DefaultPlugins, ))
.run(); .run();
} }
#[derive(Resource)] fn handle_input(
struct GreetTimer(Timer); keyboard_input: Res<ButtonInput<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>
#[derive(Component)] ) {
struct Name(String); if keyboard_input.pressed(KeyCode::KeyQ) {
app_exit_events.send(bevy::app::AppExit);
#[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())));
}
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;
}
}
}
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);
}
}
}
fn hello_world() {
println!("hello world!");
}
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()));
} }
} }