forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.rs
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy pathload.rs
File metadata and controls
38 lines (34 loc) · 1.12 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
use ahash::AHashMap;
use feather_core::anvil::entity::{EntityData, EntityDataKind};
use feather_server_types::{EntityLoaderFn, EntityLoaderRegistration};
use fecs::EntityBuilder;
/// 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))
}
}