forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload.rs
More file actions
69 lines (59 loc) · 1.95 KB
/
load.rs
File metadata and controls
69 lines (59 loc) · 1.95 KB
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
//! Implements the loading of entities.
use ahash::AHashMap;
use feather_core::entity::{EntityData, EntityDataKind};
use fecs::EntityBuilder;
pub trait EntityLoaderFn:
Fn(EntityData) -> anyhow::Result<EntityBuilder> + Send + Sync + 'static
{
}
impl<F> EntityLoaderFn for F where
F: Fn(EntityData) -> anyhow::Result<EntityBuilder> + Send + Sync + 'static
{
}
/// A registration for a function to convert an `EntityData`
/// to an `EntityBuilder` for spawning into the world. The
/// registration must provide the `EntityDataKind` it handles
/// to determine which `EntityData`s to pass to this function.
pub struct EntityLoaderRegistration {
/// The loader function.
pub f: &'static dyn EntityLoaderFn,
/// The kind of `EntityData` which this loader
/// function will accept.
pub kind: EntityDataKind,
}
impl EntityLoaderRegistration {
pub fn new(kind: EntityDataKind, f: &'static dyn EntityLoaderFn) -> Self {
Self { f, kind }
}
}
inventory::collect!(EntityLoaderRegistration);
/// Stores state for loading entities.
pub struct EntityLoader {
/// Map from `EntityDataKind` to functions
/// to load entities of those kinds.
loaders: AHashMap<EntityDataKind, &'static dyn EntityLoaderFn>,
}
impl Default for EntityLoader {
fn default() -> Self {
Self::new()
}
}
impl EntityLoader {
/// Initializes a new entity loader state. This function allocates.
pub fn new() -> Self {
let loaders = inventory::iter::<EntityLoaderRegistration>
.into_iter()
.map(|registration| (registration.kind, registration.f))
.collect();
Self { loaders }
}
}
impl EntityLoader {
/// Converts an `EntityData` into an `EntityBuilder`
/// ready for spawning in a `World`.
pub fn load(&self, data: EntityData) -> Option<anyhow::Result<EntityBuilder>> {
self.loaders
.get(&EntityDataKind::from(&data))
.map(|loader| loader(data))
}
}