forked from amethyst/amethyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_data.rs
87 lines (75 loc) · 2.35 KB
/
game_data.rs
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
use amethyst::{
core::{ArcThreadPool, SystemBundle},
ecs::prelude::{Dispatcher, DispatcherBuilder, System, World},
DataInit, Error, Result,
};
pub struct CustomGameData<'a, 'b> {
pub base: Dispatcher<'a, 'b>,
pub running: Dispatcher<'a, 'b>,
}
impl<'a, 'b> CustomGameData<'a, 'b> {
/// Update game data
pub fn update(&mut self, world: &World, running: bool) {
if running {
self.running.dispatch(&world.res);
}
self.base.dispatch(&world.res);
}
}
pub struct CustomGameDataBuilder<'a, 'b> {
pub base: DispatcherBuilder<'a, 'b>,
pub running: DispatcherBuilder<'a, 'b>,
}
impl<'a, 'b> Default for CustomGameDataBuilder<'a, 'b> {
fn default() -> Self {
CustomGameDataBuilder::new()
}
}
impl<'a, 'b> CustomGameDataBuilder<'a, 'b> {
pub fn new() -> Self {
CustomGameDataBuilder {
base: DispatcherBuilder::new(),
running: DispatcherBuilder::new(),
}
}
pub fn with_base<S>(mut self, system: S, name: &str, dependencies: &[&str]) -> Self
where
for<'c> S: System<'c> + Send + 'a,
{
self.base.add(system, name, dependencies);
self
}
pub fn with_base_bundle<B>(mut self, bundle: B) -> Result<Self>
where
B: SystemBundle<'a, 'b>,
{
bundle
.build(&mut self.base)
.map_err(|err| Error::Core(err))?;
Ok(self)
}
pub fn with_running<S>(mut self, system: S, name: &str, dependencies: &[&str]) -> Self
where
for<'c> S: System<'c> + Send + 'a,
{
self.running.add(system, name, dependencies);
self
}
}
impl<'a, 'b> DataInit<CustomGameData<'a, 'b>> for CustomGameDataBuilder<'a, 'b> {
fn build(self, world: &mut World) -> CustomGameData<'a, 'b> {
#[cfg(not(no_threading))]
let pool = world.read_resource::<ArcThreadPool>().clone();
#[cfg(not(no_threading))]
let mut base = self.base.with_pool(pool.clone()).build();
#[cfg(no_threading)]
let mut base = self.base.build();
base.setup(&mut world.res);
#[cfg(not(no_threading))]
let mut running = self.running.with_pool(pool.clone()).build();
#[cfg(no_threading)]
let mut running = self.running.build();
running.setup(&mut world.res);
CustomGameData { base, running }
}
}