Skip to main content

ecson_core/
plugin.rs

1//! プラグインシステムの基盤
2
3use bevy_ecs::world::World;
4
5use crate::app::*;
6
7/// プラグインのライフサイクル状態を表す列挙型。
8#[derive(PartialEq, Eq, Debug, Clone, Copy, PartialOrd, Ord)]
9pub enum PluginsState {
10    Adding,
11    Ready,
12    Finished,
13    Cleaned,
14}
15
16/// 個別のプラグインが実装する基本トレイト。
17pub trait Plugin {
18    fn build(&self, app: &mut EcsonApp);
19
20    /// シャットダウン時に呼ばれるクリーンアップ処理
21    fn cleanup(&self, _app: &mut World) {}
22}
23
24/// `app.add_plugins()` に単一の `Plugin` や複数の `Plugin` タプルを渡せるようにするトレイト。
25pub trait Plugins {
26    fn add_to_app(self, app: &mut EcsonApp);
27}
28
29impl<P: Plugin + 'static> Plugins for P {
30    fn add_to_app(self, app: &mut EcsonApp) {
31        self.build(app);
32        app.plugins.push(Box::new(self));
33    }
34}
35
36macro_rules! impl_plugins_for_tuples {
37    ($($name:ident),*) => {
38        impl<$($name: Plugin + 'static),*> Plugins for ($($name,)*) {
39            #[allow(non_snake_case)]
40            fn add_to_app(self, app: &mut EcsonApp) {
41                let ($($name,)*) = self;
42                $(
43                    $name.build(app);
44                    app.plugins.push(Box::new($name));
45                )*
46            }
47        }
48    };
49}
50
51impl_plugins_for_tuples!(P1);
52impl_plugins_for_tuples!(P1, P2);
53impl_plugins_for_tuples!(P1, P2, P3);
54impl_plugins_for_tuples!(P1, P2, P3, P4);
55impl_plugins_for_tuples!(P1, P2, P3, P4, P5);
56impl_plugins_for_tuples!(P1, P2, P3, P4, P5, P6);