use cosync::{Cosync, CosyncQueueHandle};
struct Game {
cosync: Cosync<World>,
world: World,
}
struct World {
player_position: f32,
monster_position: f32,
cosync_handle: CosyncQueueHandle<World>,
cancel_game: bool,
}
fn main() {
let cosync = Cosync::new();
let world = World {
player_position: 0.0,
monster_position: 20.0,
cosync_handle: cosync.create_queue_handle(),
cancel_game: false,
};
let mut game = Game { cosync, world };
let pos = 2.0;
game.world
.cosync_handle
.queue(move |mut world| async move {
cosync::sleep_ticks(10).await;
world.get().player_position = pos;
})
.unwrap();
game.cosync.queue(move |mut world| async move {
world.get().monster_position = pos;
cosync::sleep_ticks(100).await;
world.get().cancel_game = true;
});
loop {
game.cosync.run(&mut game.world);
if game.world.cancel_game {
break;
}
}
assert_eq!(game.world.player_position, 2.0);
assert_eq!(game.world.monster_position, 2.0);
}