Followed getting-started guide

This commit is contained in:
2024-07-05 08:45:34 -05:00
commit 3b9114d4b2
4 changed files with 4332 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

4270
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

7
Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "bevy-testing"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.14.0"

54
src/main.rs Normal file
View File

@@ -0,0 +1,54 @@
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, HelloPlugin))
.run();
}
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("John".to_string())));
commands.spawn((Person, Name("Jack".to_string())));
commands.spawn((Person, Name("Bill".to_string())));
}
#[derive(Resource)]
struct GreetTimer(Timer);
fn greet_people(
time: Res<Time>,
mut timer: ResMut<GreetTimer>,
query: Query<&Name, With<Person>>
) {
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 == "Bill" {
name.0 = "Jill".to_string();
break;
}
}
}
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, (update_people, greet_people).chain());
}
}