1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Minimal `App`/`Plugin` scaffolding on top of `bevy_ecs`.
//!
//! This crate only needs systems, components and a way to group them into
//! reusable plugins (like [`crate::ui::UiPlugin`]) — not a full engine, so we
//! skip pulling in `bevy_app` and its own windowing/rendering integration,
//! which would fight with our own `winit`/`wgpu` setup in [`crate::gpu`].
use bevy_ecs::system::ScheduleSystem;
pub use bevy_ecs::prelude::*;
/// A reusable bundle of components, resources and systems.
pub trait Plugin {
fn build(&self, app: &mut Application);
}
/// Owns the ECS `World` plus a startup schedule (run once) and an update
/// schedule (run every [`App::update`] call).
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
}
/// Adds a resource's default, leaving any already there alone.
///
/// For a plugin that reads something another plugin owns: it can stand on
/// its own without clobbering the other's state when both are installed.
pub fn init_resource<R: Resource + FromWorld>(&mut self) -> &mut Self {
self.world.init_resource::<R>();
self
}
/// Runs the startup schedule once (on the first call), then the update schedule.
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()
}
}