mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-16 00:03:29 +00:00
Bevy Book: Resources
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::{
|
||||
app::{Startup, Update},
|
||||
prelude::{App, Commands, Component, Query, With},
|
||||
prelude::{App, Commands, Component, Query, Res, ResMut, Resource, Startup, Update, With},
|
||||
time::{Time, TimerMode},
|
||||
};
|
||||
|
||||
pub struct Plugin;
|
||||
|
||||
impl bevy::app::Plugin for Plugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
#[rustfmt::skip]
|
||||
app.add_systems(Startup, startup)
|
||||
.add_systems(Update, greet);
|
||||
app.insert_resource(Timer(bevy::time::Timer::from_seconds(
|
||||
2.0,
|
||||
TimerMode::Repeating,
|
||||
)))
|
||||
.add_systems(Startup, startup)
|
||||
.add_systems(Update, greet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,14 +30,25 @@ impl Name {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct Timer(bevy::time::Timer);
|
||||
|
||||
impl Timer {
|
||||
fn tick(&mut self, delta: Duration) -> &bevy::time::Timer {
|
||||
self.0.tick(delta)
|
||||
}
|
||||
}
|
||||
|
||||
fn startup(mut cmds: Commands) {
|
||||
cmds.spawn((Person, Name::new("Elaina Proctor")));
|
||||
cmds.spawn((Person, Name::new("Renzo Hume")));
|
||||
cmds.spawn((Person, Name::new("Zayna Nieves")));
|
||||
}
|
||||
|
||||
fn greet(q: Query<&Name, With<Person>>) {
|
||||
for name in &q {
|
||||
println!("Hello, {}!", name.0);
|
||||
fn greet(time: Res<Time>, mut timer: ResMut<Timer>, q: Query<&Name, With<Person>>) {
|
||||
if timer.tick(time.delta()).just_finished() {
|
||||
for name in &q {
|
||||
println!("Hello, {}!", name.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user