use shred::{
DispatcherBuilder, MultiDispatchController, MultiDispatcher, Read, System, World, Write,
};
use std::{thread::sleep, time::Duration};
fn main() {
let mut dispatcher = DispatcherBuilder::new()
.with(SayHelloSystem, "say_hello_system", &[])
.with_batch(
MultiDispatcher::new(Run3Times),
DispatcherBuilder::new()
.with(BuyTomatoSystem, "buy_tomato_system", &[])
.with(BuyPotatoSystem, "buy_potato_system", &[]),
"BatchSystemTest",
&[],
)
.build();
let mut world = World::empty();
dispatcher.setup(&mut world);
for i in 0..10 {
println!("Dispatching {} ", i);
dispatcher.dispatch(&world);
sleep(Duration::new(0, 100000000));
}
println!("Execution finished");
}
#[derive(Default)]
pub struct PotatoStore(i32);
#[derive(Default)]
pub struct TomatoStore(f32);
pub struct SayHelloSystem;
impl<'a> System<'a> for SayHelloSystem {
type SystemData = (Write<'a, PotatoStore>, Write<'a, TomatoStore>);
fn run(&mut self, _data: Self::SystemData) {
println!("Hello!")
}
}
pub struct BuyPotatoSystem;
impl<'a> System<'a> for BuyPotatoSystem {
type SystemData = Write<'a, PotatoStore>;
fn run(&mut self, _data: Self::SystemData) {
println!("Buy Potato")
}
}
pub struct BuyTomatoSystem;
impl<'a> System<'a> for BuyTomatoSystem {
type SystemData = Write<'a, TomatoStore>;
fn run(&mut self, _data: Self::SystemData) {
println!("Buy Tomato")
}
}
#[derive(Default)]
struct Run3Times;
impl<'a> MultiDispatchController<'a> for Run3Times {
type SystemData = Read<'a, TomatoStore>;
fn plan(&mut self, _data: Self::SystemData) -> usize {
3
}
}