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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::{
    app::{App, AppExit},
    event::Events,
    plugin::Plugin,
    stage, startup_stage, PluginGroup, PluginGroupBuilder,
};
use bevy_ecs::{FromResources, IntoQuerySystem, Resources, System, World};

/// Configure [App]s using the builder pattern
pub struct AppBuilder {
    pub app: App,
}

impl Default for AppBuilder {
    fn default() -> Self {
        let mut app_builder = AppBuilder {
            app: App::default(),
        };

        app_builder.add_default_stages();
        app_builder.add_event::<AppExit>();
        app_builder
    }
}

impl AppBuilder {
    pub fn empty() -> AppBuilder {
        AppBuilder {
            app: App::default(),
        }
    }

    pub fn resources(&self) -> &Resources {
        &self.app.resources
    }

    pub fn resources_mut(&mut self) -> &mut Resources {
        &mut self.app.resources
    }

    pub fn run(&mut self) {
        let app = std::mem::take(&mut self.app);
        app.run();
    }

    pub fn set_world(&mut self, world: World) -> &mut Self {
        self.app.world = world;
        self
    }

    pub fn add_stage(&mut self, stage_name: &'static str) -> &mut Self {
        self.app.schedule.add_stage(stage_name);
        self
    }

    pub fn add_stage_after(&mut self, target: &'static str, stage_name: &'static str) -> &mut Self {
        self.app.schedule.add_stage_after(target, stage_name);
        self
    }

    pub fn add_stage_before(
        &mut self,
        target: &'static str,
        stage_name: &'static str,
    ) -> &mut Self {
        self.app.schedule.add_stage_before(target, stage_name);
        self
    }

    pub fn add_startup_stage(&mut self, stage_name: &'static str) -> &mut Self {
        self.app.startup_schedule.add_stage(stage_name);
        self
    }

    pub fn add_startup_stage_after(
        &mut self,
        target: &'static str,
        stage_name: &'static str,
    ) -> &mut Self {
        self.app
            .startup_schedule
            .add_stage_after(target, stage_name);
        self
    }

    pub fn add_startup_stage_before(
        &mut self,
        target: &'static str,
        stage_name: &'static str,
    ) -> &mut Self {
        self.app
            .startup_schedule
            .add_stage_before(target, stage_name);
        self
    }

    pub fn add_system(&mut self, system: Box<dyn System>) -> &mut Self {
        self.add_system_to_stage(stage::UPDATE, system)
    }

    pub fn add_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
        self.add_systems_to_stage(stage::UPDATE, systems)
    }

    pub fn init_system(
        &mut self,
        build: impl FnMut(&mut Resources) -> Box<dyn System>,
    ) -> &mut Self {
        self.init_system_to_stage(stage::UPDATE, build)
    }

    pub fn init_system_to_stage(
        &mut self,
        stage: &'static str,
        mut build: impl FnMut(&mut Resources) -> Box<dyn System>,
    ) -> &mut Self {
        let system = build(&mut self.app.resources);
        self.add_system_to_stage(stage, system)
    }

    pub fn add_startup_system_to_stage(
        &mut self,
        stage_name: &'static str,
        system: Box<dyn System>,
    ) -> &mut Self {
        self.app
            .startup_schedule
            .add_system_to_stage(stage_name, system);
        self
    }

    pub fn add_startup_systems_to_stage(
        &mut self,
        stage_name: &'static str,
        systems: Vec<Box<dyn System>>,
    ) -> &mut Self {
        for system in systems {
            self.app
                .startup_schedule
                .add_system_to_stage(stage_name, system);
        }
        self
    }

    pub fn add_startup_system(&mut self, system: Box<dyn System>) -> &mut Self {
        self.app
            .startup_schedule
            .add_system_to_stage(startup_stage::STARTUP, system);
        self
    }

    pub fn add_startup_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
        self.add_startup_systems_to_stage(startup_stage::STARTUP, systems)
    }

    pub fn init_startup_system(
        &mut self,
        build: impl FnMut(&mut Resources) -> Box<dyn System>,
    ) -> &mut Self {
        self.init_startup_system_to_stage(startup_stage::STARTUP, build)
    }

    pub fn init_startup_system_to_stage(
        &mut self,
        stage: &'static str,
        mut build: impl FnMut(&mut Resources) -> Box<dyn System>,
    ) -> &mut Self {
        let system = build(&mut self.app.resources);
        self.add_startup_system_to_stage(stage, system)
    }

    pub fn add_default_stages(&mut self) -> &mut Self {
        self.add_startup_stage(startup_stage::PRE_STARTUP)
            .add_startup_stage(startup_stage::STARTUP)
            .add_startup_stage(startup_stage::POST_STARTUP)
            .add_stage(stage::FIRST)
            .add_stage(stage::PRE_EVENT)
            .add_stage(stage::EVENT)
            .add_stage(stage::PRE_UPDATE)
            .add_stage(stage::UPDATE)
            .add_stage(stage::POST_UPDATE)
            .add_stage(stage::LAST)
    }

    pub fn add_system_to_stage(
        &mut self,
        stage_name: &'static str,
        system: Box<dyn System>,
    ) -> &mut Self {
        self.app.schedule.add_system_to_stage(stage_name, system);
        self
    }

    pub fn add_system_to_stage_front(
        &mut self,
        stage_name: &'static str,
        system: Box<dyn System>,
    ) -> &mut Self {
        self.app
            .schedule
            .add_system_to_stage_front(stage_name, system);
        self
    }

    pub fn add_systems_to_stage(
        &mut self,
        stage_name: &'static str,
        systems: Vec<Box<dyn System>>,
    ) -> &mut Self {
        for system in systems {
            self.app.schedule.add_system_to_stage(stage_name, system);
        }
        self
    }

    pub fn add_event<T>(&mut self) -> &mut Self
    where
        T: Send + Sync + 'static,
    {
        self.add_resource(Events::<T>::default())
            .add_system_to_stage(stage::EVENT, Events::<T>::update_system.system())
    }

    /// Adds a resource to the current [App] and overwrites any resource previously added of the same type.
    pub fn add_resource<T>(&mut self, resource: T) -> &mut Self
    where
        T: Send + Sync + 'static,
    {
        self.app.resources.insert(resource);
        self
    }

    pub fn add_thread_local_resource<T>(&mut self, resource: T) -> &mut Self
    where
        T: 'static,
    {
        self.app.resources.insert_thread_local(resource);
        self
    }

    pub fn init_resource<R>(&mut self) -> &mut Self
    where
        R: FromResources + Send + Sync + 'static,
    {
        let resource = R::from_resources(&self.app.resources);
        self.app.resources.insert(resource);

        self
    }

    pub fn init_thread_local_resource<R>(&mut self) -> &mut Self
    where
        R: FromResources + 'static,
    {
        let resource = R::from_resources(&self.app.resources);
        self.app.resources.insert_thread_local(resource);

        self
    }

    pub fn set_runner(&mut self, run_fn: impl Fn(App) + 'static) -> &mut Self {
        self.app.runner = Box::new(run_fn);
        self
    }

    pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
    where
        T: Plugin,
    {
        log::debug!("added plugin: {}", plugin.name());
        plugin.build(self);
        self
    }

    pub fn add_plugins<T: PluginGroup>(&mut self, mut group: T) -> &mut Self {
        let mut plugin_group_builder = PluginGroupBuilder::default();
        group.build(&mut plugin_group_builder);
        plugin_group_builder.finish(self);
        self
    }

    pub fn add_plugins_with<T, F>(&mut self, mut group: T, func: F) -> &mut Self
    where
        T: PluginGroup,
        F: FnOnce(&mut PluginGroupBuilder) -> &mut PluginGroupBuilder,
    {
        let mut plugin_group_builder = PluginGroupBuilder::default();
        group.build(&mut plugin_group_builder);
        func(&mut plugin_group_builder);
        plugin_group_builder.finish(self);
        self
    }
}