use bevy_ecs::system::ScheduleSystem;
pub use bevy_ecs::prelude::*;
pub trait Plugin {
fn build(&self, app: &mut Application);
}
pub struct Application {
pub world: World,
startup: Schedule,
update: Schedule,
startup_done: bool,
}
impl Application {
pub fn new() -> Self {
Self {
world: World::new(),
startup: Schedule::default(),
update: Schedule::default(),
startup_done: false,
}
}
pub fn add_plugin(&mut self, plugin: impl Plugin) -> &mut Self {
plugin.build(self);
self
}
pub fn add_startup_systems<M>(
&mut self,
systems: impl IntoScheduleConfigs<ScheduleSystem, M>,
) -> &mut Self {
self.startup.add_systems(systems);
self
}
pub fn add_update_systems<M>(
&mut self,
systems: impl IntoScheduleConfigs<ScheduleSystem, M>,
) -> &mut Self {
self.update.add_systems(systems);
self
}
pub fn insert_resource<R: Resource>(&mut self, resource: R) -> &mut Self {
self.world.insert_resource(resource);
self
}
pub fn init_resource<R: Resource + FromWorld>(&mut self) -> &mut Self {
self.world.init_resource::<R>();
self
}
pub fn update(&mut self) {
if !self.startup_done {
self.startup.run(&mut self.world);
self.startup_done = true;
}
self.update.run(&mut self.world);
}
}
impl Default for Application {
fn default() -> Self {
Self::new()
}
}