Skip to main content

pebble/
app.rs

1use crate::{
2    plugin::Plugin,
3    resources::Resources,
4    system::{IntoSystem, System},
5};
6use std::collections::BTreeMap;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub enum SystemStage {
10    Startup,
11    AssetSync,
12    AssetSyncDeps,
13    PreUpdate,
14    Update,
15    PostUpdate,
16    PreRender,
17    Render,
18    PostRender,
19}
20
21pub type AppRunner = Box<dyn FnOnce(App)>;
22
23pub struct App {
24    pub(crate) world: hecs::World,
25    pub(crate) resources: Resources,
26    plugins: Vec<Box<dyn Plugin>>,
27    systems: BTreeMap<SystemStage, Vec<Box<dyn System>>>,
28    runner: Option<AppRunner>,
29}
30
31impl Default for App {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl App {
38    pub fn new() -> Self {
39        let mut world = hecs::World::default();
40        let resources = Resources::new(&mut world);
41
42        Self {
43            world: world,
44            resources: resources,
45            plugins: Vec::new(),
46            systems: BTreeMap::new(),
47            runner: Some(Box::new(|mut app| {
48                loop {
49                    app.update();
50                }
51            })),
52        }
53    }
54
55    pub fn add_plugin(&mut self, plugin: impl Plugin) -> &mut Self {
56        self.plugins.push(Box::new(plugin));
57        self
58    }
59
60    pub fn add_resource(&mut self, res: impl hecs::Component) -> &mut Self {
61        self.resources.insert_resource(&mut self.world, res);
62        self
63    }
64
65    pub fn get_resource<'a, T: hecs::Component>(&'a self) -> hecs::Ref<'a, T> {
66        self.resources.get_resource(&self.world)
67    }
68
69    pub fn get_resource_mut<'a, T: hecs::Component>(&'a self) -> hecs::RefMut<'a, T> {
70        self.resources.get_resource_mut(&self.world)
71    }
72
73    pub fn try_insert_resource<T: hecs::Component>(&mut self, res: T) -> bool {
74        self.resources.try_insert(&mut self.world, res)
75    }
76
77    pub fn add_system<Marker>(
78        &mut self,
79        stage: SystemStage,
80        system: impl IntoSystem<Marker> + 'static,
81    ) -> &mut Self {
82        self.systems
83            .entry(stage)
84            .or_default()
85            .push(Box::new(system.into_system()));
86        self
87    }
88
89    pub fn build(&mut self) -> &mut Self {
90        let plugins: Vec<_> = self.plugins.drain(..).collect();
91        for plugin in plugins {
92            plugin.build(self);
93        }
94
95        if let Some(systems) = self.systems.remove(&SystemStage::Startup) {
96            for mut system in systems {
97                system.run(&self.world, &self.resources);
98            }
99            self.resources.get_command_buffer().run_on(&mut self.world);
100        }
101        self
102    }
103
104    pub fn update(&mut self) {
105        for systems in self.systems.values_mut() {
106            for system in systems {
107                system.run(&self.world, &self.resources);
108            }
109            self.resources.get_command_buffer().run_on(&mut self.world);
110        }
111    }
112
113    pub fn set_runner<F>(&mut self, runner: F) -> &mut Self
114    where
115        F: FnOnce(App) + 'static,
116    {
117        self.runner = Some(Box::new(runner));
118        self
119    }
120
121    pub fn run(&mut self) {
122        let mut owned_app = std::mem::take(self);
123        let runner = owned_app.runner.take().expect("No runner found!");
124        runner(owned_app);
125    }
126}