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
88
89
90
91
92
93
94
95
96
97
98
use crate::prelude::*;
use bevy::{prelude::*, utils::HashMap};
/// A generic library of prototypes. Accessed as a resource when you want
/// to load prototypes of type P.
#[derive(Debug, Resource)]
pub struct PrototypeLibrary<P>
where
P: Prototype,
{
prototypes: HashMap<Id<P>, P>,
}
impl<P> PrototypeLibrary<P>
where
P: Prototype,
{
/// Create a new `PrototypeLibrary`
pub fn new() -> Self {
Self {
prototypes: HashMap::new(),
}
}
/// Gets a prototype from the library by id, or None if the prototype doesn't exist
#[must_use]
pub fn get(&self, id: &Id<P>) -> Option<P> {
if let Some(p) = self.prototypes.get(id) {
let p: P = p.to_owned();
Some(p)
} else {
None
}
}
/// Gets a prototype from the library by name, or None if the prototype doesn't exist
#[must_use]
pub fn get_by_name(&self, name: &str) -> Option<P> {
self.get(&Id::from_name(name))
}
/// Gets the id of a prototype in the library by name, or None if the
/// prototype doesn't exist
#[must_use]
pub fn get_id(&self, name: &str) -> Option<Id<P>> {
let maybe_id = Id::from_name(name);
if self.get(&maybe_id).is_some() {
return Some(maybe_id);
}
None
}
/// Gets mutable access to a prototype from the library by id,
/// or None if the prototype doesn't exist
#[must_use]
pub fn get_mut(&mut self, id: &Id<P>) -> Option<&mut P> {
self.prototypes.get_mut(id)
}
/// Gets mutable access to a prototype from the library by name,
/// or None if the prototype doesn't exist
#[must_use]
pub fn get_mut_by_name(&mut self, name: &str) -> Option<&mut P> {
self.get_mut(&Id::from_name(name))
}
pub(crate) fn insert(&mut self, item: P) -> Id<P> {
let id = Id::from_name(&item.name());
self.prototypes.insert(id, item);
id
}
pub(crate) fn clear(&mut self) {
self.prototypes.clear();
}
/// Returns true if the library is empty
pub fn is_empty(&self) -> bool {
self.prototypes.is_empty()
}
/// Returns the number of prototypes in the library
pub fn len(&self) -> usize {
self.prototypes.len()
}
}
impl<T> Default for PrototypeLibrary<T>
where
T: Prototype,
{
fn default() -> Self {
Self {
prototypes: Default::default(),
}
}
}