| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use bevy::prelude::*; |
| 8 |
use core::time::Duration; |
| 9 |
|
| 10 |
fn main() { |
| 11 |
App::new() |
| 12 |
|
| 13 |
.add_plugins(( |
| 14 |
DefaultPlugins, |
| 15 |
PrintMessagePlugin { |
| 16 |
wait_duration: Duration::from_secs(1), |
| 17 |
message: "This is an example plugin".to_string(), |
| 18 |
}, |
| 19 |
)) |
| 20 |
.run(); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
struct PrintMessagePlugin { |
| 25 |
|
| 26 |
wait_duration: Duration, |
| 27 |
message: String, |
| 28 |
} |
| 29 |
|
| 30 |
impl Plugin for PrintMessagePlugin { |
| 31 |
|
| 32 |
fn build(&self, app: &mut App) { |
| 33 |
let state = PrintMessageState { |
| 34 |
message: self.message.clone(), |
| 35 |
timer: Timer::new(self.wait_duration, TimerMode::Repeating), |
| 36 |
}; |
| 37 |
app.insert_resource(state) |
| 38 |
.add_systems(Update, print_message_system); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[derive(Resource)] |
| 43 |
struct PrintMessageState { |
| 44 |
message: String, |
| 45 |
timer: Timer, |
| 46 |
} |
| 47 |
|
| 48 |
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) { |
| 49 |
if state.timer.tick(time.delta()).is_finished() { |
| 50 |
info!("{}", state.message); |
| 51 |
} |
| 52 |
} |