Created
November 10, 2023 00:43
-
-
Save thebluefish/82023d945c5a7b6127756fa6e4083e0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::app::ScheduleRunnerPlugin; | |
use bevy::ecs::schedule::{ExecutorKind, ScheduleLabel}; | |
use bevy::prelude::*; | |
fn main() { | |
let mut app = App::empty(); | |
let mut schedule = Schedule::new(); | |
schedule.set_executor_kind(ExecutorKind::SingleThreaded); | |
app.main_schedule_label = Box::new(ServerSchedule); | |
app | |
.init_resource::<AppTypeRegistry>() | |
.add_plugins(MinimalPlugins.build().disable::<ScheduleRunnerPlugin>()) | |
.add_schedule(PreStartup, Schedule::new()) | |
.add_schedule(Startup, Schedule::new()) | |
.add_schedule(PostStartup, Schedule::new()) | |
.add_schedule(StateTransition, Schedule::new()) | |
.add_schedule(First, Schedule::new()) | |
.add_schedule(Loading, Schedule::new()) | |
.add_schedule(PreUpdate, Schedule::new()) | |
.add_schedule(Update, Schedule::new()) | |
.add_schedule(PostUpdate, Schedule::new()) | |
.add_schedule(Last, Schedule::new()) | |
.add_schedule(ServerSchedule, schedule) | |
.add_state::<LoadState>() | |
.add_systems(ServerSchedule, ServerSchedule::run) | |
.add_systems(Loading, check_load) | |
.add_systems(Update, guarded_system) | |
// my app no longer uses a runner, this quick approximation serves for a non-looping demonstration | |
.set_runner(move |mut app: App| { | |
while !app.ready() { | |
#[cfg(not(target_arch = "wasm32"))] | |
bevy::tasks::tick_global_task_pools_on_main_thread(); | |
} | |
app.finish(); | |
app.cleanup(); | |
app.update(); | |
app.update(); | |
app.update(); | |
}) | |
.run() | |
; | |
} | |
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | |
enum LoadState { | |
#[default] | |
NotLoaded, | |
Loading, | |
Loaded, | |
} | |
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] | |
pub struct Loading; | |
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] | |
pub struct ServerSchedule; | |
impl ServerSchedule { | |
pub fn run(world: &mut World) { | |
world.run_schedule(First); | |
world.run_schedule(StateTransition); | |
let state = world.resource::<State<LoadState>>(); | |
println!("run {:?}", state.get()); | |
match state.get() { | |
LoadState::NotLoaded => { | |
world.run_schedule(PreStartup); | |
world.run_schedule(Startup); | |
world.run_schedule(PostStartup); | |
world.resource_mut::<NextState<LoadState>>().set(LoadState::Loading); | |
}, | |
LoadState::Loading => world.run_schedule(Loading), | |
LoadState::Loaded => { | |
world.run_schedule(PreUpdate); | |
world.run_schedule(Update); | |
world.run_schedule(PostUpdate); | |
} | |
} | |
world.run_schedule(Last); | |
} | |
} | |
#[derive(Resource)] | |
struct MockLoaded(pub usize); | |
fn check_load(mut commands: Commands, mut next: ResMut<NextState<LoadState>>) { | |
commands.insert_resource(MockLoaded(42)); | |
next.set(LoadState::Loaded); | |
} | |
fn guarded_system(loaded: Res<MockLoaded>) { | |
println!("Update got {}", loaded.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment